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

private static void getDocument(String xslName) {
    try {/*from w  ww .  j  av a  2 s .c om*/
        URL xslUri = new URL(xslName);
        InputStream xslIs = xslUri.openStream();
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        xsl = dBuilder.parse(xslIs);
        xsl.getDocumentElement().normalize();
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:Main.java

/**
 * Convert the byte array representation of an Element into an Element
 * @param byte[] representation of an Element.
 * @param boolean set whether the return Element should be namespace aware.
 * @return Element/*from   w w  w  .j  av a2s . c  o  m*/
 */
public static Element byteArrayToElement(byte[] b, boolean namespaceAware)
        throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(namespaceAware);
    docBuilderFactory.setValidating(false);
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new ByteArrayInputStream(b));
    return doc.getDocumentElement();
}

From source file:Main.java

public static Node getRoot(String documentAsString) {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {//from   w ww.j  a  v  a  2  s.c o m
        final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        final Document document = documentBuilder.parse(new InputSource(new StringReader(documentAsString)));
        return document.getFirstChild();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    } catch (SAXException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static boolean validProcess(String xml) {
    try {//from   w  ww. jav a  2  s . com
        Set<String> classifiers = new HashSet<String>();

        InputStream is = new ByteArrayInputStream(xml.getBytes());
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(is);

        // iterate over all operators
        NodeList nodes = document.getElementsByTagName("operator");
        for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            String className = element.getAttribute("class");

            if (classifiers.contains(className)) {
                return false;
            }
            classifiers.add(className);
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Parse une string contenant un document XML dans un DOM Element.
 * // w w w. ja v  a  2 s. co  m
 * @param xml le document XML sous forme de string
 * @return l'element racine du document (root element), ou null si erreur
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public static Element stringToDomElement(final String xml)
        throws IOException, SAXException, ParserConfigurationException {
    Element element = null;
    if (xml != null) {
        final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final Document doc = db.parse(new InputSource(new StringReader(xml)));
        element = doc.getDocumentElement();
    }
    return element;
}

From source file:Main.java

public static Document parse(String uri)
        throws TransformerException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilder db = documentBuilder();
    try {//from   w w  w.  ja  va2s  .  co m
        return db.parse(uri);
    } finally {
        db.reset();
    }
}

From source file:Main.java

public static Document parse(InputStream stream)
        throws TransformerException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilder db = documentBuilder();
    try {//from  w  w  w .j  a  v a2s  . c  o  m
        return db.parse(stream);
    } finally {
        db.reset();
    }
}

From source file:Main.java

public static Document parse(File f)
        throws TransformerException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilder db = documentBuilder();
    try {//from  ww w .  jav  a2 s . c  om
        return db.parse(f);
    } finally {
        db.reset();
    }
}

From source file:Main.java

public static Document buildAndParseDocument(String in) {
    try {/*from   ww w .java  2s  . c  o  m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document getDocumentFromFile(String absolutePath) throws Exception {
    DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder();

    Document oDocument = null;//w  w w.j  ava 2  s  .  co m

    oDocument = oDocumentBuilder.parse(new File(absolutePath));

    return oDocument;
}