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 {

    String xml = "<metadata><codes class = 'class1'>" + "<code code='ABC'>" + "<detail x='blah blah'/>"
            + "</code>" + "</codes>" + "<codes class = 'class2'>" + "<code code = '123'>"
            + "<detail x='blah blah'/></code></codes></metadata>";

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new ByteArrayInputStream(xml.getBytes()));

    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile("//codes/code[@code ='ABC']");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) result;
    System.out.println(nodes.getLength() > 0 ? "Yes" : "No");
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println("nodes: " + nodes.item(i).getNodeValue());
    }/*from w  w  w  .  j  a va 2  s.co  m*/
}

From source file:MainClass.java

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

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.parse("sample.xml");

    String docNS = "http://www.my-company.com";

    Element order = document.getDocumentElement();
    Attr date = order.getAttributeNodeNS(docNS, "date");
    NodeList children = order.getElementsByTagNameNS(docNS, "item");

}

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 order = document.getDocumentElement();

    DocumentRange ranges = (DocumentRange) document;

    Range range = ranges.createRange();//from  w w  w.j  a  v  a 2  s.  c  o  m
    range.setStartBefore(order.getFirstChild());
    range.setEndAfter(order.getLastChild());

    range.deleteContents();
    range.detach();

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    File stylesheet = new File("style.xsl");
    File xmlSource = new File("data.xml");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(xmlSource);

    StreamSource stylesource = new StreamSource(stylesheet);
    Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
    Source source = new DOMSource(document);
    Result outputTarget = new StreamResult(new File("/tmp/x.csv"));
    transformer.transform(source, outputTarget);
}

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");

    DocumentTraversal traversal = (DocumentTraversal) document;

    TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null,
            true);//from   w w  w  . ja  v a 2s.c  o  m

    traverseLevel(walker, "");
}

From source file:Main.java

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

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("//node3/name", document, XPathConstants.NODE);
}

From source file:Main.java

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

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("//*[@id='one']", document, XPathConstants.NODE);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("path/to/file.xml"));

    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression expression = xPath.compile("PersonList/Person/Age/text() | PersonList/Person/Name/text()");

    NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getParentNode().getNodeName().equals("Name")) {
            node.setNodeValue("new name");
        } else {/* ww  w.java2  s. c  om*/
            node.setNodeValue("42");
        }
    }
}

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 purchaseOrder = document.getDocumentElement();

    Attr orderDate = purchaseOrder.getAttributeNode("date");
    System.out.println(orderDate.getValue());

    NamedNodeMap attrs = purchaseOrder.getAttributes();
    int attrsCount = attrs.getLength();

    for (int i = 0; i < attrsCount; i++) {
        Attr item = (Attr) attrs.item(i);
        System.out.println("'" + item.getName() + "' = '" + item.getValue() + "'");
    }//from   w w  w .ja  v  a  2 s  . co  m
}