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

/**
 * This method parse an Attributes tag, DTD for Attribute is as follows.
 * /*from  w ww.jav  a 2  s .c  o  m*/
 * <pre>
 *  &lt; !-- This DTD defines the DPro Attribute tag.
 *    Unique Declaration name for DOCTYPE tag:
 *    &quot;Directory Pro 5.0 Attributes DTD&quot;
 *  --&gt;
 *  &lt; !ELEMENT Attributes (Attribute)+&gt;
 *  &lt; !ELEMENT Attribute EMPTY&gt;
 *  &lt; !ATTLIST Attribute
 *       name    NMTOKEN         #REQUIRED
 *  &gt;
 * </pre>
 * 
 * @param n
 *            Node
 * @return Set Set of the attribute names
 */
public static Set parseAttributesTag(Node n) {
    // get Attribute node list
    NodeList attributes = n.getChildNodes();
    final int numAttributes = attributes.getLength();

    if (numAttributes == 0) {
        return null;
    }

    Set set = new HashSet();
    for (int l = 0; l < numAttributes; l++) {
        // get next attribute
        Node attr = attributes.item(l);
        if ((attr.getNodeType() != Node.ELEMENT_NODE) && !attr.getNodeName().equals("Attribute")) {
            // need error handling
            continue;
        }

        set.add(((Element) attr).getAttribute("name"));
    }
    return set;
}

From source file:Main.java

public static double getNodeValue(final Node parentNode, final String strNodeName, final double dDefaultValue) {
    final String strValue = getNodeValue(parentNode.getChildNodes(), strNodeName, null);

    if (strValue != null) {
        return Double.parseDouble(strValue);
    }//from www  .j  a  va  2 s.co m

    return dDefaultValue;
}

From source file:Main.java

/**
 * Returns a child node that has the given node name and give attribute name
 * and value.//from   www . java  2  s.co m
 */
public static Node getNamedChildNode(Node parentNode, String childNodeName, String attrName, String attrValue) {
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (childNodeName.equalsIgnoreCase(node.getNodeName())) {
            if (getNodeAttributeValue(node, attrName).equalsIgnoreCase(attrValue))
                return (node);
        }
    }
    return (null);
}

From source file:Main.java

public static Map<String, Object> getPropertiesWithValuesFromXML(Node propNode) {
    Map<String, Object> propertiesWithValues = new HashMap<String, Object>();

    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
            String fqn = namespace + ":" + nodeName;
            propertiesWithValues.put(fqn, nodeValue(currentNode));

        }/*ww  w . j  a  va2s  . c  o m*/
    }
    return propertiesWithValues;
}

From source file:Main.java

/**
 * This method parse an Attributes tag, DTD for Attribute is as follows.
 * /*from  w  ww .  ja va 2  s  . c  o  m*/
 * <pre>
 *  &lt; !-- This DTD defines the DPro Attribute tag.
 *    Unique Declaration name for DOCTYPE tag:
 *    &quot;Directory Pro 5.0 Attributes DTD&quot;
 *  --&gt;
 *  &lt; !ELEMENT Attributes (Attribute)+&gt;
 *  &lt; !ELEMENT Attribute EMPTY&gt;
 *  &lt; !ATTLIST Attribute
 *       name    NMTOKEN         #REQUIRED
 *  &gt;
 * </pre>
 * 
 * @param n
 *            Node
 * @return Set Set of the attribute names
 */
public static Set parseAttributesTag(Node n) {
    // get Attribute node list
    NodeList attributes = n.getChildNodes();
    final int numAttributes = attributes.getLength();

    if (numAttributes == 0) {
        return null;
    }

    Set set = new HashSet();
    for (int l = 0; l < numAttributes; l++) {
        // get next attribute
        Node attr = attributes.item(l);
        if ((attr.getNodeType() != Node.ELEMENT_NODE) && !attr.getNodeName().equals("Attribute")) {
            // need error handling
            continue;
        }

        String attrName = ((Element) attr).getAttribute("name");
        set.add(attrName);
    }
    return set;
}

From source file:Main.java

public static String getTextContent(Node e) {
    if (e == null || e.getNodeType() != Node.ELEMENT_NODE) {
        return null;
    }/*  w  w w  .  jav  a 2s. co  m*/

    NodeList nodes = e.getChildNodes();
    StringBuilder text = new StringBuilder();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = e.getFirstChild();

        if (node != null && node.getNodeType() == Node.TEXT_NODE) {
            String s = node.getNodeValue();
            if (s != null) {
                text.append(s);
            }
        }
    }

    if (text.length() > 0) {
        return text.toString();
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Returns all direct simple text value subnodes and their values for a dataNode
 *
 * Example XML: <dataNode> <a>1</a> <b>2</b> <c>3</c> </dataNode> Returns: a=1, b=2, c=3.
 *
 * @param dataNode//from  ww  w.  j  a  v  a2 s .  c  o  m
 *            the data node
 * @return the simple values of node
 */
public static Map<String, String> getSimpleValuesOfNode(Node dataNode) {
    Map<String, String> returnMap = new HashMap<String, String>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.TEXT_NODE
                && node.getChildNodes().getLength() == 1) {
            returnMap.put(node.getNodeName(), node.getFirstChild().getNodeValue());
        }
    }

    return returnMap;
}

From source file:Main.java

private static void walkNodes(Node nodeIn, HashSet<String> hElements) {
    if (nodeIn == null)
        return;// www .j  a v  a2s.  c  om
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            String sNodeName = n.getNodeName();
            if (!hElements.contains(sNodeName))
                hElements.add(sNodeName);
            walkNodes(n, hElements);
        }
    }
}

From source file:Main.java

public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception {
    Document doc = initializeXML(userMgtXML);
    NodeList configNodeList = doc.getElementsByTagName("Configuration");
    Node configNode = configNodeList.item(0); //get the 0 index, since only one available

    NodeList nodeList = configNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equalsIgnoreCase("Property")) {
                if (node.hasAttributes()) {
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    Node attr = namedNodeMap.getNamedItem("name");
                    if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) {
                        node.setTextContent(jndiConfigNameUserDB);
                    }/* w  ww.  j  av  a2  s. com*/
                }
            }
        }
    }
    finalizeXML(userMgtXML, doc, 4);
}

From source file:Main.java

public static Element findElementWithAttributes(Node node, String tagName, Collection<String> attrs) {
    Element result = null;/*from  ww w. j  ava 2s.c om*/
    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) {
            boolean match = true;
            for (String attrName : attrs) {
                if (!element.hasAttribute(attrName)) {
                    match = false;
                    break;
                }
            }
            if (match) {
                result = element;
                break;
            }
        }
    }
    return result;
}