List of usage examples for org.w3c.dom Node appendChild
public Node appendChild(Node newChild) throws DOMException;
newChild
to the end of the list of children of this node. From source file:Main.java
public static Element addTextElement(Node parent, String name, String value, Attr[] attrs) { Element element;//from w w w . j a v a 2s .com if (parent instanceof Document) { element = ((Document) parent).createElement(name); } else { element = parent.getOwnerDocument().createElement(name); } if (attrs != null && attrs.length > 0) { for (Attr attr : attrs) { element.setAttributeNode(attr); } } if (value != null) { element.setTextContent(value); } parent.appendChild(element); return element; }
From source file:Main.java
/** * The method creates an element with specified name and attributes and appends it to the parent element. * This is a convenience method for often used sequence of calls. * * @param parent - the node where to append the new element. * @param name - the name of the element type being created. * @param attributes - string array of pairs attributes and their values. * Each attribute must have a value, so the array must have even number of elements. * @return the newly created element.// ww w.j a v a 2s . co m * * @throws ArrayIndexOutOfBoundsException in case of odd number of elements of the attribute array * (i.e. the last attribute is missing a value). */ public static Element appendElement(Node parent, String name, String[] attributes) { Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument(); Element element = doc.createElement(name); if (attributes != null) { int attrLen = attributes.length; for (int i = 0; i < attrLen; i += 2) { String attrName = attributes[i]; String attrValue = attributes[i + 1]; element.setAttribute(attrName, attrValue); } } parent.appendChild(element); return element; }
From source file:Main.java
public static Node appendTextNode(Node parent, String child) { if (parent == null) System.out.println("parent null 0"); if (parent instanceof Document) parent = ((Document) parent).getDocumentElement(); if (parent == null) System.out.println("parent null 1"); if (parent.getOwnerDocument() == null) System.out.println("parent ownerdoc null"); if (child == null) System.out.println("child null"); Text newNode = parent.getOwnerDocument().createTextNode(child); return parent.appendChild(newNode); }
From source file:Main.java
/** * Sets the text for a node/*from www .jav a 2s . c o m*/ */ public synchronized static void setText(Node n, String text) { if (n == null) throw new IllegalArgumentException("Node argument cannot be null"); if (text == null) throw new IllegalArgumentException("Node text argument cannot be null"); NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) { nl.item(i).setNodeValue(text); return; } } Node textNode = n.getOwnerDocument().createTextNode(text); n.appendChild(textNode); }
From source file:Main.java
/** * @param parent/*from w ww . j a va2 s.c o m*/ * node to add fragment to * @param fragment * a well formed XML fragment * @throws ParserConfigurationException */ public static void appendXmlFragment(Node parent, String fragment) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder docBuilder = domFactory.newDocumentBuilder(); Document doc = parent.getOwnerDocument(); Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement(); fragmentNode = doc.importNode(fragmentNode, true); parent.appendChild(fragmentNode); }
From source file:curam.molsa.test.customfunctions.MOLSADOMReader.java
/** * Get unique child from parent node. If not exists, creates child. * /* ww w . j a va 2 s . c o m*/ * @param parent * @param name * @return nodes */ public static Element child(final Node parent, final String name) { final List<Node> nodes = nodes(parent, name); if (nodes.isEmpty()) { final Element element = parent.getOwnerDocument().createElement(name); parent.appendChild(element); return element; } else { if (nodes.size() > 1) { throw new RuntimeException("There are more than one element with name=" + name); } return (Element) nodes.get(0); } }
From source file:com.hphoto.server.ApiServlet.java
private static Element addNode(Document doc, Node parent, String name) { Element child = doc.createElement(name); parent.appendChild(child); return child; }
From source file:ca.uqac.info.trace.generation.AmazonEcsGenerator.java
private static Node createKeyValue(EventTrace trace, String key, String value) { Node n = trace.createElement(key); Node v = trace.createTextNode(value); n.appendChild(v); return n;//from w ww. j a v a 2s . c om }
From source file:Main.java
/** * Creates a new <code>Node</code>-object with the specified <code>name</code> * and appends it as a child element to the provided <code>Node</code>- * parameter.//from w ww . j a v a2 s .c om * * @param elem the <code>Node</code> to which the newly created * <code>Node</code> will be appended to. Not allowed to be <code>null</code>. * @param elemName the name of the to-be-created <code>Element</code>, not allowed * to be empty or <code>null</code>. * @return the created element * @exception IllegalArgumentException if <code>elem</code> and/ord * <code>elemName</code> are null (or empty in the case of <code>elemName</code>) */ public static Element createChild(Node node, String elemName) { if (node == null || elemName == null || "".equals(elemName)) throw new IllegalArgumentException("Arguments are not allowed to be null or empty"); Document document = null; if (node instanceof Document) { document = (Document) node; } else if (node.getOwnerDocument() != null) { document = node.getOwnerDocument(); } Element newChild = null; if (document != null) { newChild = document.createElement(elemName); node.appendChild(newChild); } return newChild; }
From source file:Main.java
/** * Same as {@link createChild(Node, String) createChild()}, but adds the * specified <code>text</code> to the newly created <code>Node</code>. * /*from ww w .j a v a 2 s .com*/ * @see #createChild(Node, String) * @param elem the <code>Node</code> to which the newly created * <code>Node</code> will be appended to. Not allowed to be <code>null</code>. * @param elemName the name of the to-be-created <code>Node</code>, not allowed * to be empty or <code>null</code>. * @param text the text-contents of the created/inserted node * @return the created element * @exception IllegalArgumentException if <code>elem</code> and/ord * <code>elemName</code> are null (or empty in the case of <code>elemName</code>) */ public static Element createTextChild(Node node, String elemName, String text) { if (node == null || elemName == null || "".equals(elemName)) throw new IllegalArgumentException("Arguments are not allowed to be null or empty"); Document document = null; if (node instanceof Document) { document = (Document) node; } else if (node.getOwnerDocument() != null) { document = node.getOwnerDocument(); } Element newChild = null; if (document != null) { newChild = document.createElement(elemName); node.appendChild(newChild); newChild.appendChild(document.createTextNode(text)); } return newChild; }