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 Document getDocument() throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(filename);
    return document;
}

From source file:Main.java

public static String[] xpathOnString(String q, String stringdoc) {
    try {//www  .  j  a  v a 2s.c  om
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        // domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(stringdoc.getBytes()));

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(q);

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        String res[] = new String[nodes.getLength()];
        for (int i = 0; i < nodes.getLength(); i++) {
            // System.out.println(nodes.item(i).toString());
            res[i] = nodes.item(i).getNodeValue();
        }
        return res;
    } catch (Exception e) {
        System.out.println("XPathUtils.xpathOnString:caught:" + e);
        return null;
    }
}

From source file:Main.java

public static DOMSource fileToDomSrc(String path) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);//from w w w  .  j  a va 2  s .  c o  m
    DocumentBuilder builder = dbFactory.newDocumentBuilder();
    Document document = builder.parse("file:///music/order/soap.xml");
    DOMSource domSource = new DOMSource(document);
    return domSource;
}

From source file:Main.java

public static Document createXMLDocument() {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);/*  w  w  w.j  a  v a  2 s.  c o  m*/
    DocumentBuilder docBuilder = null;
    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Document doc = docBuilder.newDocument();
    return doc;
}

From source file:Main.java

/**
 * Load a DOM document/*from  w  ww.  j a va 2s.c o  m*/
 */
public static Document loadDocument(InputStream in) throws IOException {
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.parse(in);
    } catch (ParserConfigurationException e) {
        throw new IOException(e.getMessage());
    } catch (SAXException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static Document newDocument() {
    try {// w w  w . j  a v a  2 s .co  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        dFactory.setNamespaceAware(true);
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document newDocument() throws ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    return builder.newDocument();
}

From source file:Main.java

public static Document readDocument(InputStream is) {
    try {/*from   w ww .java 2s.c om*/
        final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        final Document doc = docBuilder.parse(is);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document parseStream(final InputStream xmlis) {
    try {/*from   ww  w . j ava  2  s.c om*/
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(xmlis));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document createNewDocument() throws ParserConfigurationException {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = builder.newDocument();
    return doc;/*from  w  w  w .  ja  va 2  s  .  c om*/
}