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

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

    return document;
}

From source file:Main.java

public static Document loadXml(File sourceFile) {
    try {//  w  ww  .  j  a va 2 s  . c  o  m
        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 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

/**
 *
 * @param obj//  ww  w  . j a  v a  2  s .  c  o  m
 * @return
 * @throws JAXBException
 * @throws ParserConfigurationException
 */
public static Document jaxbToDocument(Object obj) throws JAXBException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = factory.newDocumentBuilder();
    Document doc = db.newDocument();
    // Marshal the Object to a Document
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.marshal(obj, doc);
    return doc;
}

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;//  w  w w  .ja  v  a 2  s. c om
}

From source file:Main.java

private static Element getRootNodeFromXmlUrl(String xmlUrl) {
    Element element = null;//from   ww w.  j  a  v  a2 s .c  o m
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(xmlUrl);
        element = document.getDocumentElement();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return element;
}

From source file:Main.java

public static Document parseDocument(final InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document document = builder.parse(new InputSource(inputStream));
    return document;
}

From source file:Main.java

public static Document parseDocument(final Reader reader)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document document = builder.parse(new InputSource(reader));
    return document;
}

From source file:Main.java

public static Document createDocument(String rootElement) throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    Document document = dbFactory.newDocumentBuilder().newDocument();
    document.appendChild(document.createElement(rootElement));
    return document;
}

From source file:Main.java

public static Document xmlToDoc(String xml) {
    Document doc = null;/*from  w  w w .j av  a 2s  .co m*/
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        doc = docBuilder.parse(xml);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return doc;
}