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 Object getBean(String className) {
    try {//from   w w w. ja  v  a 2s  . c  o m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName(className);
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();

        Class c = Class.forName(chartType);
        return c.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Build a Document from a file//  ww  w. j  a v  a  2 s  . co  m
 *
 * @param xmlFile file
 * @return a Document object
 * @throws Exception if an error occurs
 */
public static Document buildDocumentFromFile(File xmlFile) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

/**
 * load a xml file from OS file system and interpret it into a Document no
 * charset limited/*w ww . j  a  va  2  s.co m*/
 * 
 * @param xmlfile
 * @return Document
 * @throws Exception
 */
public static Document load(String xmlfile) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(xmlfile);
}

From source file:Main.java

/**
 * load a xml file from OS file system and interpret it into a Document no
 * charset limited//from w ww  . j ava 2s.c o  m
 * 
 * @param xmlfile
 *            String
 * @return Document
 * @throws Exception
 */
public static Document load(File xmlfile) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(xmlfile);
}

From source file:Main.java

public static Document parseXMLFile(String file) {

    try {//from   ww w .j a  v a2s . c  o m
        File mappingsFile = new File(file);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(mappingsFile);
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document readXmlFromString(String data) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(new org.xml.sax.InputSource(new StringReader(data)));
    return document;
}

From source file:Main.java

/**
 * Build a document from a InputStream./*ww w .  j  av a  2s .  co  m*/
 *
 * @param is InputStream
 * @return a Document object
 * @throws Exception if an error occurs
 */
public static Document buildDocumentFromInputStream(InputStream is) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

private static Document createDocument() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(XMLSTRING.getBytes("ISO-8859-1")));
    return doc;/*from ww  w  .jav a  2 s.c o  m*/
}

From source file:Main.java

public static Document getNormalizedDocument(Reader input)
        throws ParserConfigurationException, SAXException, IOException {

    InputSource is = new InputSource(input);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from w w  w.  ja va2s.co  m
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document d = db.parse(is);
    return d;
    //      return (Document) Nodes.normalizeWhitespace(d); // FIXME: is this required??
}

From source file:Main.java

/**
 * Parses the XML contents specified stream.
 *
 * @param stream the XML stream.//from  w  w  w.j  a va  2  s  .c om
 * @return a DOM instance.
 * @throws ParserConfigurationException if XML parser is not
 *         properly configured.
 * @throws SAXException if XML is not well formed.
 * @throws IOException if any error happens processing the stream.
 */
public static Document loadDocument(final InputStream stream)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(stream);
}