List of usage examples for org.w3c.dom Element appendChild
public Node appendChild(Node newChild) throws DOMException;
newChild
to the end of the list of children of this node. From source file:Utils.java
public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child, String value) {/* ww w . j av a 2 s .c om*/ NodeList nl = parent.getElementsByTagName(child); if (nl.getLength() == 0) { parent.appendChild(document.createElement(child)); } Element ret = (Element) parent.getElementsByTagName(child).item(0); if (ret.getFirstChild() != null) { ret.removeChild(ret.getFirstChild()); } ret.appendChild(document.createTextNode(value)); return ret; }
From source file:Main.java
/** * Processes any element and add them into the element. * * @param element the element where to add the elements. * @param any the any elements to process. * @throws Exception if there is some error during processing. *//*from w w w.j ava 2s. c om*/ public static void processAny(Element element, List<Element> any) throws Exception { for (Element anyElement : any) { Node importedElement = element.getOwnerDocument().importNode(anyElement, true); element.appendChild(importedElement); } }
From source file:Main.java
/** * Convert Properties to string//from ww w .ja v a 2 s .c o m * * @param props * @return xml string * @throws IOException */ public static String writePropToString(Properties props) throws IOException { try { org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); org.w3c.dom.Element conf = doc.createElement("configuration"); doc.appendChild(conf); conf.appendChild(doc.createTextNode("\n")); for (Enumeration e = props.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); Object object = props.get(name); String value; if (object instanceof String) { value = (String) object; } else { continue; } org.w3c.dom.Element propNode = doc.createElement("property"); conf.appendChild(propNode); org.w3c.dom.Element nameNode = doc.createElement("name"); nameNode.appendChild(doc.createTextNode(name.trim())); propNode.appendChild(nameNode); org.w3c.dom.Element valueNode = doc.createElement("value"); valueNode.appendChild(doc.createTextNode(value.trim())); propNode.appendChild(valueNode); conf.appendChild(doc.createTextNode("\n")); } Source source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (Exception e) { throw new IOException(e); } }
From source file:Main.java
public static void setTextContent(Element elem, String content) { Document doc = elem.getOwnerDocument(); if ((content != null) && !content.equals("")) { Text textNode = doc.createTextNode(content); elem.appendChild(textNode); }// w ww. ja v a 2s .c o m }
From source file:Main.java
public static Element addElement(Element parent, String childName, String textValue, Document doc) { Element childElement = doc.createElementNS(DEFAULT_NAMESPACE, childName); childElement.setTextContent(textValue); parent.appendChild(childElement); return childElement; }
From source file:Main.java
/** * Adds an {@link Element} child to the given element. * * @param parent The {@link Element} to add the {@link Element} child to. * @param qualifiedName The fully qualified name of the new {@link Element} * child./* www. j a v a 2 s. c o m*/ * @return Returns the new {@link Element} child. */ public static Element addElementChildTo(Element parent, String nsURI, String qualifiedName) { final Document doc = parent.getOwnerDocument(); final Element child = doc.createElementNS(nsURI, qualifiedName); parent.appendChild(child); return child; }
From source file:Main.java
public static Element createElement(Element parent, String path) { int i = path.indexOf('.'); if (i < 0) { Element element = parent.getOwnerDocument().createElement(path); parent.appendChild(element); return element; }//from ww w . j av a2 s .c o m String p = path.substring(0, i), c = path.substring(i + 1); Element pe = getUniqueChild(parent, p); if (pe == null) { pe = parent.getOwnerDocument().createElement(p); parent.appendChild(pe); } return createElement(pe, c); }
From source file:Main.java
public static Element appendCDATAElement(Element parent, String tagName, String value) { Element child = appendElement(parent, tagName); if (value == null) { value = ""; }//from w w w .jav a 2 s . c o m CDATASection cdata = child.getOwnerDocument().createCDATASection(value); child.appendChild(cdata); return child; }
From source file:Utils.java
public static Element findElementElseCreateAndSet(Document document, Element parent, String child, String value) {// w w w. j a v a2 s . c om Element ret = null; NodeList nl = parent.getElementsByTagName(child); if (nl.getLength() == 0) { parent.appendChild(document.createElement(child)); ret = (Element) parent.getElementsByTagName(child).item(0); ret.appendChild(document.createTextNode(value)); } return ret; }
From source file:Main.java
/** * Add the element to the parent by the node name * * @param parent/* w w w . j a v a 2s . c o m*/ * @param tagName */ public static void addElement(Element parent, String tagName) { Document doc = parent.getOwnerDocument(); Element child = doc.createElement(tagName); parent.appendChild(child); }