Example usage for org.w3c.dom Document getChildNodes

List of usage examples for org.w3c.dom Document getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Document 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 void getXMLContent(String filePath) {
    try {/*from   w  ww  . j  a va2s  . c  om*/
        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

private static String readXsdVersionFromFile(Document doc) {
    final String JBOSS_ESB = "jbossesb";
    NodeList nodes = doc.getChildNodes();
    if (nodes != null) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (JBOSS_ESB.equals(node.getNodeName())) {
                NamedNodeMap attributes = node.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node attribute = attributes.item(j);
                    if ("xmlns".equals(attribute.getNodeName())) {
                        String value = attribute.getNodeValue();
                        if (value.contains(JBOSS_ESB) && value.endsWith(".xsd"))
                            return value.substring(value.lastIndexOf('/') + 1, value.length());
                        else
                            throw new IllegalStateException(
                                    "The ESB descriptor points to an invalid XSD" + value);
                    }//from  www.j  ava2s. c o  m
                }
            }
        }
        throw new IllegalArgumentException("No root node " + JBOSS_ESB + " found.");
    } else
        throw new IllegalArgumentException("Descriptor has no root element !");
}

From source file:Main.java

static public String getDocumentComment(Document doc) {
    String docComment = null;//w ww .  j av a 2s.c o m
    try {
        NodeList childs = doc.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node child = childs.item(i);
            if (child.getNodeType() == Node.COMMENT_NODE) {
                docComment = child.getNodeValue();
            }
        }
        return docComment;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Return the root {@link Element} of a {@link Document}.
 *
 * @param doc/*  w w  w  .j  av a  2 s  .  com*/
 * the document, where the root element is looked up
 *
 * @return
 * root element of the document or null, if not found
 */
public static Element getRootElement(Document doc) {
    if (doc == null)
        return null;
    NodeList children = doc.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node instanceof Element)
            return (Element) node;
    }
    return null;
}

From source file:Main.java

/**
 * Search a first node by name in the document
 * //from   w  w w.j  ava2s.c o  m
 * @param doc      source document
 * @param nodeName   the node name for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodesByName(Document doc, String nodeName) throws Exception {

    Node node = null;

    NodeList nl = doc.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            break;
        }
    }

    return node;

}

From source file:Main.java

public static void addElementToXml(String xmlFile, String nodeToAdd, String nodeContent)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {

    Document doc = getXmlDoc(xmlFile);

    NodeList nodes = doc.getChildNodes();

    Node node = doc.createElement(nodeToAdd);
    node.setNodeValue(nodeContent);//from   w  w w . j a  va 2 s.co m

    nodes.item(0).appendChild(node);

    saveXml(doc, xmlFile);
}

From source file:Main.java

public static void removeElementFromXml(String xmlFile, String nodeName)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {

    Document doc = getXmlDoc(xmlFile);

    NodeList nodes = doc.getChildNodes();

    NodeList childNodes = nodes.item(0).getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {

        if (childNodes.item(i).getNodeName().equals(nodeName)) {

            nodes.item(0).removeChild(childNodes.item(i));
        }//w  w  w.  ja v a  2 s . c  om
    }
    saveXml(doc, xmlFile);
}

From source file:Main.java

/**
 * Extract included filenames in the XML document, assuming that filenames are
 * provided with the attribute "href".//w ww  .j a  v a2 s  .c  o m
 * 
 * @param xmlDocument the XML document
 * @return the filenames to include
 */
private static Vector<String> extractIncludedFiles(Document xmlDocument) {

    Vector<String> includedFiles = new Vector<String>();

    NodeList top = xmlDocument.getChildNodes();
    for (int i = 0; i < top.getLength(); i++) {
        Node topNode = top.item(i);
        NodeList firstElements = topNode.getChildNodes();
        for (int j = 0; j < firstElements.getLength(); j++) {
            Node midNode = firstElements.item(j);
            for (int k = 0; k < midNode.getChildNodes().getLength(); k++) {
                Node node = midNode.getChildNodes().item(k);
                if (node.hasAttributes() && node.getAttributes().getNamedItem("href") != null) {
                    String fileName = node.getAttributes().getNamedItem("href").getNodeValue();
                    includedFiles.add(fileName);
                }
            }

        }
    }
    return includedFiles;
}

From source file:com.spun.util.ups.UPSUtils.java

/***********************************************************************/
private static Node getDocument(Document document) {
    return document != null ? document.getChildNodes().item(0) : null;
}

From source file:Main.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;//from w  ww .  j  a va 2s. com
    if (element == null) {
        child = doc.getChildNodes();

    } else {
        child = element.getChildNodes();
    }
    for (int j = 0; j < child.getLength(); j++) {
        if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            Element childElement = (Element) child.item(j);
            if (childElement.hasChildNodes()) {
                System.out.println(childElement.getTagName() + " : " + childElement.getTextContent());
                oauthResponse.put(childElement.getTagName(), childElement.getTextContent());
                parseXMLDoc(childElement, null, oauthResponse);
            }

        }
    }
}