Example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder.

Prototype


public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;

Source Link

Document

Creates a new instance of a javax.xml.parsers.DocumentBuilder using the currently configured parameters.

Usage

From source file:Main.java

/**
 * Uses the DocumentBuilderFactory to create a new doc and returns it
 *
 * @return Document object called doc//from   w ww. j a v  a 2 s . c om
 */
public static Document createDoc() throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.newDocument();
}

From source file:Main.java

public static Document getXMLDoc(String root) throws ParserConfigurationException {
    Document xmldoc = null;/* w  w  w .ja v a2 s.co  m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    // Document.
    xmldoc = impl.createDocument(null, root, null);
    return xmldoc;
}

From source file:Main.java

static void getHTTPXml(URL url) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("ACCEPT", "application/xml");
    InputStream xml = conn.getInputStream();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(xml);

    System.out.println(document);
    String doctype = conn.getContentType();
    System.out.println(doctype);//from   w  w  w . j a v a  2 s.com

    XPathFactory pathFactory = XPathFactory.newInstance();
    XPath path = pathFactory.newXPath();
    XPathExpression expression;
    expression = path.compile("/result/checkid");
    NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
    String checkids[] = getNodeValue(nodeList);
    for (String checkid : checkids) {
        System.out.print(checkid + ", ");
    }
    conn.disconnect();
}

From source file:Main.java

public static Document createDocument() throws ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    docFactory.setNamespaceAware(true);// ww  w .  ja  v  a 2 s.c  om
    return docBuilder.newDocument();

}

From source file:Main.java

public static org.w3c.dom.Document getDocument(File file)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(file);
}

From source file:Main.java

public static String getXmlContent() throws ParserConfigurationException, SAXException, IOException {
    File file = new File("src/main/resources/tesco/data/input/priceChange.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(file);

    return doc.toString();
}

From source file:Main.java

private static Document parseXmlFile(String in) {
    try {//  ww  w  . j  a va2s.  c  om
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Returns Document object of the xml located at the given path
 * /*  w w w .j  a v a2 s.c  om*/
 * @param path path to xml
 * @return Document object
 * @throws ParserConfigurationException If parsing error occurs
 * @throws SAXException If parsing error occurs
 * @throws IOException If IO error occurs.
 */
public static Document getXMLDocument(String path)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(path);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

public static Document getStringToDocument(String xml)
        throws TransformerException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbFactory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document;
}

From source file:Main.java

public static Document getDocument(String xml) {
    if (xml == null || xml.indexOf("<?xml") < 0)
        return null;
    try {//from  w  w w. j  ava 2 s.c  om
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}