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

/**
 * Convenience method to parse an input stream into a document
 * @param inputStream/*from w w  w  . java  2  s . c  o m*/
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document getDocument(InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(inputStream);
}

From source file:Main.java

/**
 * Parse XML/* w ww .  j  a v  a2s.c  om*/
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * */
public static Document parseXMLFromInputStream(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new InputSource(new InputStreamReader(is)));
    return doc;
}

From source file:Main.java

public static Document getDocumentFromString(String xml) {
    Document retVal = null;/*from www  . ja  v  a2s.com*/
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        retVal = db.parse(getInputSourceFromString(xml));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return retVal;
}

From source file:Main.java

public static Document load(String uri) {
    Document dom = null;/*from  w  w  w  .  jav a  2 s .c  o m*/
    try {
        DocumentBuilder db = newDocumentBuilder();
        dom = db.parse(uri);
    } catch (Exception e) {
    }
    return dom;
}

From source file:Main.java

public static Document stringToDocument(String assumedXml)
        throws ParserConfigurationException, SAXException, IOException {
    Document doc = null;// www  .ja  v  a 2s  .c  om
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    doc = builder.parse(new InputSource(new ByteArrayInputStream(assumedXml.getBytes())));

    return doc;
}

From source file:Main.java

/**
 * Parsing xml doc text as w3c dom// w w  w. j  a v a 2 s . co m
 *
 * @param xmlString xml doc as string
 * @return W3C DOM
 */
public static Document parseNSAware(String xmlString) {
    try {
        InputSource xmlInputSource = new InputSource(new StringReader(xmlString));
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.parse(xmlInputSource);
    } catch (Exception ex) {
        throw new RuntimeException("Error when parsing Xml doc", ex);
    }
}

From source file:Main.java

/**
 * Returns a {@link org.w3c.dom.Document} from an {@link java.io.InputStream}.
 * @param inputStream the stream from which getting the XML content
 * @return the XML document or null if //from  w  w  w  . j av  a 2s.  c  o m
 * @throws IOException if there was an error while getting the XML from the stream
 * @throws SAXException if there was an error while parsing the XML document
 * @throws javax.xml.parsers.ParserConfigurationException
 */
public static Document getDocument(InputStream inputStream)
        throws IOException, SAXException, ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(inputStream);
}

From source file:Main.java

public static Document stringToDocument(String xml) {
    Document doc = null;/*  w ww .  j a  va  2  s.c  o m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // Turn on validation, and turn off namespaces
    factory.setValidating(false);
    factory.setNamespaceAware(false);

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return doc;
}

From source file:Main.java

public static String getBrandName() {
    try {// w  w  w.  java  2s . c o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File("configTV.xml"));

        NodeList nl = doc.getElementsByTagName("Name");
        Node classNode = nl.item(0).getFirstChild();
        String Name = classNode.getNodeValue().trim();
        return Name;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Document parseXml(String xml) {
    try {/*from   w  w w  .  j a va  2s .  c  o  m*/
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        setUpSecurity(dbFactory);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.parse(new InputSource(new StringReader(xml)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}