Example usage for javax.xml.parsers ParserConfigurationException printStackTrace

List of usage examples for javax.xml.parsers ParserConfigurationException printStackTrace

Introduction

In this page you can find the example usage for javax.xml.parsers ParserConfigurationException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

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

From source file:Main.java

/**
 * Parses a document from the given string
 * /*from ww w. j  av  a 2  s  . c o  m*/
 * @param template The string to parse
 * @return The parsed {@link Document}
 */
public static Document getTemplateDocument(String template) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    dbf.setValidating(false);
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
                return new InputSource(new StringReader(""));
            }
        });
        Document doc = db.parse(new ByteArrayInputStream(template.getBytes("utf8")));
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document createXMLDocument() {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);/*from w ww .j a  va2s  . co m*/
    DocumentBuilder docBuilder = null;
    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Document doc = docBuilder.newDocument();
    return doc;
}

From source file:Main.java

/**
 * Creates W3C DOM Document object from XML file
 * @param filePath path to xml file including file name and extension
 *//*from  w ww. j  av  a2 s  .c  o  m*/
public static Document parseXml(String filePath) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(filePath);
        doc.getDocumentElement().normalize();
        return doc;
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
        return null;
    } catch (SAXException se) {
        se.printStackTrace();
        return null;
    } catch (IOException ioe) {
        //ioe.printStackTrace();
        String toRemove = File.separator + "WebApp" + File.separator;
        Document doc;
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(filePath.replace(toRemove, File.separator));
            doc.getDocumentElement().normalize();
            return doc;
        } catch (SAXException saxex) {
            saxex.printStackTrace();
            return null;
        } catch (ParserConfigurationException pcex) {
            pcex.printStackTrace();
            return null;
        } catch (IOException ioex) {
            ioex.printStackTrace();
            return null;
        }
    }
}

From source file:Main.java

public static Node loadURL(java.net.URL url) {
    Document document = null;/*from w  w  w .j a v  a2 s . co  m*/
    if (url != null) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(url.openStream());
        } catch (java.io.IOException ioe) {
            document = null;
            ioe.printStackTrace();
        } catch (ParserConfigurationException pce) {
            document = null;
            pce.printStackTrace();
        } catch (org.xml.sax.SAXException se) {
            document = null;
            se.printStackTrace();
        }

        return document;
    }
    return null;
}

From source file:Main.java

public static Document buildDocumentWithRootElement() {

    try {//ww w.java 2  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

/**
 * This method parses an XML file and returns an {@code org.w3c.dom.Document}
 * @param f// w w  w  .j a v a  2  s. c om
 * @return
 */
public static Document parseXMLFile(File f) {
    Document dom = null;
    //      get the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        //Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        //parse using builder to get DOM representation of the XML file
        dom = db.parse(f);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (SAXException se) {
        se.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return dom;
}

From source file:Utils.java

public static Document newDocumentFromString(String xmlString) {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document ret = null;//w  w w.j a va2s. c  om

    try {
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    try {
        ret = builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:Utils.java

public static Document newDocumentFromInputStream(InputStream in) {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document ret = null;//from  w  w  w .  ja v  a2 s.  c  o m

    try {
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    try {
        ret = builder.parse(new InputSource(in));
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:Main.java

public static Document getDoc(String xml) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;//from w w  w. ja  va 2  s.  c  o  m

    dbf.setValidating(false);
    try {
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        db = dbf.newDocumentBuilder();
        InputSource source = new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")));
        Document doc = db.parse(source);
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}