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

/**
 * This method returns the Document object for input stream.
 * @param inputStream InputStream of xml file .
 * @return Document object for input stream.
 * @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.
 *//*from w  w  w. j  av a 2  s  . c o  m*/
public static Document getDocument(InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilder documentBuilder = getDocumentBuilder();
    return documentBuilder.parse(inputStream);
}

From source file:Main.java

/**
 * Load a DOM document//  w w w  .  j  a  v  a  2 s. com
 */
public static Document loadDocument(InputStream in) throws IOException {
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.parse(in);
    } catch (ParserConfigurationException e) {
        throw new IOException(e.getMessage());
    } catch (SAXException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

/**
 * Loads the imput stream and returns the root element.
 * //from w ww .  j  a  v a2  s .  co  m
 * @param in
 *            Input Stream.
 * 
 * @return root element of the xml document, never <code>null</code>.
 * 
 * @throws Exception
 *             on any error
 */
public static Element loadXmlResource(final InputStream in) throws Exception {
    DocumentBuilder db = getDocumentBuilder();
    Document doc = db.parse(in);
    return doc.getDocumentElement();
}

From source file:Main.java

public static Document DocumentFactory(Path xmlfile) {
    //get document object of the xml file
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    Document document = null;/*  ww  w  . jav  a2  s.  co  m*/
    try {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        document = builder.parse(xmlfile.toFile());
    } catch (ParserConfigurationException e) {
        log.error("DOM parser configuration exception");
        e.printStackTrace();
    } catch (SAXException e) {
        log.error("DOM parse error");
        e.printStackTrace();
    } catch (IOException e) {
        log.error("IO exception");
        e.printStackTrace();
    }
    return document;
}

From source file:Main.java

public static NodeList getXMLNodes(final String xmlFileName, final String tagName) {
    NodeList nList = null;//from w ww.j av  a 2 s .  c  o m
    try {
        File xmlFile = new File(xmlFileName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlFile);
        doc.getDocumentElement().normalize();
        nList = doc.getElementsByTagName(tagName);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return nList;
}

From source file:Main.java

public static Document parse(final String xmlContent) {
    try {//www .  j a v  a  2s .co m
        final DocumentBuilder builder = getDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xmlContent.getBytes()));
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

static public Element getDocumentElement(InputStream in) {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

    try {/* w w  w.ja va 2 s.com*/
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(in);

        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/** Parse a valid xml string and return the Element representing this string. */
public static Element parseXMLString(Document document, String string)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  ww  w  .ja  v  a  2  s . co m*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document subDoc = builder.parse(new InputSource(new StringReader(string)));
    Element e = subDoc.getDocumentElement();
    return (Element) document.importNode(e, true);
}

From source file:Main.java

public static boolean isXMLWellFormed(String xmlUrl)
        throws IOException, SAXException, ParserConfigurationException {

    boolean isXMLValid = false;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);//from  w  ww.  ja v a 2s  .  com
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = (Document) builder.parse(new InputSource(xmlUrl));
    isXMLValid = true;

    return isXMLValid;
}

From source file:com.cxf.sts.WSO2STSTest.java

private static Element loadPolicy(String xmlPath) {
    try {/*from   ww w .ja  va2s  . co m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document d = db.parse(new File(xmlPath));
        return d.getDocumentElement();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}