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

private static void appendChildren(Node node, StringBuffer sb) {
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            sb.append(convertDOMToString(children.item(i)));
        }//  ww  w. j  a  v  a  2 s. c om
    }
}

From source file:Main.java

public static String getTextForNode(Node node) {
    StringBuilder sb = new StringBuilder();

    NodeList children = node.getChildNodes();
    if (children.getLength() == 0)
        return null;

    for (int i = 0; i < children.getLength(); ++i) {
        Node n = children.item(i);

        if (n instanceof Text)
            sb.append(n.getNodeValue());
        else if (n instanceof EntityReference) {
            String s = getTextForNode(n);
            if (s == null)
                return null;
            else// w w w  .  j av a  2 s  .  c om
                sb.append(s);
        } else
            return null;
    }

    return sb.toString();
}

From source file:Main.java

public static Node findWithAttrubute(Node root, String name, String attributeName) {
    final NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        final Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.ELEMENT_NODE) {
            if (subnode.getNodeName().equals(name)) {
                if (subnode.getAttributes().getNamedItem(attributeName) != null) {
                    return subnode;
                }/*  ww  w  .ja  v a2  s.co m*/
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Search all child nodes of the given for the first element that has the
 * specified tag name.//from   w  w w. j  a va 2s.  c o  m
 *
 * @param aStartNode
 *        The parent element to be searched. May not be <code>null</code>.
 * @param sName
 *        The tag name to search.
 * @return <code>null</code> if the parent element has no such child element.
 */
@Nullable
public static Element getFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName) {
    final NodeList aNodeList = aStartNode.getChildNodes();
    final int nLen = aNodeList.getLength();
    for (int i = 0; i < nLen; ++i) {
        final Node aNode = aNodeList.item(i);
        if (aNode.getNodeType() == Node.ELEMENT_NODE) {
            final Element aElement = (Element) aNode;
            if (aElement.getTagName().equals(sName))
                return aElement;
        }
    }
    return null;
}

From source file:Main.java

public static int outputNode(Node outputNode, PrintWriter outputWriter, int curPos) {
    NodeList nodes = outputNode.getChildNodes();
    int curNodeNum;
    if (outputNode.getNodeType() == Node.TEXT_NODE) {
        outputWriter.print(outputNode.getNodeValue());
    } else {/*from   w ww. j  a  v  a  2s .  com*/
        if (outputNode.getNodeName().equals("p")) {
            outputWriter.println();
        }

        for (curNodeNum = 0; curNodeNum < nodes.getLength(); curNodeNum++) {
            Node curNode = nodes.item(curNodeNum);
            curPos = outputNode(curNode, outputWriter, curPos);
        }

    }
    return (curPos);
}

From source file:Main.java

/**
 * Get sub tag content.//  w  w w. ja va  2 s. c o m
 * @param node to process
 * @param tagName the tag name
 * @return the child content
 */
public static String getSubTagValue(Node node, String tagName) {
    if (node == null) {
        return null;
    }
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n instanceof Element) {
            if (((Element) n).getTagName().equals(tagName)) {
                Node nn = n.getFirstChild();
                if (nn instanceof Text) {
                    return ((Text) nn).getData();
                }
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Node> getDescendents(Node node, String nodeName) throws IOException {
    List<Node> childList = new ArrayList<Node>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(nodeName)) {
            childList.add(child);// ww w .j av  a 2  s.  c o m
        }
    }
    return childList;
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node == null)
        return null;
    StringBuffer buff = new StringBuffer();
    for (int c = 0; c < node.getChildNodes().getLength(); c++) {
        Node cn = node.getChildNodes().item(c);
        if (cn.getNodeType() == Node.TEXT_NODE || cn.getNodeType() == Node.CDATA_SECTION_NODE) {
            buff.append(cn.getNodeValue());
        }//  w ww. j  a  va 2  s. c om
    }
    return buff.toString().trim();

}

From source file:Main.java

public static List<String> getPropertiesFromXML(Node propNode) {
    ArrayList<String> properties;
    properties = new ArrayList<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.add(namespace + ":" + nodeName);
        }/*from  w  w w  . j  a v a 2s  .  co m*/
    }
    return properties;
}

From source file:Main.java

/**
 * Return a Node object with the name is nodeName
 * @param parent parent Node which contains the node nodeName
 * @param nodeName name of the node//from   w  w  w  . j a  v a2s  .  c  o m
 * @return a Node object with the name is nodeName<br>
 * if the node parent doesn't have child nodes, null is return.
 */
public static Node getNode(Node parent, String nodeName) {
    if (parent.hasChildNodes()) {
        for (int i = 0; i < parent.getChildNodes().getLength(); i++) {
            if (parent.getChildNodes().item(i).getNodeName().equalsIgnoreCase(nodeName)) {
                return parent.getChildNodes().item(i);
            }
        }
    }
    return null;
}