Example usage for javax.xml.parsers DocumentBuilder parse

List of usage examples for javax.xml.parsers DocumentBuilder parse

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder parse.

Prototype


public abstract Document parse(InputSource is) throws SAXException, IOException;

Source Link

Document

Parse the content of the given input source as an XML document and return a new DOM Document object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
    domBuilder.parse(args[0]);
    System.out.println("'" + args[0] + "' is well-formed.");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document xml = db.parse("input.xml");

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    String health = (String) xpath.evaluate("/game/health", xml, XPathConstants.STRING);
    System.out.println(health);/*from ww w  .  j ava  2s.  c  om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("input.xml"));
    NodeList nodeList = document.getElementsByTagName("Item");
    for (int x = 0, size = nodeList.getLength(); x < size; x++) {
        System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue());
    }//from ww  w. j a  v a  2  s.c  o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("yourFile.xml"));
    NodeList nodeList = document.getElementsByTagName("message");
    for (int x = 0, size = nodeList.getLength(); x < size; x++) {
        System.out.println(nodeList.item(x).getAttributes().getNamedItem("to").getNodeValue());
    }/* w  w  w  . j  a va 2s . co m*/
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.parse("sample.xml");
    Element tree = document.getDocumentElement();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();/*w ww  .  j a va  2  s . co m*/
    Document doc = db.parse(new File("games.xml"));
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.parse(new FileInputStream("data.xml"));
    Element root = doc.getDocumentElement();
    org.w3c.dom.NodeList nodeList = root.getElementsByTagName("key");
    for (int i = 0; i < nodeList.getLength(); i++) {
        System.out.print(((Node) nodeList.item(i)).getAttributes().getNamedItem("keyname"));
        System.out.println("\tvalue: " + ((Node) nodeList.item(i)).getTextContent());
    }//from  w  w  w. jav a2s.c  om

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new URL("http://www.your server.com/daily.xml").openStream();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);
    NodeList nodeList = doc.getElementsByTagName("v");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getAttribute("currency").equals("BRL")) {
                System.out.println(element.getAttribute("rate"));
            }//from   w  w w .ja v  a2s  .c  om
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("input.xml");

    XPath xPath = XPathFactory.newInstance().newXPath();
    String string = (String) xPath.evaluate("/documents/document/element[@name='doctype']/value", dDoc,
            XPathConstants.STRING);
    System.out.println(string);/*from w  ww  . j  a  v a2s . c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("data.xsd"));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w w  w .j  a  va 2 s.c  om*/
    dbf.setSchema(schema);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("data.xml"));

    Element result = document.getElementById("abc");
    System.out.println(result);
}