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 Document parseXML(String s) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(s)));
    return doc;//from w w w  .j  a  v a2s  .  c om
}

From source file:Main.java

public static Document fromXML(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   w w  w.j  ava2s  .  c  om
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

public static org.w3c.dom.Document getDocument(File file)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(file);
}

From source file:Main.java

public static Document loadDocument(String filePath) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(filePath);
    return doc;/*from  www .ja  va2  s  .  c  om*/
}

From source file:Main.java

public static String getValue(String xmlPath, String key) throws Exception {
    File file = new File(xmlPath);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(file);
    return doc.getElementsByTagName(key).item(0).getFirstChild().getNodeValue();
}

From source file:Main.java

public static Document getW3cDomDocumentFromAsset(AssetManager assetManager, String fileName)
        throws IOException, ParserConfigurationException, SAXException {
    InputStream inputStream = assetManager.open(fileName);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(inputStream);
}

From source file:Main.java

public static Document getDoc(String xmlFile) throws ParserConfigurationException, SAXException, IOException {
    File file = new File(xmlFile);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();

    return doc;//from  w  ww  . j  a v a 2s . co  m
}

From source file:Main.java

public static Document InputStreamToDoc(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.parse(is);
}

From source file:Main.java

public static Document getDocument() throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dBuilderFactory.newDocumentBuilder();
    return dBuilder.parse(docPath);
}

From source file:Main.java

static public Document inputStreamToDocument(InputStream inputStream) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(inputStream);
    doc.getDocumentElement().normalize();

    return doc;/*from ww  w  .  j  a  v  a  2s .  com*/
}