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 boolean validateXmlByDtd(String xmlPath)
        throws FileNotFoundException, SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);//from  ww  w .  j a v a 2s  . c  o m
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.parse(new File(xmlPath));
    return true;
}

From source file:Main.java

public static Document string2Document(String xml, String encode)
        throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException {
    Document document = null;//from  w w  w .jav a2s. c  om

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    try {
        document = builder.parse(new ByteArrayInputStream(xml.getBytes(encode)));
    } catch (Exception e) {
    }

    return document;
}

From source file:Main.java

public static Object getBean(String filePath) {
    try {/*ww w. j a  v a2 s.  c  o m*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File(filePath));

        NodeList nl = doc.getElementsByTagName("className");
        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();
        System.out.println(cName);
        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Document createXML() {
    try {/*from  w w  w. ja va 2 s  .c  o m*/
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        return docBuilder.newDocument();
    } catch (ParserConfigurationException ex) {
        return null;
    }
}

From source file:Main.java

public static DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory)
        throws ParserConfigurationException {
    return factory.newDocumentBuilder();
}

From source file:Main.java

public static Document getDocumentFromFile(String absolutePath) throws Exception {
    DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder();

    Document oDocument = null;/*  www.  j a  v a 2  s  .c o m*/

    oDocument = oDocumentBuilder.parse(new File(absolutePath));

    return oDocument;
}

From source file:Main.java

public static String elementToString(final Node node) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final Document document = factory.newDocumentBuilder().newDocument();
    final Node importedNode = document.importNode(node, true);
    document.appendChild(importedNode);//from   w  w w.j  av a  2 s .  c  o m
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(bos));
    return bos.toString("UTF-8");
}

From source file:Main.java

/**
 * /* ww  w. ja v  a  2 s.c om*/
 * <B>Purpose:</B> Parses the String to an XML Document Object
 * 
 * @param xml
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document parse(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xml)));

}

From source file:Main.java

public static Document createDocument() {
    Document document = null;//from  w  ww .j  av a 2s  .  c  o m

    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.newDocument();
    } catch (ParserConfigurationException ex) {
        // Ignore parser configuration errors
    }

    return document;
}

From source file:Main.java

/**
 * Parse an XML document./*w  ww.ja va 2 s .com*/
 * 
 * @param in xml input stream
 * @return document
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document parseXml(InputStream in) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder.parse(in);
}