Example usage for org.w3c.dom Element appendChild

List of usage examples for org.w3c.dom Element appendChild

Introduction

In this page you can find the example usage for org.w3c.dom Element appendChild.

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

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   www .  jav  a2 s. c  o  m
 * @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;
}

From source file:Main.java

/**
 * add profiles section under pomRoot/*www .  ja  v  a  2  s.  c o m*/
 * @param pomRoot
 * @return the profiles node
 */
private static Element addProfilesSection(Document document, Element root) {
    Node profiles = document.createElement(PROFILES_NODE_NAME);
    root.appendChild(profiles);
    return (Element) profiles;
}

From source file:DOMEdit.java

public static void append(Document doc, String name, String phone, String email) {
        Element personNode = doc.createElement("person");

        Element nameNode = doc.createElement("name");
        personNode.appendChild(nameNode);
        Text nametextNode = doc.createTextNode(name);
        nameNode.appendChild(nametextNode);

        Element phoneNode = doc.createElement("phone");
        personNode.appendChild(phoneNode);
        Text phonetextNode = doc.createTextNode(phone);
        phoneNode.appendChild(phonetextNode);

        Element emailNode = doc.createElement("email");
        personNode.appendChild(emailNode);
        Text emailtextNode = doc.createTextNode(email);
        emailNode.appendChild(emailtextNode);

        Element root = doc.getDocumentElement();
        root.appendChild(personNode);/*w w w.jav a 2s  . co m*/
    }

From source file:Utils.java

public static Element findElementElseCreateAndAttribute(Document document, Element parent, String element,
        String attributeName, String attributeValue) {
    NodeList nl = parent.getElementsByTagName(element);
    Element e = null;//from  w w  w.ja  v  a 2 s.c o  m

    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(element));
        e = (Element) parent.getElementsByTagName(element).item(0);
        e.setAttribute(attributeName, attributeValue);
    }

    return e;
}

From source file:Main.java

/**
 * Adds a new {@link Element} with the given text to the given parent
 * {@link Element}./*  www .j ava2 s.  c  o m*/
 * 
 * @param doc the document to add to.
 * @param parent the parent element.
 * @param name the name of the new element.
 * @param value the text value.
 * @return the created element.
 */
public static Element addTextElement(Document doc, Node parent, String name, String value) {
    if (doc == null) {
        doc = parent.getOwnerDocument();
    }
    final Element elem = addElement(doc, parent, name);
    if (value != null) {
        elem.appendChild(doc.createTextNode(value));
    }
    return elem;
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomSerializer.java

private static Element feed(final AtomFeed feed) throws ParserConfigurationException {
    final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element feedElem = doc.createElement(ODataConstants.ATOM_ELEM_FEED);
    feedElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    feedElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    feedElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    feedElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    feedElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (feed.getBaseURI() != null) {
        feedElem.setAttribute(ODataConstants.ATTR_XMLBASE, feed.getBaseURI().toASCIIString());
    }//w  w  w. jav  a2s  .c  om
    doc.appendChild(feedElem);

    if (StringUtils.isNotBlank(feed.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(feed.getTitle()));
        feedElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(feed.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(feed.getSummary()));
        feedElem.appendChild(summary);
    }

    for (AtomEntry entry : feed.getEntries()) {
        feedElem.appendChild(doc.importNode(entry(entry), true));
    }

    return feedElem;
}

From source file:Main.java

/**
 * Sets element TEXT data//w  ww . j a  v a2s.c o  m
 * 
 * @param e
 *            the element
 * @param data
 *            the new data
 */
public static void setElementTextValue(Element e, String data) {
    Text txt = getElementTextNode(e);
    if (txt != null) {
        txt.setData(data);
    } else {
        txt = e.getOwnerDocument().createTextNode(data);
        e.appendChild(txt);
    }
}

From source file:Main.java

public static <K, V> String mapToXml(Map<K, V> map, String rootName, String childName, String keyName,
        String valueName) throws TransformerException, ParserConfigurationException {
    Document document = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().newDocument();

    Element root = document.createElement(rootName);
    document.appendChild(root);//from   ww  w  .jav  a  2 s.  c o  m

    for (Map.Entry<K, V> errorCodeReport : map.entrySet()) {
        Element s = document.createElement(childName);
        root.appendChild(s);

        s.setAttribute(keyName, errorCodeReport.getKey().toString());
        s.setAttribute(valueName, errorCodeReport.getValue().toString());
    }

    return getStringFromDocument(document);
}

From source file:Main.java

public static void encodeStringList(Document doc, Element parent, List<String> list, String namespaceURI,
        String qualifiedName) {/*from  ww  w.ja  va 2s .com*/
    Element currElm;
    Text currVal;

    if (list != null) {
        for (String item : list) {

            currElm = doc.createElementNS(namespaceURI, qualifiedName);
            currVal = doc.createTextNode(item);

            currElm.appendChild(currVal);
            parent.appendChild(currElm);
        }
    }
}

From source file:Main.java

/**
 * Creates a text node with the given content and appends it as child to the given element.
 * //w ww  . ja  v a 2s . co  m
 * @param domElement the element to recieve the text node
 * @param textContent the content for the text node
 */
public static void appendTextContent(Element domElement, String textContent) {
    if (textContent == null) {
        return;
    }
    Document parentDocument = domElement.getOwnerDocument();
    Text textNode = parentDocument.createTextNode(textContent);
    domElement.appendChild(textNode);
}