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

public static Map<String, String> getConfigs(InputStream is)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document dt = db.parse(is);// w w  w  .  j a  v  a 2s . c  om
    Element element = dt.getDocumentElement();
    Map<String, String> config = new TreeMap<String, String>();

    NodeList propertyList = element.getElementsByTagName("property");
    int length = propertyList.getLength();
    for (int i = 0; i < length; i++) {
        Node property = propertyList.item(i);
        String key = property.getChildNodes().item(0).getTextContent();
        String value = property.getChildNodes().item(1).getTextContent();
        config.put(key, value);
    }
    return config;
}

From source file:Main.java

/**
 * //from www  .j  a v  a2 s .  co  m
 * @param node
 * @return String
 */
public static String getSimpleElementText(final Node node) {
    final NodeList children = node.getChildNodes();

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

        if (child instanceof Text) {
            return child.getNodeValue();
        }
    }

    return null;
}

From source file:Main.java

public static String getNodeContents(Node parentNode) {
    StringBuilder sb = new StringBuilder();

    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        String nodeValue = node.getNodeValue();
        if (nodeValue != null) {
            sb.append(nodeValue);/*from  w  ww .  j  av a2  s  .com*/
        }
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Creates a Text child, or replaces all existing children of type Text.
 * Non-Text descendants are preserved without change.
 * @return The Text child which receives the specified text. 
 * @see Node.setTextContent()//w  w  w .  ja  v a  2  s . c  o m
 */
public static Text setText(Node node, String text) {
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0, n = children.getLength(); i < n; ++i) {
            Node child = children.item(i);
            if (child instanceof Text) {
                return ((Text) child).replaceWholeText(text);
            }
        }
    }
    Text ans = node.getOwnerDocument().createTextNode(text);
    node.appendChild(ans);
    return ans;
}

From source file:Main.java

public static void getXMLContent(String filePath) {
    try {/*from   ww  w.j  av  a  2  s.  c o m*/
        File f = new File(filePath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document doc = builder.parse(f);
        NodeList nodes_1 = doc.getChildNodes();
        for (int i = 0; i < nodes_1.getLength(); i++) {
            org.w3c.dom.Node nodes_2 = nodes_1.item(i);
            NodeList nodes_3 = nodes_2.getChildNodes();
            for (int j = 0; j < nodes_3.getLength(); j++) {
                org.w3c.dom.Node node = nodes_3.item(j);
                NodeList xmlMeta = node.getChildNodes();
                for (int k = 0; k < xmlMeta.getLength(); k++) {
                    //                  value = xmlMeta.item(k).getNodeValue();
                    System.out.println(xmlMeta.item(k).getNodeName() + ":" + xmlMeta.item(k).getNodeValue());
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static List<Map<String, String>> readXMLFile(String outFile) {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    List<Map<String, String>> returnlist = new ArrayList<Map<String, String>>();

    try {// www . j  a  v a  2  s  . co m
        DocumentBuilder dombuilder = domfac.newDocumentBuilder();
        InputStream is = new FileInputStream(outFile);
        Document doc = dombuilder.parse(is);
        NodeList nl = doc.getElementsByTagName("row");
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            NodeList fileds = node.getChildNodes();
            Map<String, String> map = new HashMap<String, String>();
            for (int j = 0; j < fileds.getLength(); j++) {
                Node filed = fileds.item(j);

                if (filed.getNodeType() == Node.ELEMENT_NODE) {
                    map.put(filed.getAttributes().getNamedItem("name").getNodeValue(),
                            filed.getFirstChild().getNodeValue());
                }
            }
            returnlist.add(map);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return returnlist;

}

From source file:Main.java

/**
 * This method gets the child node with the provided name from the 
 * provided start node./*w  ww .  j a v  a  2  s.c  om*/
 * @param start The Parent node to scan children.
 * @param nodeName The name of the node that we're looking for.
 * @return The Node with name: nodeName, whose parent is the start node.
 *
 * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a>
 */
public static Node getChildNode(Node start, String nodeName) {
    Node child = null;

    NodeList children = start.getChildNodes();

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

        if (nodeName.equals(n.getNodeName())) {
            child = n;
            break;
        }
    }

    return child;
}

From source file:Main.java

/**
 * Search a child node by type//from  w w  w.  ja  v a2s.c o  m
 * 
 * @param parent   the parent node
 * @param nodeName   the node name
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByNameAndType(Node parent, String nodeName, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static List<Comment> getCommentNode(Node n) {
    List<Comment> commentList = new ArrayList<Comment>();
    NodeList nodeList = n.getChildNodes();
    if (nodeList == null)
        return null;
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node ithNode = nodeList.item(i);
        if (ithNode instanceof Comment) {
            commentList.add((Comment) ithNode);
        }/*  ww  w .  j  av a 2 s  .c  o m*/
    }
    return commentList;
}

From source file:Main.java

private static String getText(Node node) {
    if (node == null)
        return null;
    NodeList lst = node.getChildNodes();
    int size = lst.getLength();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);//from w  ww.j a  v a2 s . co m
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            sb.append(t.getData());
        }
    }
    return sb.toString();
}