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:com.iggroup.oss.restdoclet.plugin.util.XmlUtils.java

/**
 * Returns the <i>first</i> child-element of a XML node with a particular
 * name.//from  www.  j av  a  2 s .c  om
 * 
 * @param node the XML node.
 * @param name the name of the child-element.
 * @return the child-element or <code>null</code> if no child with the name
 *         was found.
 */
public static Element child(final Node node, final String name) {
    Element element = null;

    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        if (node.getChildNodes().item(i).getNodeType() == ELEMENT_NODE) {
            final Element child = (Element) node.getChildNodes().item(i);

            if (StringUtils.equals(name, child.getNodeName())) {
                element = child;
                break;
            }
        }
    }
    return element;
}

From source file:Main.java

/**gets string associated with a node
 * @param node node to get string for//from  w  w  w.  j  a v  a2  s.co  m
 * @param string associated with the input node*/
static public String getAssociatedString(Node node) {
    // Has to be a real Node
    if (node == null)
        return null;

    // Must have one and only one string associted with it
    NodeList children = node.getChildNodes();
    if (children.getLength() > 1) {
        System.out.println("XMLUtils: Node has more than one child");
        return null;
    }
    Node firstChild = children.item(0);
    if (firstChild.getNodeType() != Node.TEXT_NODE) {
        System.out.println("XMLUtils: Node is not a text node");
        System.out.println(
                "XMLUtils: Node = " + firstChild.getNodeName() + ", Parent Node = " + node.getNodeName());
        return null;
    }
    String stringToReturn = firstChild.getNodeValue().trim();
    if (stringToReturn.equals("")) {
        System.out.println("XMLUtils: Trimmed string is empty");
        return null;
    } else
        return stringToReturn;
}

From source file:org.owasp.benchmark.tools.BenchmarkCrawler.java

public static Node getNamedChild(String name, Node parent) {
    NodeList children = parent.getChildNodes();
    return getNamedNode(name, children);
}

From source file:org.owasp.benchmark.tools.BenchmarkCrawler.java

public static List<Node> getNamedChildren(String name, Node parent) {
    NodeList children = parent.getChildNodes();
    return getNamedNodes(name, children);
}

From source file:com.threewks.thundr.googleapis.cloudstorage.GoogleCloudStorageServiceImpl.java

static Node findNamedChild(Node node, String name) {
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (name.equalsIgnoreCase(child.getNodeName())) {
            return child;
        }/*from   w  w w .  j av a 2 s  . co m*/
    }
    return null;
}

From source file:Main.java

public static List<Element> findElementsByAttribute(Node node, String tagName, String attrName,
        String attrValue) {//from  w  w  w .  java 2 s  .c  om
    List<Element> result = new ArrayList<Element>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return result;
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Element element = checkIfElement(nodeList.item(i), tagName);
        if (element != null && element.getAttribute(attrName).equals(attrValue)) {
            result.add(element);
        }
    }
    return result;
}

From source file:Main.java

public static String nodeToString(Node n) {
    StringBuilder sb = new StringBuilder();
    if (n instanceof Text)
        return n.getNodeValue().trim();
    else {/* w w w . j av a  2 s  . c  o m*/
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node child = nl.item(i);
            sb.append(nodeToString(child));
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if ("td".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\t");
            } else if ("th".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\t");
            } else if ("tr".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\n");
            } else if ("br".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\n");
            }
        }
        return sb.toString();
    }
}

From source file:Main.java

/**
 * returns contents of first TextNode defined within an XML Node
 * /*from   w  w w . j av  a2  s .com*/
 * @param xmlNode parsed XML Node in which to look for a TextNode
 * @return text character string value of text node
 **/
public static String xmlFindTextNode(final Node xmlNode) {
    if (xmlNode == null) {
        return null;
    }

    final NodeList children = xmlNode.getChildNodes();
    final int childrenCnt = size(children);
    for (int j = 0; j < childrenCnt; j++) {
        final Node childNode = children.item(j);
        if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
            return childNode.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get the value of an element. If the node is a Document, use the
 * document element as the element node.
 * The value of an element node is the sum of all the element's
 * first generation child text nodes. Note that this is not what you
 * would get from a mixed element in an XSL program.
 * @param node the node.//from   w w w  . jav a  2 s .co m
 * @return the value of the element, or an empty string if the
 * node is not an element.
 */
public static String getElementValue(Node node) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return "";
    NodeList nodeList = node.getChildNodes();
    String value = "";
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE)
            value += n.getNodeValue();
    }
    return value;
}

From source file:com.amalto.core.initdb.InitDBUtil.java

private static void parseInitMap(InputStream in, DocumentBuilder builder, Map<String, List<String>> initMap)
        throws Exception {
    Document doc = builder.parse(in);
    NodeList nodelist = doc.getElementsByTagName("item"); //$NON-NLS-1$
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node node = nodelist.item(i);
        NodeList list = node.getChildNodes();
        String name = null;//from  w  ww . j  a va2 s.  c  o m
        for (int j = 0; j < list.getLength(); j++) {
            Node n = list.item(j);
            if (n instanceof Element) {
                if ("name".equals(n.getNodeName())) { //$NON-NLS-1$
                    name = n.getTextContent();
                    if (initMap.get(name) == null) {
                        initMap.put(name, new ArrayList<String>());
                    }
                }
                if ("list".equals(n.getNodeName())) { //$NON-NLS-1$
                    if (n.getTextContent() == null || n.getTextContent().trim().length() == 0) {
                        continue;
                    }
                    List<String> lists = initMap.get(name);
                    String[] arr = n.getTextContent().split(";"); //$NON-NLS-1$
                    lists.addAll(Arrays.asList(arr));
                }
            }
        }
    }
}