Example usage for javax.xml.parsers DocumentBuilderFactory newInstance

List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory newInstance.

Prototype

public static DocumentBuilderFactory newInstance() 

Source Link

Document

Obtain a new instance of a DocumentBuilderFactory .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*ww w  .  j a  v  a 2  s .  c o  m*/

    factory.setExpandEntityReferences(false);

    Document doc1 = factory.newDocumentBuilder().parse(new File("filename"));
    NodeList list = doc1.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    Document doc2 = factory.newDocumentBuilder().parse(new File("infilename2.xml"));

    // Make a copy of the element subtree suitable for inserting into doc2
    Node node = doc2.importNode(element, true);

    // Get the parent
    Node parent = node.getParentNode();

    // Get children
    NodeList children = node.getChildNodes();

    // Get first child; null if no children
    Node child = node.getFirstChild();

    // Get last child; null if no children
    child = node.getLastChild();

    // Get next sibling; null if node is last child
    Node sibling = node.getNextSibling();

    // Get previous sibling; null if node is first child
    sibling = node.getPreviousSibling();

    // Get first sibling
    sibling = node.getParentNode().getFirstChild();

    // Get last sibling
    sibling = node.getParentNode().getLastChild();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    Element root = doc.getDocumentElement();
    System.out.println(root);/*from ww  w .j a v  a2  s.  c o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("Test.xml"));
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression exp = xPath.compile("//data");
    NodeList nl = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
    System.out.println("Found " + nl.getLength() + " results");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    builder.setEntityResolver(new MyResolver());
    Document doc = builder.parse(new File("infilename.xml"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new File("D:/test.xml"));

    NodeList elt = doc.getElementsByTagName("EMPLOYEE");
    for (int k = 0; k < elt.getLength(); k++) {
        Node firstNode3 = elt.item(k);
        Element elt1 = (Element) firstNode3;
        String att = elt1.getAttribute("PERMANENT");
        System.out.println("\n\nPERMANENT: " + att);

        NodeList nodes = elt1.getElementsByTagName("DETAILS");
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childNode = nodes.item(i);
            Element elt2 = (Element) childNode;
            System.out.println("---" + elt2.getNodeName());
            System.out.println("NAME:" + elt2.getAttribute("NAME"));
            System.out.println("ID:" + elt2.getAttribute("ID"));
            System.out.println("AGE:" + elt2.getAttribute("AGE"));
        }/*  w  w  w. jav  a 2s .c  o  m*/
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from   w ww . j a  va2  s.  c  o  m*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    cdata.setData("some data");
    int len = cdata.getLength();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File CFile = new File("data.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);//w  w w .j a  v a 2 s. c  o m
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(CFile);

    NodeList pizzas = document.getElementsByTagName("Pizza");

    for (int i = 0; i < pizzas.getLength(); i++) {
        Element pizzaSize = (Element) pizzas.item(i);
        String pSize = pizzaSize.getAttribute("Size");

        if (pSize.equalsIgnoreCase("Large"))
            System.out.println(10.0);
        if (pSize.equalsIgnoreCase("Medium"))
            System.out.println(7.0);
        if (pSize.equalsIgnoreCase("Small"))
            System.out.println(5.0);
    }
}

From source file:ListMoviesXML.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);//from   w ww.j  a  va  2  s. c  o m

    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new InputSource("y.xml"));
    Element root = doc.getDocumentElement();

    Element movieElement = (Element) root.getFirstChild();
    Movie m;
    while (movieElement != null) {
        m = getMovie(movieElement);
        String msg = Integer.toString(m.year);
        msg += ": " + m.title;
        msg += " (" + m.price + ")";
        System.out.println(msg);
        movieElement = (Element) movieElement.getNextSibling();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*  w w  w.ja v  a2s.  co  m*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    // All three types of nodes implement the CharacterData interface
    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    // Delete text
    int offset = 0;
    int len = 5;
    cdata.deleteData(offset, len);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from ww  w  .ja  v a2 s.  c o m
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    // All three types of nodes implement the CharacterData interface
    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    cdata.setData("some data");
    int len = cdata.getLength();
    int offset = 5;
    len = 4;
    String s = cdata.substringData(offset, len);
}