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 readXML(File xmlfile, String tag) throws Exception {
    NodeList list = null;//  w w w .j a va 2s . co m
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xmlfile);
    Element rootElement = doc.getDocumentElement();
    list = rootElement.getElementsByTagName(tag);

    return list;
}

From source file:Main.java

public static Document fileToXMLDoc(File file) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(file);

    doc.getDocumentElement().normalize();

    return doc;/* w  ww  .j av  a 2 s.c om*/
}

From source file:Main.java

public static Document getXmlDocument(InputStream is) throws Exception {
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    return documentBuilder.parse(is);
}

From source file:Main.java

public static Document loadXml(String fileName) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//w  w w  .  j av a  2s.c  om
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(fileName);
    return doc;
}

From source file:Main.java

public static Document getW3CDom(String xmlStr) throws ParserConfigurationException, SAXException, IOException {
    StringReader sr = new StringReader(xmlStr);
    InputSource is = new InputSource(sr);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(is);
    return doc;/*w  w w . j a va 2s .com*/
}

From source file:Main.java

/**
 * Loads an XML stream into a Document instance
 * //from  w  w  w. j av  a2s . com
 * @param is
 * @return XML file loaded in a document
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document loadDoc(InputStream is) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(is);
}

From source file:Main.java

public static Document file2doc(File file) throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilder dBuilder = builderFac.newDocumentBuilder();
    Document doc = dBuilder.parse(file);
    doc.getDocumentElement().normalize();
    return doc;/*from w  ww .j a va2  s.  c om*/
}

From source file:Main.java

public static Document parseXml(String xml) {
    try {/*from w w w.j  a v  a 2  s  .  c o  m*/
        DocumentBuilder dBuilder = getDocumentBuilder();
        return dBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document read(InputStream input) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {//from ww  w.  j a  v  a 2 s .c  om
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.parse(input);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    // SAXReader saxReader = new SAXReader();
    // Document document;
    // try {
    // document = saxReader.read(input);
    // }
    // catch (DocumentException e) {
    // throw new RuntimeException(e.getMessage(), e);
    // }
    // return document;
}

From source file:Main.java

public static Document documentFromString(String xml) throws SAXException, IOException {

    Reader reader = new StringReader(xml);
    InputSource source = new InputSource(reader);
    DocumentBuilder db = newDocumentBuilder(false);
    return db.parse(source);
}