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:eu.semaine.util.XMLTool.java

/**
 * Create a child element with the given name and namespace, and append it below node.
 * @param node//from w  w  w .  ja v a2s  .  com
 * @param childName
 * @param childNamespace the namespace of the child, or null if no namespace is desired.
 * @return the child element
 */
public static Element appendChildElement(Node node, String childName, String childNamespace) {
    if (node == null)
        throw new NullPointerException("Received null node");
    if (childName == null)
        throw new NullPointerException("Received null childName");
    return (Element) node.appendChild(createElement(node.getOwnerDocument(), childName, childNamespace));
}

From source file:DomUtils.java

/**
 * Append the nodes from the supplied list to the supplied node. 
 * @param node Node to be appended to.//from w w w .  j av  a  2  s  .co m
 * @param nodes List of nodes to append.
 */
public static void appendList(Node node, List nodes) {

    int nodeCount = nodes.size();

    for (int i = 0; i < nodeCount; i++) {
        node.appendChild((Node) nodes.get(i));
    }
}

From source file:Main.java

/**
 * /*from   w ww.ja  va  2s  .c om*/
 * 
 * @param node
 *            Node object
 * @param childName
 *            Name of child node looking for
 * @param createNode
 *            If true, appends a child node with given name when not found
 * @return Node Child Node
 * @throws IllegalArgumentException
 *             for Invalid input
 */
public static Node getChildNodeByName(final Node node, final String childName, final boolean createNode)
        throws IllegalArgumentException {
    // Validate node
    if (node == null) {
        throw new IllegalArgumentException("Node cannot " + "be null in XmlUtils.getChildNodebyName method");
    }

    // Validate child name
    if (childName == null) {
        throw new IllegalArgumentException(
                "Child name cannot" + " be null in XmlUtils.getChildNodebyName method");
    }

    final NodeList childList = node.getChildNodes();
    if (childList != null) {
        for (int childIndex = 0; childIndex < childList.getLength(); childIndex++) {
            if (childName.equals(childList.item(childIndex).getNodeName())) {
                return childList.item(childIndex);
            }
        }
    }

    if (createNode) {
        final Node newNode = node.getOwnerDocument().createElement(childName);
        node.appendChild(newNode);
        return newNode;
    } else {
        return null;
    }
}

From source file:com.photon.phresco.framework.commons.QualityUtil.java

private static void appendTypeProp(Document document, Node parentProp, String tag, String nameAttr,
        String textContent) {/* w w  w.j a  v a 2s.  com*/
    Node typeProp = document.createElement(tag);
    NamedNodeMap attributes = typeProp.getAttributes();
    attributes.setNamedItem(createAttribute(document, "name", nameAttr));
    typeProp.setTextContent(textContent);
    parentProp.appendChild(typeProp);
}

From source file:DomUtils.java

/**
 * Copy child node references from source to target.
 * @param source Source Node.//from ww w  .  jav a2 s  . com
 * @param target Target Node.
 */
public static void copyChildNodes(Node source, Node target) {
    List nodeList = DomUtils.copyNodeList(source.getChildNodes());
    int childCount = nodeList.size();

    for (int i = 0; i < childCount; i++) {
        target.appendChild((Node) nodeList.get(i));
    }
}

From source file:Main.java

/**
 * Copy one node to another node./*from   w  ww. ja v a2s.  c o m*/
 * @param source source Node
 * @param dest destination Node
 * @return destination Node
 */
public static synchronized Node copyNode(Node source, Node dest) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue());
        return tn;
    }

    Node attr = null;
    NamedNodeMap attrs = source.getAttributes();

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            attr = attrs.item(i);
            ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue());
        }
    }

    Node child = null;
    NodeList list = source.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        child = list.item(i);
        if (!(child instanceof Text)) {
            Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName());

            if (child.getNodeValue() != null) {
                en.setNodeValue(child.getNodeValue());
            }

            Node n = copyNode(child, en);
            dest.appendChild(n);
        } else if (child instanceof CDATASection) {
            CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue());
            dest.appendChild(cd);
        } else {
            Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue());
            dest.appendChild(tn);
        }
    }
    return dest;
}

From source file:Main.java

public static final Node copyElement(Node e) throws UnsupportedDataTypeException {
    Node result = null;

    switch (e.getNodeType()) {
    case Node.ELEMENT_NODE:
        result = document.createElement(e.getNodeName());

        for (int i = 0; i < e.getAttributes().getLength(); i++) {
            Attr attr = document.createAttribute(e.getAttributes().item(i).getNodeName());
            attr.setNodeValue(e.getAttributes().item(i).getNodeValue());
            result.getAttributes().setNamedItem(attr);
        }/*from w  w w .  j a  va  2s . c om*/
        break;

    case Node.CDATA_SECTION_NODE:
        result = document.createCDATASection(((CDATASection) e).getData());
        break;
    case Node.TEXT_NODE:
        if (((Text) e).getTextContent().replaceAll("\t", "").trim() != "")
            result = document.createTextNode(((Text) e).getTextContent().replaceAll("\t", "").trim());
        break;

    default:
        throw new UnsupportedDataTypeException(new StringBuilder(e.getNodeType()).toString());
    }

    for (int i = 0; i < e.getChildNodes().getLength(); i++)
        result.appendChild(copyElement(e.getChildNodes().item(i)));

    return result;
}

