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 String getDefaultNamespaceUri(URL xmlResource, String rootElementName)
        throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);/* w w  w  . j  av a 2s .c o  m*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(xmlResource.openStream());
    NodeList nodes = document.getElementsByTagName(rootElementName);
    if (nodes == null || nodes.getLength() == 0) {
        throw new IllegalArgumentException("Root element \"" + rootElementName + "\" not found in xml \""
                + xmlResource.toExternalForm() + "\".");
    }
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        Node xmlns = node.getAttributes().getNamedItem("xmlns");
        if (xmlns != null) {
            String value = xmlns.getNodeValue();
            return value.substring(value.indexOf("=") + 1);
        }
    }
    return null;
}

From source file:Main.java

public static Document getDocument(String fileName) {
    Document document = null;//from  ww w . j  ava  2  s.com
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.parse(fileName);

    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return document;
}

From source file:Main.java

public static Document buildDocumentWithRootElement() {

    try {//from  ww w  .j  a  v a2  s.c om
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        docBuilder = docFactory.newDocumentBuilder();
        doc = docBuilder.newDocument();
        Element rootElement = doc.createElement(rootElementName);
        doc.appendChild(rootElement);
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return doc;
}

From source file:Main.java

public static Document fromXML(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w w w  . j  a  v a  2 s  .com
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

/**
 * Gets the document builder factory, creating it if necessary.
 * /* w ww.j  a v  a  2 s  .  co m*/
 * @return the document builder factory.
 */
public static synchronized DocumentBuilderFactory getDocumentBuilderFactory() {
    if (documentBuilderFactory == null) {
        documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
    }
    return documentBuilderFactory;
}

From source file:Main.java

public static Document fromString(String text) {
    try {/*from  w  w  w  . j  a va 2  s.  c o  m*/

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(text);

        doc.getDocumentElement().normalize();

        return doc;

    } catch (IOException | ParserConfigurationException | SAXException e) {

    }

    return null;
}

From source file:Main.java

/**
 * Parses the File into a DOM Document./*from  www  .  j  ava  2 s .  c  om*/
 * @param file the file to parse
 * @return Document - the parsed DOM Document
 */
public static Document getDocument(File file) {
    DocumentBuilder builder;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.parse(file);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Document read(String filename) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);/*from w w w  . ja v a  2 s  .co  m*/
    DocumentBuilder builder = null;
    Document document = null;
    try {
        builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        document = builder.parse(new File(filename));
        document.getDocumentElement().normalize();
    } catch (Exception e) {
    }
    return document;
}

From source file:Main.java

private static Document getDocument(String xml) {
    try {/*  ww  w  . j a v a  2  s  . c o  m*/
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        return null;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}