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 String getRequestName(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-name").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else//from   w w  w . j a  v  a  2 s . c o m
        return null;
}

From source file:Main.java

public static Document createDocument() {
    Document document;/*from   w w  w . j  a v a 2  s .c  o m*/

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.newDocument();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }

    return document;
}

From source file:Main.java

public static String getUnicastID(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("unicast-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else/*from www.  java  2 s  .c  o  m*/
        return null;
}

From source file:Main.java

public static Document parse(InputStream is) throws IOException {
    try {//  w  w  w.j  ava  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 Document createDocument(String mainType, String customType) throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();
    document.setXmlStandalone(false);//from ww  w  . ja  va2 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

public static Document open(IFile file, IProgressMonitor monitor) throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    if (!file.exists()) {
        return dBuilder.newDocument();
    }//  w ww.j  a v a 2  s . c  om
    try {
        return dBuilder.parse(file.getContents());
    } catch (Exception e) {
        return dBuilder.newDocument();
    }
}

From source file:Main.java

public static String toXMLString(String fileName) throws Exception {
    File file = new File(fileName);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return toXMLString(doc);

}

From source file:Main.java

/**
 * Parse XML/*from  w  w  w .ja v  a2s  .  co m*/
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * */
public static Document parseXMLFromString(String str)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new ByteArrayInputStream(str.getBytes()));
    return doc;
}

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// ww  w.j  ava  2 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

private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
    // SAXParsers are not concurrency compatible, so always return a new instance to prevent thread issues 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from   w  w w . j  a v a  2s  .  co  m
    return dbf.newDocumentBuilder();
}