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 loadXMLFromString(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

public static Document createNewDocument() throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder.newDocument();
}

From source file:Main.java

public static final Document createDocument(boolean standalone, String xmlVersion) {
    Document result = null;/*from w  w  w  .  j av  a2s .c  om*/

    try {

        result = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        result.setXmlStandalone(standalone);
        result.setXmlVersion(xmlVersion);
    } catch (ParserConfigurationException e) {

        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static Document convertStringToXmlDocument(String xmlString) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;/*  w  w  w  . j a v  a 2s  .  co  m*/
    try {
        factory.setNamespaceAware(true);
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document createDocument() {
    // creo la factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // setX (che tipo di document builder ci deve produrre)

    dbf.setNamespaceAware(false);//from   w  w  w . j  a  v a 2s  .co  m
    // se metti a true la valiazione il parser fa la validazione (prende la dtd associata e valida l'xml)
    dbf.setValidating(false);
    try {
        // dalla factory creo il DocumentBuilder
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.newDocument();
    } catch (ParserConfigurationException ex) {
        System.err.println("Impossibile creare il parser XML!");
    }

    return null;
}

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  w  w  . 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 static Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from w  ww  .  j a  va 2 s.c om
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

        public void error(SAXParseException ex) throws SAXException {
            throw ex;
        }

        public void fatalError(SAXParseException ex) throws SAXException {
            throw ex;
        }
    });
    return documentBuilder.parse(xml);
}

From source file:Main.java

public static Document CreateDocument() {
    try {/*from  ww  w  .j  av  a 2  s. c om*/
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (FactoryConfigurationError e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static synchronized Element createRootElement() {
    Element rootElement = null;/* w ww .  j  ava2s. c  o  m*/
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.newDocument();
        rootElement = doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootElement;
}

From source file:Main.java

public static Document createDocument() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;/*  w  w  w  . ja  v a 2s  .  com*/
    Document document = null;
    try {
        db = dbf.newDocumentBuilder();
        document = db.newDocument();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return document;
}