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

/**
 * create new DOM document//from  w  w w  .  j  a v a2 s . c  om
 * @return empty DOM object
 * @throws ParserConfigurationException in case of error
 */
public static Document createDocument() throws ParserConfigurationException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
}

From source file:Main.java

public static Node parseString(String string) throws Exception {
    InputStream is = new ByteArrayInputStream(string.getBytes("UTF-16"));

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    // from AIMLProcessor.evalTemplate and AIMLProcessor.validTemplate:
    //   dbFactory.setIgnoringComments(true); // fix this
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    Node root = doc.getDocumentElement();
    return root;// w  ww .  j  a  va2  s. com
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    if (builder == null) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        builder = domFactory.newDocumentBuilder();
    }/*from w w w  .j a v  a 2 s. c  om*/
    return builder;
}

From source file:Main.java

public static Document createDocument(String mainType, String customType) throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();
    document.setXmlStandalone(false);/*from www .  j  a  v  a 2 s . c  o m*/
    Element sync = document.createElement("Sync");
    document.appendChild(sync);
    Element entity = document.createElement("Entity");
    entity.setAttribute("mainType", mainType);
    if (customType.length() > 0)
        entity.setAttribute("customType", customType);
    document.getElementsByTagName("Sync").item(0).appendChild(entity);
    return document;
}

From source file:Main.java

/**
 * Gets Document representation of xml file
 * /*w  w w  .jav a 2 s.c om*/
 * @param file
 *            xml file
 * @return Document object
 */
public static Document getXmlDocument(File file) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(file);
        return doc;
    } catch (Exception e) {
        throw new RuntimeException("Could not parse XML document", e);
    }
}

From source file:Main.java

/** Creates a new XML document.
 *
 * @param namespace the default namespace of the document
 * @param documentElement the name of the document element
 * @return a new XML document// w  w w  .  j a  va2  s .c  o  m
 */
public static Document createDocument(String namespace, String documentElement) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        return impl.createDocument(namespace, documentElement, null);
    } catch (ParserConfigurationException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document fileToXMLDoc(File file) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(file);

    doc.getDocumentElement().normalize();

    return doc;//from   w  w  w  .ja v a  2  s. c  o m
}

From source file:Main.java

public static Document loadXMLFromString(String theXML_URL) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;/*from  w  w  w .j  av  a  2  s .c om*/
    try {
        db = dbf.newDocumentBuilder();
        Document doc = db.parse(new URL(theXML_URL).openStream());
        return doc;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (MalformedURLException e) {
        return null;
    } catch (SAXException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Parse the supplied string into an xml document.
 * //from   w  ww .j a v  a 2s . c  o m
 * @param content
 * @return
 */
public static Document parseString(String content) {
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        //parse the stream into a document
        return documentBuilder.parse(new ByteArrayInputStream(content.getBytes()));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document noPrefixDomObjcet(StringBuffer data)
        throws SAXException, IOException, ParserConfigurationException {
    Document xmlDoc = null;/*from w  w  w. ja  v  a 2 s  .  c  o m*/
    xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new ByteArrayInputStream(data.toString().getBytes()));
    return xmlDoc;
}