Example usage for org.w3c.dom Node getChildNodes

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

Introduction

In this page you can find the example usage for org.w3c.dom Node getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

static public String getNodeAttr(String tagName, String attrName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (data.getNodeName().equalsIgnoreCase(attrName)) {
                        return data.getNodeValue();
                    }//from w  w w.  j  av  a2 s  .c o  m
                }
            }
        }
    }

    return "";
}

From source file:Main.java

/**
 * Get all child nodes of parent that implement/subclass the given type.
 * /*from   w  w w  .  ja v a2 s.  c om*/
 * @param parent
 *            Parent to search
 * @param nodeType
 *            Type of child to search for
 * @return Array of children of parent that conform to the given nodeType
 */
public static <N extends Node> N[] getChildrenImplementing(Node parent, Class<N> nodeType) {
    if (parent == null) {
        return null;
    }
    NodeList children = parent.getChildNodes();
    if (children == null) {
        return null;
    }
    int count = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (nodeType.isAssignableFrom(node.getClass())) {
            count++;
        }
    }
    @SuppressWarnings("unchecked")
    N[] array = (N[]) Array.newInstance(nodeType, count);
    for (int i = 0, pos = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (nodeType.isAssignableFrom(node.getClass())) {
            @SuppressWarnings("unchecked")
            N n = (N) node;
            Array.set(array, pos++, n);
        }
    }
    return array;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes and collects and returns
 * their values./*w ww . j a  v a2 s .c o m*/
 *
 * @param node     The parent node
 * @param elemName The matching child node element name
 * @return List of node values
 */
public static Enumeration getChildrenNodeValues(Node node, String elemName) {
    Vector vect = new Vector();
    NodeList nl = node.getChildNodes();
    int n = nl.getLength();
    for (int i = 0; i < n; i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(elemName)) {
            Node child = nd.getFirstChild();
            if (child == null) {
                vect.add("");
            } else {
                vect.add(child.getNodeValue());
            }
        }
    }
    return vect.elements();
}

From source file:Main.java

public static void nodeSubUpdate(Element root, String table, String queryType, String queryValue) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    NodeList nl = find.getChildNodes();
    int numnodes = nl.getLength();

    System.out.println("length : " + numnodes);

    Node replNode = null;//from www .  j  av a2  s  .c  o m

    for (int i = 0; i < numnodes; i++) {
        Node n = nl.item(i);
        String name = n.getNodeName();

        if (name.equals(queryType)) {
            replNode = n;
            System.out.println("Finding Node : " + replNode.getNodeName());

            break;
        }
    }

    Element type = doc.createElement(queryType);
    Text value = doc.createTextNode(queryValue);

    type.appendChild(value);

    find.replaceChild(type, replNode);

    print(doc);
}

From source file:Main.java

/**
 * This method will get the child nodes of a given node and return them in an ArrayList
 * instead of a NodeList. NodeList does not implement Iterator, which is needed to make
 * the compare code safer.//from   w  ww  .j a v a2s . c  o  m
 * @param parentNode
 * @return an ArrayList of childNodes if the parentNode has childNodes. If there are no children an empty ArrayList is returned.
 */
private static ArrayList<Node> getChildNodes(Node parentNode) {
    ArrayList<Node> childNodesArray = new ArrayList<Node>();
    if (null != parentNode) {
        NodeList childNodes = parentNode.getChildNodes();
        if (null != childNodes && childNodes.getLength() > 0) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node child = childNodes.item(i);
                if (null != child) {
                    childNodesArray.add(child);
                }
            }
        }
    }
    return childNodesArray;
}

From source file:Main.java

/**
 * Gets the given node's children of the given type.
 *
 * @param node parent.//from w w w  .j av a 2  s . com
 * @param nodetype searched child type.
 * @return children.
 */
public static List<Node> getChildNodes(final Node node, final short nodetype) {
    final List<Node> result = new ArrayList<Node>();

    final NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child.getNodeType() == nodetype) {
            result.add(child);
        }
    }

    return result;
}

From source file:Main.java

public static void setPrefixRecursive(final Node node, final String prefix) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        node.setPrefix(prefix);//from w w  w  .java 2s  .  c o m
    }

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        setPrefixRecursive(list.item(i), prefix);
    }
}

From source file:net.mindengine.oculus.frontend.domain.document.testcase.Testcase.java

public static Testcase parse(String xmlData) throws Exception {
    Testcase testcase = new Testcase();

    try {/*from w ww .j a v a 2s . c  o  m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder builder = dbf.newDocumentBuilder();

        Reader reader = new CharArrayReader(xmlData.toCharArray());
        org.w3c.dom.Document doc = builder.parse(new org.xml.sax.InputSource(reader));

        Node root = doc.getDocumentElement();

        NodeList nodeList = root.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if ("steps".equals(node.getNodeName())) {
                NodeList stepsList = node.getChildNodes();
                for (int j = 0; j < stepsList.getLength(); j++) {
                    Node n = stepsList.item(j);

                    TestcaseStep step = new TestcaseStep();
                    step.setAction(XmlUtils.getChildNodeText(n, "action"));
                    step.setExpected(XmlUtils.getChildNodeText(n, "expected"));
                    step.setComment(XmlUtils.getChildNodeText(n, "comment"));
                    testcase.steps.add(step);
                }
            }
        }
    } catch (Exception e) {
    }

    return testcase;
}

From source file:Utils.java

/**
 * <p>Returns an array of text values of a child element. Returns
 * <code>null</code> if there is no child element found.</p>
 *
 * @param parent parent element// w  w  w .j av  a 2 s.  c o m
 * @param name name of the child element
 * @return text value
 */
public static String[] getChildElementTextArr(Element parent, String name) {
    // Get all the elements
    List children = getChildElementsByName(parent, name);

    String str[] = new String[children.size()];

    for (int i = 0; i < children.size(); i++) {
        Node child = (Node) children.get(i);

        StringBuffer buf = new StringBuffer();

        NodeList nodes = child.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
            Node node = nodes.item(j);
            if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
                Text text = (Text) node;
                buf.append(text.getData().trim());
            }
        }

        str[i] = buf.toString();
    }

    return str;
}

From source file:com.autentia.tnt.xml.UtilitiesXML.java

/**
 * Devuelve el texto de un nodo: <tag>TEXTO</tag>
 * @param n// w  w w  .  jav  a 2  s  .  c o m
 * @return
 */
public static String getTexto(Node n) {
    NodeList nl = n.getChildNodes();
    Node act = null;

    for (int i = 0; i < nl.getLength(); i++) {
        act = nl.item(i);
        if (act == null)
            return null;
        if ((act.getNodeType() == Node.CDATA_SECTION_NODE) || (act.getNodeType() == Node.TEXT_NODE))
            return act.getNodeValue();
    }
    return "";
}