Example usage for javax.xml.parsers DocumentBuilder parse

List of usage examples for javax.xml.parsers DocumentBuilder parse

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder parse.

Prototype


public abstract Document parse(InputSource is) throws SAXException, IOException;

Source Link

Document

Parse the content of the given input source as an XML document and return a new DOM Document object.

Usage

From source file:Main.java

public static String getCharttype() {
    try {/*  ww w . ja  v  a  2 s.  com*/
        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 createDoc(String xml, String encoding)
        throws ParserConfigurationException, SAXException, IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes(encoding));

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w  w w  .j a va  2  s .c  o  m

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(in);
    return doc;
}

From source file:Main.java

public static Document readDocument(String filePath)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(filePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    return document;
}

From source file:Main.java

/**
 * @param xmlURL is a URL to an XML document
 * @return the Document contained in the content at tha tURL
 *//*w ww .  ja v a  2s.c  o  m*/
public static Document readURL(String xmlURL) {
    ensureFactory();
    try {
        URL u = new URL(xmlURL);
        DocumentBuilder builder = mFactory.newDocumentBuilder();
        return builder.parse(u.openStream());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void indentXmlFile(File file) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w  w  w .j  a  v  a2 s.  c om*/
    DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
    Document doc = documentBuilder.parse(file);
    OutputFormat format = new OutputFormat(doc);
    format.setLineWidth(80);
    format.setIndenting(true);
    format.setIndent(4);
    XMLSerializer serializer = new XMLSerializer(new FileWriter(file), format);
    serializer.serialize(doc);
}

From source file:Main.java

public static Document String2XML(String xmlStr) {
    try {//w w w .j  a  va 2 s.  c  o  m
        StringReader sr = new StringReader(xmlStr);
        InputSource is = new InputSource(sr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(is);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static void validate(String xmlFileName, String schemaFileName)
        throws IOException, ParserConfigurationException, SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(new File(schemaFileName));
    Validator validator = schema.newValidator();

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder parser = builderFactory.newDocumentBuilder();
    Document document = parser.parse(new File(xmlFileName));
    validator.validate(new DOMSource(document));
}

From source file:Main.java

/**
 * Returns a new DOM document for the given URI.
 * // w w  w.  ja v a  2 s .  c om
 * @param uri
 *            URI to parse into the document.
 * @return Returns a new DOM document for the given URI.
 */
public static Document loadDocument(String uri) {
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        return docBuilder.parse(uri);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Node asDOMNode(String xml) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
    return doc.getFirstChild();
}

From source file:Main.java

public static Document createXmlDoc(File file) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(file);
    return doc;/* ww  w .ja v a2 s.c o m*/
}