From source file:com.photon.phresco.framework.commons.QualityUtil.java

public static void adaptPerformanceJmx(String jmxFileLocation, List<String> name, List<String> context,
        List<String> contextType, List<String> contextPostData, List<String> encodingType, int noOfUsers,
        int rampUpPeriod, int loopCount, Map<String, String> headersMap) throws Exception {
    File jmxFile = null;//from   ww  w. ja  v  a2 s  .  c  o  m
    File jmxDir = new File(jmxFileLocation + "/tests");
    if (jmxDir.isDirectory()) {
        FilenameFilter filter = new FileListFilter("", "jmx");
        File[] jmxFiles = jmxDir.listFiles(filter);
        jmxFile = jmxFiles[0];
    }

    Document document = com.photon.phresco.framework.impl.util.FrameworkUtil.getDocument(jmxFile);
    appendThreadProperties(document, noOfUsers, rampUpPeriod, loopCount);
    NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
            "jmeterTestPlan/hashTree/hashTree/hashTree/HTTPSamplerProxy");
    if (nodelist != null && nodelist.getLength() > 0) {
        NodeList headerManagerNodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
                "jmeterTestPlan/hashTree/hashTree/hashTree/HeaderManager");

        Node hashTree = nodelist.item(0).getParentNode();
        hashTree = removeAllChilds(hashTree);
        hashTree.setTextContent(null);

        if (headerManagerNodelist != null && headerManagerNodelist.getLength() > 0) {
            for (int i = 0; i < headerManagerNodelist.getLength(); i++) {
                hashTree.appendChild(headerManagerNodelist.item(i));
            }
            hashTree.appendChild(document.createElement("hashTree"));
        }

        if (MapUtils.isNotEmpty(headersMap)) {
            NodeList headerMngrNodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
                    "jmeterTestPlan/hashTree/hashTree/hashTree/HeaderManager/collectionProp");
            if (headerMngrNodelist != null && headerMngrNodelist.getLength() > 0) {
                createHeaderElementProp(document, headersMap, headerMngrNodelist.item(0));
            } else {
                Node appendHeaderManager = appendHeaderManager(document, headersMap);
                hashTree.appendChild(appendHeaderManager);
                hashTree.appendChild(document.createElement("hashTree"));
            }
        }

        for (int j = 0; j < name.size(); j++) {
            Node appendHttpSamplerProxy = appendHttpSamplerProxy(document, hashTree, name.get(j),
                    "${context}/" + context.get(j), contextType.get(j), contextPostData.get(j),
                    encodingType.get(j));
            hashTree.appendChild(appendHttpSamplerProxy);
            hashTree.appendChild(document.createElement("hashTree"));
        }
    }
    saveDocument(jmxFile, document);
}

From source file:com.photon.phresco.framework.commons.QualityUtil.java

public static void adaptDBPerformanceJmx(String jmxFileLocation, List<String> name, String dataSource,
        List<String> queryType, List<String> query, int noOfUsers, int rampUpPeriod, int loopCount,
        String dbUrl, String driver, String userName, String passWord) throws Exception {
    File jmxFile = null;//  w  ww . jav a2s  . c o  m
    File jmxDir = new File(jmxFileLocation + "/tests");
    if (jmxDir.isDirectory()) {
        FilenameFilter filter = new FileListFilter("", "jmx");
        File[] jmxFiles = jmxDir.listFiles(filter);
        jmxFile = jmxFiles[0];
    }

    Document document = com.photon.phresco.framework.impl.util.FrameworkUtil.getDocument(jmxFile);
    appendThreadProperties(document, noOfUsers, rampUpPeriod, loopCount);
    appendJdbcDataSrc(document, dataSource, dbUrl, driver, userName, passWord);
    NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
            "jmeterTestPlan/hashTree/hashTree/hashTree/JDBCSampler");
    if (nodelist != null && nodelist.getLength() > 0) {
        Node hashTree = nodelist.item(0).getParentNode();
        hashTree = removeAllChilds(hashTree);
        hashTree.setTextContent(null);

        for (int j = 0; j < name.size(); j++) {
            Node appendJdbcSampler = appendJdbcSampler(document, hashTree, name.get(j), dataSource,
                    queryType.get(j), query.get(j));
            hashTree.appendChild(appendJdbcSampler);
            hashTree.appendChild(document.createElement("hashTree"));
        }
    }
    saveDocument(jmxFile, document);
}

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * /*from w  w  w  .j  a  va 2  s  .c om*/
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}