Example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

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

Introduction

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

Prototype


public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;

Source Link

Document

Creates a new instance of a javax.xml.parsers.DocumentBuilder using the currently configured parameters.

Usage

From source file:Main.java

public static Document addXMLItems(File xmlfile, String tag) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc;//from   w ww.  j  av a 2 s  .c om
    if (xmlfile.length() == 0) {
        doc = builder.newDocument();
        Element rootElement = doc.createElement(tag + "s");
        doc.appendChild(rootElement);
    } else {
        doc = builder.parse(xmlfile);
    }
    return doc;
}

From source file:Main.java

public synchronized static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    return factory.newDocumentBuilder();
}

From source file:Main.java

public static Document newDomDocument() throws IOException {
    try {//from  w ww.  j  a va 2s  .  c o m
        DocumentBuilderFactory factory = newDocumentBuilderFactory();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new IOException("Can't create new DOM-document.", e);
    }
}

From source file:Main.java

/**
 * @param .Should be followed by createRoot()
 * @return Creates a new Document that contains XML
 * @throws ParserConfigurationException/*  w  ww  .  j  a  v  a2s . c  o m*/
 * @throws SAXException
 * @throws IOException
 */
public static Document createDocument(String version, String encoding)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document parse = builder.newDocument();
    parse.setXmlVersion(version);
    return parse;
}

From source file:Main.java

public static Document createDocument(File file) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new InputSource(new FileInputStream(file)));
    return doc;/*from   w ww .ja v a  2  s.co m*/
}

From source file:Main.java

public static Document createDocument() throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    return doc;// w w w.jav a  2s. c  om
}

From source file:Main.java

public static Document getXmlDocument(String path)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document document = builder.parse(new FileInputStream(path));

    return document;
}

From source file:Main.java

public static String getRequestID(String xml) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("request-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else//from w w  w.  j  a  v  a 2  s. co  m
        return null;
}

From source file:Main.java

public static Document load(String filename) {
    Document document = null;/*from   ww w. j a v a 2  s  . c o m*/
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new File(filename));
        document.normalize();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return document;
}

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;//  ww  w.  java2  s  . c o m
}