Example usage for javax.xml.parsers DocumentBuilderFactory newInstance

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

Introduction

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

Prototype

public static DocumentBuilderFactory newInstance() 

Source Link

Document

Obtain a new instance of a DocumentBuilderFactory .

Usage

From source file:Main.java

public static Document getDocument(String xmlDocument) {
    try {//w w w .  jav a2 s .  co m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xmlDocument.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new RuntimeException("Error parsing XML document", e);
    }
}

From source file:Main.java

public static Document startXMLDocument(String _firstTag) {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    Document doc = null;//from  ww  w  . j ava 2  s.  c o m
    try {
        docBuilder = dbfac.newDocumentBuilder();
        doc = docBuilder.newDocument();

        Element xmlRoot = doc.createElement(_firstTag);
        doc.appendChild(xmlRoot);

    } catch (ParserConfigurationException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    }
    return doc;
}

From source file:Main.java

public static Node parseFile(String fileName) throws Exception {
    File file = new File(fileName);

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    // from AIMLProcessor.evalTemplate and AIMLProcessor.validTemplate:
    //   dbFactory.setIgnoringComments(true); // fix this
    Document doc = dBuilder.parse(file);
    doc.getDocumentElement().normalize();
    Node root = doc.getDocumentElement();
    return root;/*from www. j av  a 2s. c  om*/
}

From source file:Main.java

public static Object getBean(String filePath) {
    try {// ww  w .j a v a 2  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 createDocument(File file) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new InputSource(new FileInputStream(file)));
    return doc;/*from   w w w.ja  v  a2 s . co  m*/
}

From source file:Main.java

/** Parse a XML document from string */
public static Document parse(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

static public Document createDefaultMessage(String messagetype) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentbuilder = null;
    try {/*from   www .ja v a2 s .co m*/
        documentbuilder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }

    Document doc = documentbuilder.newDocument();
    Element root = doc.createElement("message");
    doc.appendChild(root);
    long timestamp = System.currentTimeMillis();
    root.setAttribute("timestamp", Long.toString(timestamp));
    root.setAttribute("type", messagetype);
    return doc;
}

From source file:Main.java

public static Document getEmptyDocument() {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;
    try {/*from   w w  w . j av  a 2s .c o  m*/
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        return null;
    }
    return documentBuilder.newDocument();
}

From source file:Main.java

public static Document getDOMMaker() throws ParserConfigurationException {
    //get an instance of factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //get an instance of builder
    DocumentBuilder db = dbf.newDocumentBuilder();
    //create an instance of DOM
    Document dom = db.newDocument();
    return dom;//from   www .j a v a 2s .  c  om
}

From source file:Main.java

public static Object getBean(String args) {
    try {/*from  w  w w . j a va 2  s.  com*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File("config.xml"));
        NodeList nl = null;
        Node classNode = null;
        String cName = null;
        nl = doc.getElementsByTagName(args);

        classNode = nl.item(0).getFirstChild();
        cName = classNode.getNodeValue();

        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}