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

/**
 * returns a XML node value./* www .ja  va 2 s .co m*/
 *
 * @param pDocument         Document XML DOM document
 * @param psTagName         String XML node name
 * @return                  String XML node value
 */
public static String getValue(Document pDocument, String psTagName) throws Exception {
    String s = null;
    try {
        NodeList elements = pDocument.getDocumentElement().getElementsByTagName(psTagName);
        Node node = elements.item(0);
        NodeList nodes = node.getChildNodes();
        //find a value whose value is non-whitespace
        for (int i = 0; i < nodes.getLength(); i++) {
            s = ((Node) nodes.item(i)).getNodeValue().trim();
            if (s.equals("") || s.equals("\r"))
                continue;
        }
    } catch (Exception ex) {
        throw new Exception(ex.getMessage());
    }
    return s;
}

From source file:Main.java

public static Collection<Node> getNodesByName(Node node, String name) {
    Collection<Node> list = new LinkedList<Node>();

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
        if (nl.item(i).getNodeName().equals(name))
            list.add(nl.item(i));/*w  w  w  . j a v  a 2s  .c o  m*/

    return list;
}

From source file:Main.java

public static Set getChildNodes(Node parentNode, String childName) {
    Set retVal = new HashSet();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeName().equalsIgnoreCase(childName)) {
            retVal.add(node);//www.ja  v a  2 s .  com
        }
    }
    return (retVal);
}

From source file:Main.java

public static Element findElementNodeByName(Node node, String name) {
    if (node == null) {
        return null;
    }//from w  w w .  ja v a  2 s .com
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null || nodeList.getLength() == 0) {
        return null;
    }

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node item = nodeList.item(i);
        if (item != null
                && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE)
                && name.equalsIgnoreCase(item.getNodeName())) {
            return (Element) item;
        } else {
            Element element = findElementNodeByName(item, name);
            if (element != null) {
                return element;
            }
        }
    }
    return null;
}

From source file:Main.java

public static void cleanText(Node node) {
    try {/*w  w w.j  a v  a  2s .  c  o  m*/
        NodeList childNodes = node.getChildNodes();
        int noChildren = childNodes.getLength();
        Node n = null;
        short type = 0;
        Vector rem = new Vector();
        for (int i = 0; i < noChildren; i++) {
            n = childNodes.item(i);
            type = n.getNodeType();
            if (type == Node.TEXT_NODE) {
                rem.add(n);
            } else if (type == Node.ELEMENT_NODE) {
                cleanText(n);
            }
        }
        for (int i = 0; i < rem.size(); i++) {
            node.removeChild((Node) rem.get(i));
        }
    } catch (Exception e) {
        //DebugUtil.debug(e);
    }
}

From source file:Main.java

public static Element findElement(Node sourceElm, String attrName, String sAttrValue) {
    if (sourceElm.hasChildNodes()) {
        NodeList nodes = sourceElm.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
            if (Node.ELEMENT_NODE == nodes.item(i).getNodeType()) {
                Element elm = findElement(nodes.item(i), attrName, sAttrValue);

                if (((Element) elm).getAttribute(attrName).equals(sAttrValue)) {
                    return (Element) elm;
                }// w w w. ja v  a 2 s  .c o  m
            }
        }
    } else {
        if (sourceElm.hasAttributes() && ((Element) sourceElm).getAttribute(attrName).equals(sAttrValue)) {
            return (Element) sourceElm;
        }
    }

    return (Element) sourceElm;
}

From source file:Main.java

public static String getAttributeInXML(org.w3c.dom.Node node, String tagName, String attributeName) {
    String output = "";
    org.w3c.dom.NodeList nodes = node.getChildNodes();
    if (!attributeName.equals("_content"))
        for (int i = 0; i < nodes.getLength(); i++) {
            if (nodes.item(i).getNodeName().equals(tagName)) {
                output = ((Element) nodes.item(i)).getAttribute(attributeName);
            }//from  w w  w . j  a  va 2s. c  o  m
        }
    else
        for (int i = 0; i < nodes.getLength(); i++) {
            if (nodes.item(i).getNodeName().equals(tagName)) {
                output = (nodes.item(i)).getTextContent();
            }
        }
    return output;
}

From source file:Main.java

/**
   Return the first children of a node with a given name.
   @return <code>null</code> if no child is found
 *///ww w  .  j  a v a  2s. c  o  m
static public Node findChild(Node node, String child_name) {
    if (node == null)
        return null;

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            return child;
    }
    return null;
}

From source file:Main.java

public static List getChildrenByTagName(Node parent, String tagName) {
    ArrayList elements = new ArrayList();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            Element elem = (Element) children.item(i);
            if (tagName.equals(elem.getTagName())) {
                elements.add(elem);//from  www .j av  a2 s .  c om
            }
        }
    }
    return elements;
}

From source file:Main.java

static public String getNodeValue(String tagName, 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.TEXT_NODE) {
                    return data.getNodeValue();
                }/*  ww w  . j av  a  2  s  .  co  m*/
            }
        }
    }
    return "";
}