Example usage for org.w3c.dom Node appendChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Node 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

public static Element addElement(Node parent, String name) {
    Element node;//from w  w  w .ja va 2 s  .  co  m
    if (parent.getOwnerDocument() != null) {
        node = parent.getOwnerDocument().createElement(name);
    } else if (parent instanceof Document) {
        node = ((Document) parent).createElement(name);
    } else {
        return null;
    }
    parent.appendChild(node);
    return node;
}

From source file:Main.java

public static void addNewNode(File xmlfile, String nodeXpath, String elementName, String attributeName,
        String attributeValue) throws Exception {
    // Get the Document object.
    //logger.debug("Add node method, adding a new node in xml file " + xmlfile.toString());
    Document doc = getDocument(xmlfile);
    // get the Node Object for the xpath.
    Node node = getNodeObject(nodeXpath, doc);

    // Create a New Element
    Element element = doc.createElement(elementName);
    // Set the Attributes in to the Element
    element.setAttribute(attributeName, attributeValue);
    // Append The Element as a child in side the Parent Node
    node.appendChild(element);

    wirteXmlFile(doc, xmlfile);/*from  ww  w .j  a  v  a2  s.c om*/
    //logger.debug("New node is added to the xml file");
}

From source file:Main.java

/**
 * Adds a new empty {@link Element} to the given parent {@link Element}.
 * /*from   w ww.  ja  v a  2 s .  co  m*/
 * @param doc the document to add to.
 * @param parent the parent element. If {@code null} then the new child
 * will be added directly to the document.  
 * @param name the name of the new element.
 * @return the created element.
 */
public static Element addElement(Document doc, Node parent, String name) {
    if (doc == null) {
        doc = parent.getOwnerDocument();
    }
    final Element elem = doc.createElement(name);
    if (parent == null) {
        parent = doc;
    }
    parent.appendChild(elem);
    return elem;
}

From source file:Main.java

public static Node createAndAppendNode(Node root, String path) {
    if (path == null)
        return root;

    Element node = null;/*  www .j a v a 2s  . co  m*/
    if (path.contains("/")) // check if path is complex
    {
        String[] steps = path.split("/");
        Node upper = root;
        Node lower = null;
        for (int i = 0; i < steps.length; i++) {
            // check if element exists
            lower = ifNodeExists(upper, steps[i]);
            if (lower == null) {
                lower = upper.getOwnerDocument().createElement(steps[i]);
                upper.appendChild(lower);
            }
            upper = lower;
        }
        return lower;
    }
    // simple path:
    node = root.getOwnerDocument().createElement(path);
    root.appendChild(node);
    return node;
}

From source file:Main.java

public static Node appendXMLChildFromString(Node parent, String child)
        throws ParserConfigurationException, SAXException, IOException {
    if (parent instanceof Document)
        parent = ((Document) parent).getDocumentElement();
    Document newDoc = getXMLFromString(child);
    Node newNode = parent.getOwnerDocument().importNode(newDoc.getDocumentElement(), true);
    return parent.appendChild(newNode);
}

From source file:DomUtil.java

/**
 * Set or replace the text value/*  www . j  av a 2  s  .co m*/
 */
public static void setText(Node node, String val) {
    Node chld = DomUtil.getChild(node, Node.TEXT_NODE);
    if (chld == null) {
        Node textN = node.getOwnerDocument().createTextNode(val);
        node.appendChild(textN);
        return;
    }
    // change the value
    chld.setNodeValue(val);
}

From source file:Main.java

private static Node createChildInternal(Document document, Node parent, String nodeName,
        String... attr_name_and_value) {
    Element newNode = document.createElement(nodeName);

    for (int i = 0; i < attr_name_and_value.length; i += 2) {
        String attrName = attr_name_and_value[i];
        String attrValue = attr_name_and_value[i + 1];
        newNode.setAttribute(attrName, attrValue);
    }/*from  w ww  .  ja  v  a 2 s.c  o  m*/
    parent.appendChild(newNode);
    return newNode;
}

From source file:fr.ece.epp.tools.Utils.java

public static void updateProduct(String path, String[] feature, boolean outOrno) {
    Document document = null;//from   w w  w . j  a v  a 2 s .  c om
    try {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        Element root = document.getDocumentElement();
        Node features = root.getElementsByTagName("features").item(0);
        Element basefeature = document.createElement("feature");
        basefeature.setAttribute("id", "org.eclipse.platform");
        features.appendChild(basefeature);
        for (int i = 0; i < feature.length; i++) {
            if (feature[i] != null && feature[i].trim().length() > 0) {
                Element fea = document.createElement("feature");
                if (feature[i].endsWith(".feature.group")) {
                    int count = feature[i].length() - ".featyre.group".length();
                    String id = feature[i].substring(0, count);
                    fea.setAttribute("id", id);
                } else {
                    fea.setAttribute("id", feature[i]);
                }
                features.appendChild(fea);
            }
        }
        output(root, path);
        if (outOrno) {
            output(root, null);
        }

    } catch (SAXException e) {
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    }
}

From source file:Main.java

/**
 * @param parent/* w w  w .j  a  v a 2 s.c  o m*/
 *          node to add fragment to
 * @param fragment
 *          a well formed XML fragment
 * @throws ParserConfigurationException 
 */
public static void appendXmlFragment(Node parent, String fragment)
        throws IOException, SAXException, ParserConfigurationException {

    DocumentBuilder docBuilder = getBuilder();
    Document doc = parent.getOwnerDocument();

    Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement();

    fragmentNode = doc.importNode(fragmentNode, true);

    parent.appendChild(fragmentNode);
}

From source file:Main.java

private static void formatNode(Document doc, Node parent, Node node) {
    Node txt = doc.createTextNode("\n    ");
    parent.insertBefore(txt, node);//ww w.  jav  a  2s  . c  om
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        txt = doc.createTextNode("\n      ");
        node.insertBefore(txt, children.item(i));
        i += 1;
    }
    txt = doc.createTextNode("\n    ");
    node.appendChild(txt);
}