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

/**
 * Adds a commentary to an element of a document in memory <br>
 * (It's supposed that the element is one of the document)
 * /*from   w w w .jav  a2  s . c o m*/
 * @param doc
 *            Document of the element where the commentary will be added
 * @param element
 *            The xml elemnet where the commentary will be added
 * @param commentary
 *            The commentary text
 * 
 * @return org.w3c.dom.Comment
 */
public static Comment addCommentary(Document doc, Element element, String commentary) {
    // Creates the commentary and adds it to the element
    Comment comment = doc.createComment(commentary);
    element.appendChild(comment);

    return comment;
}

From source file:Main.java

public static void createElement(Element parent, String elementName, String text) {
    Document doc = parent.getOwnerDocument();
    Element id = doc.createElement(elementName);
    id.setTextContent(text);//from   w w w .  jav a  2 s .  c om
    parent.appendChild(id);
}

From source file:Main.java

public static Document createXMLResult(String rootName, Map<String, String> map) {

    try {/*from  ww w.  java  2s  .  c  o m*/
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element root = doc.createElement(rootName);
        doc.appendChild(root);
        for (String elementName : map.keySet()) {
            Element child = doc.createElement(elementName);
            Text text = doc.createTextNode(map.get(elementName));
            child.appendChild(text);
            root.appendChild(child);
        }

        return doc;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

/**
 * Add literal text to the supplied element.
 * @param element Target DOM Element./*from ww w  .j  av  a 2 s.  com*/
 * @param literalText Literal text to be added.
 * @return if true all the operation are done. 
 */
public static boolean addLiteral(Element element, String literalText) {
    try {
        Document document = element.getOwnerDocument();
        Text literal = document.createTextNode(literalText);
        element.appendChild(literal);
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Creates a DOM element node with the given name and contents. If indicated
 * the contents is enclosed in CDATA marks. The tag ends with a new line
 * character.//from  ww  w .ja va 2 s.  com
 * 
 * @param d
 *            the "mother" document of the created Element
 * @param name
 *            the name of the element
 * @param value
 *            the element's value
 * @param isCDATA
 *            a flag indicating if this element contains (unformatted)
 *            CDATA.
 * @return a new DOM Element
 */
public static Element createElementLn(Document d, String name, String value, boolean isCDATA) {
    Element e = createElement(d, name);
    if (isCDATA) {
        e.appendChild(createTextLn(d));
        e.appendChild(createCDATA(d, value));
        e.appendChild(createTextLn(d));
    } else {
        e.appendChild(createTextLn(d, "\n" + value));
    }
    return e;
}

From source file:Main.java

public static Element getPhoneTypeWithText(Document doc, String elementName, String areaCodeAndPhone) {
    Element elem_phone_type = doc.createElement(elementName);
    Element elem_phone = getElementWithText(doc, "Phone", areaCodeAndPhone);

    elem_phone_type.appendChild(elem_phone);

    return elem_phone_type;
}

From source file:Main.java

public static void insertValue(Element parent, String childName, String childNamespace, String value) {
    Element child = parent.getOwnerDocument().createElementNS(childNamespace, childName);
    child.setTextContent(value);/*  ww  w.ja  v a2  s  .  c om*/
    parent.appendChild(child);
}

From source file:Main.java

public static Element createParagraphElement(String name, String content, Document doc) {

    Element element = doc.createElement(name);

    BufferedReader br = new BufferedReader(new StringReader(content));

    try {/*from  ww  w . j  a  v a2s  . c  om*/
        while (br.ready()) {
            // Create an element for this node
            String p = br.readLine();

            // To avoid an infinite loop
            if (p == null)
                break;
            Element pe = doc.createElement("p");

            //Add text to paragraph node
            Text t = doc.createTextNode(p);
            pe.appendChild(t);
            element.appendChild(pe);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    return element;
}

From source file:Main.java

static public void setElementText(Element elm, String text) {
    Node node = elm.getFirstChild();
    if (node == null) {
        if (text != null)
            elm.appendChild(elm.getOwnerDocument().createTextNode(text));
    } else if (node.getNodeType() == Node.TEXT_NODE) {
        if (text == null)
            node.getParentNode().removeChild(node);
        else/*from  ww  w . j ava  2 s .co  m*/
            node.setNodeValue(text);
    } else if (text != null) {
        Text textNode = node.getOwnerDocument().createTextNode(text);
        elm.insertBefore(textNode, elm.getFirstChild());
    }
}

From source file:Main.java

public static Element appendSingleValElement(Document owner, Element appendElement, String newElemName,
        String newElemVal) {/*w  ww  . j a v a 2  s  .  c o  m*/
    Element newElem;
    newElem = owner.createElement(newElemName);
    if (newElemVal != null) {
        Text value = owner.createTextNode(newElemVal);
        newElem.appendChild(value);
    }
    appendElement.appendChild(newElem);
    return (newElem);
}