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 Element stringToElement(String str) {
    StringReader sr = new StringReader(str);

    try {//from   w w w.j a v  a  2s  .  c om
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputSource is = new InputSource(sr);
        // is.setEncoding("gb2312");
        Document doc = docBuilder.parse(is);
        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static DocumentBuilderFactory newNamespaceAwareFactory() {
    /*//from w  w  w  .  j  av  a  2  s .co m
     * TODO return DocumentBuilderFactory.newInstance(
     * "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",
     * XmlHelper.class.getClassLoader());
     */
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    /*
     * Somewhat required for proper JAXB usage... Really? Why I've wrote
     * that? Default value is false and it seems to works like a charm
     * whatever the value.
     */
    factory.setNamespaceAware(true);
    return factory;
}

From source file:Main.java

/**
 * Carica il DOM a partire da un file contenente un documento XML
 * // w ww  . j a v a 2s .c  om
 * @throws Exception
 */
public static Document caricaDOMDocument(File fileXML) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document domDocument = builder.parse(fileXML);
    if (domDocument == null) {
        throw new Exception("Errore in lettura del Document del file XML di configurazione");
    }
    return domDocument;
}

From source file:Main.java

public static Node stringToNode(String string) {
    try {//from   ww w  .j a  va2 s .  c om
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return db.parse(new ByteArrayInputStream(string.getBytes())).getFirstChild();
    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

/**
 *
 * @return/*from  w ww  .  j  ava2 s  .c  o  m*/
 */
public static Document getNewDocument() {
    Document document = null;
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.newDocument();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

public static String format(String xml) throws Exception {
    InputSource src = new InputSource(new StringReader(xml));
    Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
    Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

    System.setProperty(DOMImplementationRegistry.PROPERTY,
            "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();

    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);
    return writer.writeToString(document);
}

From source file:Main.java

public static Document getDocumentFromString(String xml) {
    Document retVal = null;//from   ww  w  .  ja v  a2 s .  com
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        retVal = db.parse(getInputSourceFromString(xml));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return retVal;
}

From source file:Main.java

public static Document loadXml(File sourceFile) {
    try {//w w  w.  j av  a  2s.com
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document baseSizeDoc = dBuilder.parse(sourceFile);

        baseSizeDoc.getDocumentElement().normalize();

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

    throw new IllegalStateException("Could not load xml file: " + sourceFile.getPath());
}

From source file:Main.java

public static String getCharttype() {
    try {/*  w  ww . j  a  v  a2  s . c  o m*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName("chartType");
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();
        return chartType;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Document parseXML(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//w  w  w. j a v a2s  . co m
    DocumentBuilder db = factory.newDocumentBuilder();

    ByteArrayInputStream sr = new ByteArrayInputStream(xml.getBytes());
    try {
        Document document = db.parse(sr);
        return document;
    } finally {
        sr.close();
    }
}