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 void test() {
    String xml = "C:\\Users\\is96092\\Desktop\\music\\test.xml";
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/*from w ww  .j a v a2 s.co  m*/
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(xml);
        Element doc = dom.getDocumentElement();
        String role1 = "";
        role1 = getTextValue(role1, doc, "note");
        System.out.println("result : " + role1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Document getDocumentFromXML(String mappingFile, String schemaFile) {
    try {/*w w  w .j  a  v  a 2  s.c  om*/
        if (schemaFile != null) {
            if (validateXML(schemaFile, mappingFile) == false) {
                return null;
            }
        }
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(mappingFile);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Node stringToNode(String string) {
    try {/*from   w w w . j  a  v a 2s . co m*/
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return db.parse(new ByteArrayInputStream(string.getBytes())).getFirstChild();
    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

/**
 * Parses a string into an XMLDocument/*  w  w  w.  jav  a2 s. c om*/
 * 
 * @param xmlString
 * @return
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public static Document loadXML(String xmlString)
        throws IOException, ParserConfigurationException, SAXException {
    InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputStream);

    // optional, but recommended
    // read this -
    // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

    return doc;
}

From source file:Main.java

public static Document deserialize(String sXML) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(sXML)));
    return doc;/*from  w  w w  . ja v  a2s  . c  o m*/
}

From source file:Main.java

static public Document createDocument(InputStream content) {
    try {// ww w  .  j a v  a2  s.com
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(content);
        return doc;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Object getXMLValue(String xml, String xQuery, QName resultType)
        throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(new InputSource(new StringReader(xml)));

    XPathFactory xPathfactory = XPathFactory.newInstance();

    XPath xPath = xPathfactory.newXPath();
    XPathExpression xPathExpression = xPath.compile(xQuery);
    return xPathExpression.evaluate(doc, resultType);
}

From source file:Main.java

public static Document loadXmlDocumentFromStream(@Nonnull InputStream in)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    return documentBuilder.parse(in);
}

From source file:Main.java

public static Document loadXmlDocumentFromFile(@Nonnull File file)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    return documentBuilder.parse(file);
}

From source file:Main.java

/**
 * This method returns the Document object for xml file.
 * @param fileName File name./*from   w  w w .j  av  a2  s.  c om*/
 * @return Document xml document.
 * @throws ParserConfigurationException throws this exception if DocumentBuilderFactory not created.
 * @throws IOException throws this exception if file not found.
 * @throws SAXException throws this exception if not able to parse xml file.
 */
public static Document getDocument(String fileName)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(fileName);
    DocumentBuilder documentBuilder = getDocumentBuilder();
    return documentBuilder.parse(file);
}