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 readXml(InputSource source)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
    DocumentBuilder b = f.newDocumentBuilder();
    return b.parse(source);
}

From source file:Main.java

public static Document parseXmlString(String xmlString) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    return docBuilder.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
}

From source file:Main.java

/**
 * Returns root tagname/*from   w ww .java2  s.c o m*/
 *
 * @param xml  The xml document to be searched.
 * @return String result.
 */
public static String getRootTagName(String xml) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document.getDocumentElement().getTagName();
}

From source file:Main.java

public static Document inputStream2Document(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    Document document = null;/* w  ww  . j a v  a2  s .c  o  m*/

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    document = builder.parse(is);

    return document;
}

From source file:Main.java

public static Document parseDocumentByString(String result) {
    try {//w  ww . j a va 2  s . co  m
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(result.getBytes("UTF-8")));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String toXMLString(String fileName) throws Exception {
    File file = new File(fileName);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return toXMLString(doc);

}

From source file:Main.java

/**
 * Parse XML/*ww  w . j  ava2  s . c  o  m*/
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * */
public static Document parseXMLFromString(String str)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new ByteArrayInputStream(str.getBytes()));
    return doc;
}

From source file:Main.java

private static Document parseXml(String xmlPath) {
    Document doc = null;/*from ww w  . jav  a2s  . c om*/
    try {
        File trainSchedule = new File(xmlPath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(trainSchedule);
        doc.getDocumentElement().normalize();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (doc == null)
        throw new ParserException("cannot parse: \"" + xmlPath + "\"");

    return doc;
}

From source file:Main.java

/**
 * Gets Document representation of xml file
 * //from ww  w  . jav  a  2  s .  c  o m
 * @param file
 *            xml file
 * @return Document object
 */
public static Document getXmlDocument(File file) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(file);
        return doc;
    } catch (Exception e) {
        throw new RuntimeException("Could not parse XML document", e);
    }
}

From source file:Main.java

public static Document xmlToDoc(String xml) {
    Document doc = null;/*from w w  w . ja  va 2  s  .  co m*/
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        doc = docBuilder.parse(xml);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return doc;
}