@Override public void startElement(String uri, String localName, String qName, Attributes attributes) { Node parent = getParent(); Element element = this.document.createElementNS(uri, qName); for (int i = 0; i < attributes.getLength(); i++) { String attrUri = attributes.getURI(i); String attrQname = attributes.getQName(i); String value = attributes.getValue(i); if (!attrQname.startsWith("xmlns")) { element.setAttributeNS(attrUri, attrQname, value); } } element = (Element) parent.appendChild(element); this.elements.add(element); }
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
@Override public void characters(char[] ch, int start, int length) { String data = new String(ch, start, length); Node parent = getParent(); Node lastChild = parent.getLastChild(); if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) { ((Text) lastChild).appendData(data); } else { Text text = this.document.createTextNode(data); parent.appendChild(text); } }
public static void main(String[] args) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse("server.xml"); Element root = document.getDocumentElement(); Element newServer = document.createElement("server"); Element name = document.createElement("name"); name.appendChild(document.createTextNode(server.getName())); newServer.appendChild(name); Element port = document.createElement("port"); port.appendChild(document.createTextNode(Integer.toString(server.getPort()))); newServer.appendChild(port); root.appendChild(newServer);
File... files) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); docBuilderFactory .setIgnoringElementContentWhitespace(true); DocumentBuilder docBuilder = docBuilderFactory .newDocumentBuilder(); Document base = docBuilder.parse(files[0]); nextResults.removeChild(kid); kid = base.importNode(kid, true); results.appendChild(kid);
nl = (NodeList) exprFrom.evaluate(source, XPathConstants.NODESET); } else if (source instanceof String) { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.newDocument(); Element temp = doc.createElementNS(null, "temp"); temp.appendChild(doc.createTextNode((String) source)); nl = temp.getChildNodes(); } else if (source == null) { targetElem = ((Text) nl.item(i)).getWholeText(); } else { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.newDocument(); targetElem = doc.importNode(nl.item(i), true); ((Element) targetElem).setAttributeNode((Attr) n); } else { ((org.w3c.dom.Node) targetElem).appendChild(n);
Element node = factory.createElement("foundJar"); node.setAttribute("name", keyStr.substring(0, keyStr.indexOf("-"))); node.setAttribute("desc", keyStr.substring(keyStr.indexOf("-") + 1)); node.appendChild(factory.createTextNode((String)subhash.get(keyStr))); container.appendChild(node); Element node = factory.createElement("foundJar"); node.appendChild(factory.createTextNode(ERROR + " Reading " + key + " threw: " + e.toString())); container.appendChild(node);
.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); DOMImplementation domImpl = db.getDOMImplementation(); Document document = buildExampleDocumentWithNamespaces(domImpl); Document document = domImpl.createDocument("urn:example.namespace", "my:example", null); Element element = document.createElementNS("http://another.namespace", "element"); document.getDocumentElement().appendChild(element); return document;
Element root = xmlDoc.createElement("booking"); item = xmlDoc.createElement("bookingID"); item.appendChild(xmlDoc.createTextNode("115")); root.appendChild(item); xmlDoc.appendChild(root);
protected void appendElement(org.w3c.dom.Node parentNode) { DocumentNavigator docNav = new DocumentNavigator(); Document ownerDocument = parentNode.getOwnerDocument(); if (ownerDocument == null) { // If the parentNode is a Document itself, it's ownerDocument is // null ownerDocument = (Document) parentNode; } String elementName = docNav.getElementName(this); Element element = ownerDocument.createElement(elementName); parentNode.appendChild(element); for (Iterator<Attribute> iter = docNav.getAttributeAxisIterator(this); iter.hasNext();) { Attribute attr = iter.next(); element.setAttribute(attr.getName(), attr.getStringValue()); } for (Iterator<Node> iter = docNav.getChildAxisIterator(this); iter.hasNext();) { AbstractNode child = (AbstractNode) iter.next(); child.appendElement(element); } }
static class LoggingErrorListener implements ErrorListener { // See http://www.cafeconleche.org/slides/sd2003west/xmlandjava/346.html boolean strict; public LoggingErrorListener(boolean strict) { } public void warning(TransformerException exception) { log.warn(exception.getMessage(), exception); // Don't throw an exception and stop the processor // just for a warning; but do log the problem } public void error(TransformerException exception) throws TransformerException { log.error(exception.getMessage(), exception); // XSLT is not as draconian as XML. There are numerous errors // which the processor may but does not have to recover from; // e.g. multiple templates that match a node with the same // priority. If I do not want to allow that, I'd throw this // exception here. if (strict) { throw exception; }
private static Element configVServiceNodeDetails(Document doc, String vlanId, String ipAddr) { Element configure = doc.createElementNS(s_ciscons, "nxos:configure"); Element modeConfigure = doc.createElement("nxos:" + s_configuremode); configure.appendChild(modeConfigure); Element vservice = doc.createElement("vservice"); vservice.appendChild(doc.createElement("node")) .appendChild(doc.createElement("ASA_" + vlanId)) .appendChild(doc.createElement("type")) .appendChild(doc.createElement("asa")); modeConfigure.appendChild(vservice); Element address = doc.createElement(s_paramvalue); address.setAttribute("isKey", "true"); address.setTextContent(ipAddr); modeConfigure.appendChild(doc.createElement("ip")).appendChild(doc.createElement("address")).appendChild(doc.createElement("value")).appendChild(address); modeConfigure.appendChild(doc.createElement("adjacency")) .appendChild(doc.createElement("l2")) .appendChild(doc.createElement("vlan")) .appendChild(doc.createElement("value")) .appendChild(vlan); modeConfigure.appendChild(doc.createElement("fail-mode")).appendChild(doc.createElement("close"));
DocumentBuilder builder = XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder(); data.targetDOM = builder.parse( inputSource ); if ( !meta.isComplexJoin() ) { DocumentBuilder builder = XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder(); Document joinDocument = builder.parse( new InputSource( new StringReader( strJoinXML ) ) ); data.targetNode.appendChild( node ); } catch ( Exception e ) { throw new KettleException( e );
public void startElement(String namespace, String localName, String qName, Attributes attrs) final Element tmp = (Element)_document.createElementNS(namespace, qName); for (int i = 0; i < nattrs; i++) { if (attrs.getLocalName(i) == null) { tmp.setAttribute(attrs.getQName(i), attrs.getValue(i)); last.insertBefore(tmp, _nextSibling); else last.appendChild(tmp);
Node previousTop = top; if ((localName == null) || (localName.length() == 0)) { top = doc.createElement(qName); } else { top = doc.createElementNS(namespaceURI, localName); previousTop.appendChild(top); depth++; } catch (DOMException e) {
private Node createElement(Document doc, Node currentNode, String axisName) { Node selectedNode; if (isNamespaceDeclared()) { selectedNode =doc.createElementNS(getNamespace(),axisName); } else { selectedNode = doc.createElementNS("", axisName); } currentNode.appendChild(selectedNode); return selectedNode; }
protected Element createChildElement(QName name, Node parent) { Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument(); Element elem; if ("".equals(name.getNamespaceURI())) { elem = doc.createElement(name.getLocalPart()); } else { elem = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart()); } parent.appendChild(elem); return elem; }
/** * Add a contained class. * * @param info information about a class * @throws DiffException when there is an underlying exception, e.g. * writing to a file caused an IOException */ public void contains(ClassInfo info) throws DiffException { Element tmp = doc.createElementNS(XML_URI, "class"); tmp.setAttribute("name", info.getName()); currentNode.appendChild(tmp); }
private static Element createChild(Node parent, String name, String text) { Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument(); Element child = doc.createElementNS("http://java.sun.com/xml/ns/jbi/management-message", name); if (text != null) { child.appendChild(doc.createTextNode(text)); } parent.appendChild(child); return child; }
Element e = doc.createElementNS(uri, qName); node.appendChild(e); node = e;