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:jp.go.nict.langrid.commons.jxpath.JXPathUtil.java

/**
 * /*  ww w.jav a 2s  .  co m*/
 * 
 */
public static JXPathContext newXMLContext(String anXml) throws IOException, SAXException {
    DocumentBuilder builder = DocumentUtil.newDocumentBuilder();
    return JXPathContext.newContext(builder.parse(new InputSource(new StringReader(anXml))));
}

From source file:jp.go.nict.langrid.commons.jxpath.JXPathUtil.java

/**
 * /* w  w w .java2 s  .co m*/
 * 
 */
public static JXPathContext newXMLContext(InputStream anInputStream) throws IOException, SAXException {
    DocumentBuilder builder = DocumentUtil.newDocumentBuilder();
    return JXPathContext.newContext(builder.parse(anInputStream));
}

From source file:Main.java

public static Document createDomDocument(String fileName) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    Document document = null;/*from w w w. j ava2s . co  m*/
    try {
        File file = new File(fileName);

        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(file);
    } catch (SAXParseException spe) {
        spe.printStackTrace();
    } catch (SAXException se) {
        se.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

public static synchronized Element getRootElement(InputStream is) {
    if (is == null) {
        return null;
    }/*from w ww . ja v a  2 s .  c  o m*/
    Element rootElement = null;
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.parse(is);
        rootElement = doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootElement;
}

From source file:Main.java

public static List<String> getNodeValues(String response, String tagName)
        throws ParserConfigurationException, SAXException {
    try {//from   ww w .j a v a 2s .  c om
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(response.getBytes()));

        Element rootElement = document.getDocumentElement();

        List<String> arrayList = new ArrayList<>();
        NodeList list = rootElement.getElementsByTagName(tagName);
        NodeList subList;
        if (list != null && list.getLength() > 0) {
            for (int k = 0; k < list.getLength(); k++) {
                subList = list.item(k).getChildNodes();
                if (subList != null && subList.getLength() > 0) {
                    arrayList.add(subList.item(0).getNodeValue());
                }
            }
            return arrayList;
        }
    } catch (Exception e) {
        return null;
    }
    return null;
}

From source file:Main.java

public static Document loadXMLFromString(String xml)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

/**
 * Read input stream into XML Document element
 * /*w  w w .  ja v  a 2  s.c o m*/
 * @param is
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document readXML(InputStream is) throws ParserConfigurationException, SAXException, IOException {
    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();

    org.w3c.dom.Document doc = db.parse(is);

    return doc;
}

From source file:Main.java

/**
 * Read a XML text file to XML Document object
 *  //  w w w  .  ja  v  a2s .c o  m
 * @param xmlFile
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document readXML(String xmlFile) throws ParserConfigurationException, SAXException, IOException {
    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();

    org.w3c.dom.Document doc = db.parse(xmlFile);

    return doc;
}

From source file:Main.java

static Document getLoadingDoc(InputStream in) throws SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setValidating(false);//from   w w w . ja va 2  s . c o  m
    dbf.setCoalescing(true);
    dbf.setIgnoringComments(true);
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(in);
        return db.parse(is);
    } catch (ParserConfigurationException x) {
        throw new Error(x);
    }
}

From source file:Main.java

/**
 * Return DOM Document object for given xml file
 *
 * @param xmlFile// w  w  w  .ja v  a2s  . co m
 * @return
 * @throws Exception
 */
public static Document getDocument(String xmlFile) throws Exception {
    /* Get the instance of BuilderFactory class. */
    DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();

    /* Instantiate DocumentBuilder object. */
    DocumentBuilder docBuilder = builder.newDocumentBuilder();

    /* Get the Document object */
    Document document = docBuilder.parse(xmlFile);
    return document;
}