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[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("file:////tmp/whatever.xml");
    String version = xpath.evaluate("//behavior/@version", doc);
    System.out.println(version);//from w  w  w  .  j a v  a 2 s .  com
}

From source file:Main.java

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

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

    Element element = (Element) doc.getElementsByTagName("junk").item(0);

    element.getParentNode().removeChild(element);
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    xmlDoc.setXmlVersion("2.0");

}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(Main.class.getResourceAsStream("/foo.xml"));
    NodeList nodeNodeList = document.getElementsByTagName("node");
    for (int i = 0; i < nodeNodeList.getLength(); i++) {
        Node nNode = nodeNodeList.item(i);
        System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue());
        System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());
    }/*  w w w. j ava  2 s. co  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  w w w .  j a v  a2s  .co 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"));

    Node dup = doc2.importNode(element, true);

    doc2.getDocumentElement().appendChild(dup);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true); // Set namespace aware
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    System.out.println(xmlDoc.getDocumentURI());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<a><o><u>ok</u></o></a>";
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));
    NodeList nl = doc.getElementsByTagName("*");
    for (int i = 0; i < nl.getLength(); i++) {
        System.out.println("name is : " + nl.item(i).getNodeName());
    }//from  w  w w.j av  a  2s. c  o  m
}

From source file:MainClass.java

static public void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//www . j a  va 2s .  com
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("y.xml");

    NodeList configs = doc.getElementsByTagName("C");
    for (int i = 0; i < configs.getLength(); i++) {
        Element config = (Element) configs.item(i);
        String runMode = config.getAttribute("r").trim();
        if (runMode.equals("test")) {
            NodeList connectionURLs = config.getElementsByTagName("URL");
            System.out.println(connectionURLs.item(0).getNodeName() + "="
                    + connectionURLs.item(0).getFirstChild().getNodeValue());
            return;
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from w  w  w  . ja v  a  2 s.  com*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    NamedNodeMap attrs = element.getAttributes();

    int numAttrs = attrs.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attrs.item(i);
        String attrName = attr.getNodeName();
        String attrValue = attr.getNodeValue();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from   w  w w.  j  a  v a  2 s .co  m*/

    factory.setExpandEntityReferences(false);

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

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);

        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}