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:Utils.java

public static Element createNewContainer(Document document, Element parent, String childElement) {
    Element child = (Element) document.createElement(childElement);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 * Appends a text node with the given information to the specified DOM
 * element./*from   w  w w  .  j  ava  2 s.  c  om*/
 */
public static Text createText(final Element el, final String info) {
    final Text text = el.getOwnerDocument().createTextNode(info);
    el.appendChild(text);
    return text;
}

From source file:Main.java

/**
 * add single element to a node. it doesn't check if an element exists with the same name.
 * @param contextNode is the node to which the element is added
 * @param elmName is the new element name, not XPath.
 * @param elmValue is the text value of the element.
 * @return the new Element/*from www . j  a v  a 2  s .c  om*/
 */
public static Element addSingleElement(Node contextNode, String elmName, String elmValue) throws Exception {
    Element elm = addSingleElement(contextNode, elmName);
    if (elmValue != null)
        elm.appendChild(contextNode.getOwnerDocument().createTextNode(elmValue));
    return (elm);
}

From source file:Main.java

public static Element newElement(Element elParent, String tagName) {
    Element element = elParent.getOwnerDocument().createElement(tagName);
    return (Element) (elParent.appendChild(element));
}

From source file:Main.java

public static void addHook(String hookName, String interfaceName, String identity, boolean superOverride,
        String superOverrideClass) {
    try {//from   w w  w  . jav a 2 s.  c  om
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBldr = dFactory.newDocumentBuilder();
        Document doc = dBldr.parse(new File("hooks.xml"));

        Element hooks = doc.getDocumentElement();

        Element hook = doc.createElement("hook");
        hook.setAttribute("name", hookName);

        Element eIdentity = doc.createElement("identity");
        eIdentity.appendChild(doc.createTextNode(identity));
        hook.appendChild(eIdentity);

        Element eInterface = doc.createElement("interface");
        eInterface.appendChild(doc.createTextNode(interfaceName));
        hook.appendChild(eInterface);

        Element eSuper = doc.createElement("super");
        if (superOverride) {
            eSuper.appendChild(doc.createTextNode(superOverrideClass));
        } else {
            eSuper.appendChild(doc.createTextNode("null"));
        }
        hook.appendChild(eSuper);

        hooks.insertBefore(hook, hooks.getLastChild());
        write(doc);
    } catch (SAXException | IOException | ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * adds a child at the first place /*from  w  w  w .j  a v a  2  s  . c  o m*/
 * @param parent
 * @param child
 */
public static void prependChild(Element parent, Element child) {
    Node first = parent.getFirstChild();
    if (first == null)
        parent.appendChild(child);
    else {
        parent.insertBefore(child, first);
    }
}

From source file:Main.java

/**
 * returns XML representation of a string, with specified XML node name
 * //from   w  w  w  .  ja  va 2s . c om
 * @param doc XML Document in which to build the XML element
 * @param namespaceURI the namespace URI
 * @param qname top level qualified XML node name in XML representation
 * @param str the string
 * @return XML representation of the string, with specified XML node name
 **/
public final static Element strToXML(final Document doc, final String namespaceURI, final String qname,
        final String str) {
    if (str == null /*|| str.length() == 0*/) {
        return null;
    }
    final Element xml = doc.createElementNS(namespaceURI, qname);
    xml.appendChild(doc.createTextNode(str));

    return xml;
}

From source file:Main.java

/**
 * Appends the child element to the parent element.
 *
 * @param parent the parent element/*ww  w. j  a  va  2s.c  om*/
 * @param tagName the child element name
 * @return the child element added to the parent element
 */
public static Element appendElement(Element parent, String tagName) {
    Element child = parent.getOwnerDocument().createElement(tagName);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 * Creates and appends a child node with a tag and value
 * @param ele = parent node/*from  ww  w . j a  va  2s . c o m*/
 * @param tag = tag of new child node
 * @param value = value of new child node (i.e. <CODE> Node.setNodeValue(contents) </CODE>)
 * @param doc = element source
 */
public static void appendChildNode(Document doc, Element ele, String tag, Object value) {
    Node n = createElement(doc, tag, value, null);
    ele.appendChild(n);
}

From source file:Main.java

/**
 * Creates a new element, with the given text inside that element.
 * /*from  w  w  w.ja  v a  2  s  . c o  m*/
 * @param document the document into which the element is added.
 * @param parentElement The parent element of the element-to-be-added.
 * @param newElementName name of new element (tag-name).
 * @param textString the text to be written inside the newly created element.
 * @return The new element.
 */
public static Element addElementWithText(Document document, Element parentElement, String newElementName,
        String textString) {
    Element newElement = document.createElement(newElementName);
    Text text = document.createTextNode(textString);
    newElement.appendChild(text);
    parentElement.appendChild(newElement);
    return newElement;
}