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 main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  ww  w.  j a  v a  2  s  . c om

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element root = null;

    NodeList list = doc.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i) instanceof Element) {
            root = (Element) list.item(i);
            break;
        }
    }
    root = doc.getDocumentElement();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = dBuilder.parse(new File("data.xml"));
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    if (doc.hasChildNodes()) {
        printNote(doc.getChildNodes(), 1);
    }/*  w ww . j  ava  2 s . c  om*/
}

From source file:Main.java

public static Node getRootNode(Document doc) {
    NodeList l = doc.getChildNodes();
    if (l != null && l.getLength() == 1)
        return l.item(0);

    return null;/*from   w w  w. j  a  v  a  2s. c  o m*/
}

From source file:Main.java

/**
 * Returns the main node of the XML document
 * //from  w ww . java2  s.  com
 * @param doc the XML document
 * @return the main node
 */
public static Node getMainNode(Document doc) {
    for (int i = 0; i < doc.getChildNodes().getLength(); i++) {
        Node node = doc.getChildNodes().item(i);
        if (!node.getNodeName().equals("#text") && !node.getNodeName().equals("#comment")) {
            return node;
        }
    }
    throw new RuntimeException("main node in XML file could not be retrieved");
}

From source file:Main.java

/**
 * Clears all childnodes in document/*from w w w.j av  a 2s  .  co  m*/
 */
public static void clearDocument(Document document) {
    NodeList nodeList = document.getChildNodes();
    if (nodeList == null) {
        return;
    }
    int len = nodeList.getLength();
    for (int i = 0; i < len; i++) {
        document.removeChild(nodeList.item(i));
    }
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {
    LinkedList result = new LinkedList();

    DOMParser parser = new DOMParser();
    try {/* w w  w. j av  a2  s .c  om*/
        parser.parse(new InputSource(new StringReader(input)));
        Document doc = parser.getDocument();
        NodeList nl = doc.getChildNodes().item(0).getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++)
            if (nl.item(i).getNodeType() == 1)
                result.add(nl.item(i).getTextContent());
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {
    LinkedList result = new LinkedList();

    DOMParser parser = new DOMParser();
    try {/* w  ww  .  j  a  v a  2s.c om*/
        parser.parse(new InputSource(new StringReader(input)));
        Document doc = parser.getDocument();
        NodeList nl = doc.getChildNodes().item(0).getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; ++i)
            if (nl.item(i).getNodeType() == 1)
                result.add(nl.item(i).getTextContent());
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {

    LinkedList<String> result = new LinkedList<String>();

    DOMParser parser = new DOMParser();
    try {/*from w  ww  . j a  v  a 2  s . c o  m*/
        parser.parse(new InputSource(new StringReader(input)));
        Document doc = parser.getDocument();
        NodeList nl = doc.getChildNodes().item(0).getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Document.ELEMENT_NODE)
                result.add(nl.item(i).getNodeValue());
        }
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {

    LinkedList<String> result = new LinkedList<String>();

    DOMParser parser = new DOMParser();
    try {//from www  . j  a v a 2s .co  m
        parser.parse(new InputSource(new StringReader(input)));
        Document doc = parser.getDocument();
        NodeList nl = doc.getChildNodes().item(0).getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Document.ELEMENT_NODE)
                result.add(nl.item(i).getTextContent());
        }
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {

    LinkedList<String> result = new LinkedList<String>();

    DOMParser parser = new DOMParser();
    try {/*  w w w .ja va 2  s  .c om*/
        parser.parse(new InputSource(new StringReader(input)));
        Document doc = parser.getDocument();
        NodeList nl = doc.getChildNodes().item(0).getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Document.ELEMENT_NODE)
                result.add(nl.item(i).getFirstChild().getNodeValue());
        }
    } catch (SAXException e) {

    } catch (IOException e) {

    }
    return result;
}