Example usage for javax.xml.parsers DocumentBuilderFactory newInstance

List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory newInstance.

Prototype

public static DocumentBuilderFactory newInstance() 

Source Link

Document

Obtain a new instance of a DocumentBuilderFactory .

Usage

From source file:Main.java

public static Document getDocument(InputStream xml) throws java.io.IOException, org.xml.sax.SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    DocumentBuilder db;// ww  w .j  a  va 2s  .com
    try {
        db = dbf.newDocumentBuilder();
    } catch (javax.xml.parsers.ParserConfigurationException e) {
        throw new RuntimeException("Parser misconfiguration");
    }

    Document doc = db.parse(xml);
    return doc;
}

From source file:Main.java

public static boolean isValidXml(String xml) {
    try {/*from  w  w w . j  av a 2 s .  c  om*/
        StringReader reader = new StringReader(xml);
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        db.parse(new InputSource(reader));
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static Document parse(InputStream is) throws IOException {
    try {/*www .j  a va  2  s  .  c o  m*/
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        return builder.parse(is);
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static NodeList readXML(File xmlfile, String tag) throws Exception {
    NodeList list = null;/*from   w w  w .java  2 s  . c  o  m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xmlfile);
    Element rootElement = doc.getDocumentElement();
    list = rootElement.getElementsByTagName(tag);

    return list;
}

From source file:Main.java

public static Document createNewXMLDocument() throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();

    return document;
}

From source file:Main.java

/**
 * @return New XML document from the default document builder factory.
 *///from   w  ww . j a v  a  2  s.c o m
private static Document createDocument() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return builder.newDocument();
}

From source file:Main.java

public static Document domFromString(String in) {
    try {/*from w  w  w.ja v  a  2  s.  com*/
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(in));
        return db.parse(is);
    } catch (ParserConfigurationException | SAXException | IOException ex) {
        throw new RuntimeException("Unable to parse xml.", ex);
    }
}

From source file:Main.java

/**
 * Factory method to create a new XML document object.
 * @return// ww  w  .j a  v  a 2s.  c  o  m
 */
public static final Document newXmlDocument() throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document xmlDocument = builder.newDocument();

    return xmlDocument;
}

From source file:Main.java

public static Document parseXml(String xml) {
    try {//  www .j a v  a  2  s.  co 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);
    }
}

From source file:Main.java

public static DocumentBuilder getDocumentBuilder(boolean NamespaceAwareness)
        throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(NamespaceAwareness);
    // Unfortunately we can not ignore whitespaces without a schema.
    // So we use the NdLst workaround for now.
    //domFactory.setValidating(true);
    //domFactory.setIgnoringElementContentWhitespace( true );
    domFactory.setAttribute("http://apache.org/xml/features/dom/include-ignorable-whitespace", Boolean.FALSE);

    return domFactory.newDocumentBuilder();
}