Java XML Element Create createElementWithText(Element parent, String name, String value)

Here you can find the source of createElementWithText(Element parent, String name, String value)

Description

helper method, creates a subelement with text embedded.

License

Apache License

Parameter

Parameter Description
parent a parameter
name a parameter
value a parameter

Declaration

public static Element createElementWithText(Element parent, String name, String value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Element;

import org.w3c.dom.Document;

public class Main {
    /**// w w w.  j  a  va2 s.c  om
     * helper method, creates a subelement with text embedded. does not format
     * the result. primarily to be used in cases like
     * <code>&lt;goals&gt;&lt;goal&gt;xxx&lt;/goal&gt;&lt;/goals&gt;</code>
     * 
     * @param parent
     * @param name
     * @param value
     * @return
     */
    public static Element createElementWithText(Element parent, String name, String value) {
        Document doc = parent.getOwnerDocument();
        Element newElement = doc.createElement(name);
        parent.appendChild(newElement);
        newElement.appendChild(doc.createTextNode(value));
        return newElement;
    }

    /**
     * helper method, creates a subelement, does not format result.
     * 
     * @param parent
     *            the parent element
     * @param name
     *            the name of the new element
     * @return the created element
     */
    public static Element createElement(Element parent, String name) {
        Document doc = parent.getOwnerDocument();
        Element newElement = doc.createElement(name);
        parent.appendChild(newElement);
        return newElement;
    }
}

Related

  1. createElementMappingForNodes(final Node n1, final Node n2, final Map mapping)
  2. createElementNS(Document doc, String namespaceURI, String prefix, String nodeName)
  3. createElementNS(Document root, String namespaceURI, String qualifiedName)
  4. createElementNS(String namespaceURI, String qualifiedName, String text, Document doc)
  5. createElementNsIn(Element parent, String ns, String name, String textContent)
  6. createElmWithText(Document doc, String tagName, String text)
  7. createNewElementAndSetAndAttribute(Document document, Element parent, String childElement, String childValue, String attributeName, String attributeValue)
  8. createNode(Document doc, Element parent, String nodeName, Object[] attr)
  9. createNode(Document doc, Element parent, String nodeName, Object[] attr)