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 NodeList getNodesByXPath(File file, String xpath) {
    if (!file.isFile() || !file.canRead())
        return null;

    Document document;// w w w  .  ja v a  2 s.  c om
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
        document = documentBuilder.parse(file);
    } catch (ParserConfigurationException e) {
        return null;
    } catch (SAXException e) {
        return null;
    } catch (IOException e) {
        return null;
    }

    if (document == null) {
        return null;
    }

    Object result;
    try {
        XPath xpathCompiler = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpr = xpathCompiler.compile(xpath);
        result = xPathExpr.evaluate(document, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        return null;
    }

    return (NodeList) result;
}

From source file:Main.java

public static Document stringToDoc(final String xml) throws Exception {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

private static Document parseXmlFile(String in) {
    try {/* w  ww. j  av a2 s .com*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Metodo para carregar o arquivo XML assinado e garantir que o XML esta bem formatado.
 * /* www . ja  v a  2s.co  m*/
 * @param xmlFile arquivo XML assinado.
 * 
 * @return org.w3c.dom.Document 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 */

public static Document carregarArquivoXML(InputStream xmlFile)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    doc = db.parse(xmlFile);
    return doc;
}

From source file:Main.java

public static Document loadDocument(File file) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc;//from w w  w.java  2 s .  c  o  m
    DocumentBuilder db;

    db = dbf.newDocumentBuilder();
    doc = db.parse(file);

    return doc;
}

From source file:Main.java

/**
 * Metodo para carregar uma string contendo um arquivo xml assinado.
 * //  w  ww  . ja  v  a 2 s .  c  o m
 * @param xmlString -> XML assinado.
 * 
 * @return org.w3c.dom.Document 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 */

public static Document carregarArquivoXML(String xmlString)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xmlString)));
    return doc;
}

From source file:Main.java

/**
 * Metodo para carregar o arquivo XML assinado e garantir que o XML esta bem formatado.
 * /*from  w  w  w  .  j av  a2s  . c om*/
 * @param xmlSource arquivo XML assinado.
 * 
 * @return org.w3c.dom.Document 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 */

public static Document carregarArquivoXML(InputSource xmlSource)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    doc = db.parse(xmlSource);
    return doc;
}

From source file:Main.java

public static Document loadDocument(String path)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc;// www. j  ava  2 s .  c om
    DocumentBuilder db;

    db = dbf.newDocumentBuilder();
    doc = db.parse(new File(path));

    return doc;
}

From source file:Main.java

public static Document loadDocument(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc;/*from w ww  .ja va 2s .c  o m*/
    DocumentBuilder db;

    db = dbf.newDocumentBuilder();
    doc = db.parse(is);

    return doc;
}

From source file:Main.java

public static Document loadDocument(ReadableByteChannel in)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc;//from   w  ww  .j a v  a 2  s  .  co m
    DocumentBuilder db;

    db = dbf.newDocumentBuilder();
    doc = db.parse(Channels.newInputStream(in));

    return doc;
}