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

private static void updateNode(String nodeName, String value) throws Exception {
    File f = new File(System.getProperty("user.dir") + "\\DBConf.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(f);
    Node tempElem = doc.getElementsByTagName(nodeName).item(0);
    tempElem.getFirstChild().setNodeValue(value);
    doc.getElementsByTagName("conf").item(0).appendChild(tempElem);
    DOMSource source = new DOMSource(doc);
    StreamResult res = new StreamResult(new FileOutputStream(f));
    TransformerFactory.newInstance().newTransformer().transform(source, res);

}

From source file:Main.java

public static Document readFileAsDoc(String xml) throws SAXException, IOException, Exception {
    org.w3c.dom.Document doc = null;
    StringReader sr = new StringReader(xml);
    DocumentBuilder bldr = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    doc = bldr.parse(new InputSource(sr));

    return doc;//  w  ww.  jav a 2 s .co  m
}

From source file:Main.java

public static Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {//  w  w  w  .j a  v a 2  s.c  o m
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document parse(InputSource iSource)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
    return builder.parse(iSource);
}

From source file:Main.java

public static Document createDoc(final InputStream docFile)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory fabriqueD = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = fabriqueD.newDocumentBuilder();
    final Document document = builder.parse(docFile);
    return document;
}

From source file:Main.java

public static Document getDocument(InputStream xmlIs) {
    docFactory = getInstance();/* ww w  .j  ava  2  s .  c om*/
    Document document = null;
    try {
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        document = builder.parse(xmlIs);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return document;
}

From source file:Main.java

public static Document getDocument(String xmlStr) {
    docFactory = getInstance();//from  ww w.  j  a  va2  s  .  c o  m
    Document document = null;
    try {
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes()));
        document.getDocumentElement().normalize();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return document;
}

From source file:Main.java

public static Document getdoc(String uri) throws Exception {
    uri = uri.replace(" ", "%20");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();//ww  w .  j  a v a2 s  .  c om
    Document doc = db.newDocument();
    doc = db.parse(new URL(uri).openStream());
    if (doc == null || doc.getTextContent() == "") {
        throw new Exception();
    } else
        return doc;
}

From source file:Main.java

/**
 * Uses the current DocumentBuilderFactory to converts a String
 * representation of XML into a Document.
 *
 * @param xml XML serialized as a String
 * @param nameSpaceAware determines whether the constructed Document
 * is name-space aware//  ww  w .ja  v  a2 s. c  o  m
 * @return Document
 */
public static Document stringToDocument(String xml, boolean nameSpaceAware) {
    Document document = null;
    try {
        if (xml != null) {
            StringReader reader = new StringReader(xml);
            InputSource input = new InputSource(reader);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(nameSpaceAware);
            factory.setValidating(false);
            DocumentBuilder builder = factory.newDocumentBuilder();

            document = builder.parse(input);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return document;
}

From source file:Main.java

public static Document createDoc(final File XMLFile)
        throws ParserConfigurationException, SAXException, IOException {
    // Creation of the DOM source
    final DocumentBuilderFactory fabriqueD = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = fabriqueD.newDocumentBuilder();
    final Document document = builder.parse(XMLFile);

    return document;
}