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 loadXml(String filename) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(filename);
    return document;
}

From source file:Main.java

public static Document loadXml(String xml) {
    InputSource inputSource = new InputSource(new StringReader(xml));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {/*from  ww  w  . ja  va2 s .  c o m*/
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputSource);
        return doc;
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

From source file:Main.java

public static org.w3c.dom.Document configureJDomDocFromResource(String fileName) {
    org.w3c.dom.Document doc = null;
    InputStream in = ClassLoader.getSystemResourceAsStream(fileName);

    if (in != null) {
        try {//  w ww.j  a  va2 s  . co m
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(false);
            doc = factory.newDocumentBuilder().parse(in);
        } catch (Exception e) {
            //                Debug.error(e);
        }
    } else {
        //            Debug.error("could not find file " + fileName);
    }

    return doc;
}

From source file:Main.java

public static Document creatXMLFile() throws ParserConfigurationException {
    Document doc = null;//from   w  w w  . j  a  v a  2  s  . c om

    // Create instance of DocumentBuilderFactory
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // Get the DocumentBuilder
    final DocumentBuilder parser = factory.newDocumentBuilder();
    // Create blank DOM Document

    doc = parser.newDocument();

    return doc;
}

From source file:Main.java

public static void ReadXMLFile()

{

    try {//www.  j  av  a2 s. c o  m

        File fXmlFile = new File("D:\\FAR_Documents\\__Startamap\\Home.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("Staff id : " + eElement.getAttribute("id"));
                System.out.println(
                        "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println(
                        "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println(
                        "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                System.out.println(
                        "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

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

From source file:Main.java

/**
 * Cria um documento XML vazio/*ww  w  .  j  av  a 2s. com*/
 * @return Document -> novo documento XML 
 * @throws Exception
 */
public static Document criarDocumentoXMl() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    return doc;
}

From source file:Main.java

private static Document buildDocument(InputStream in) throws Exception {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document ret = null;//from w  w w.  j  a v  a2  s .  co  m

    factory = DocumentBuilderFactory.newInstance();
    builder = factory.newDocumentBuilder();

    ret = builder.parse(new InputSource(in));
    return ret;
}

From source file:Main.java

/**
 * Creates new {@link DocumentBuilder}.//www  . j a  v  a2 s.c  om
 * 
 * @return new {@link DocumentBuilder}.
 * @throws AnalyzerConfigurationException
 */
public static DocumentBuilder newBuilder() throws ParserConfigurationException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder();
}

From source file:Main.java

public static Document createDocument() {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);/* w  w w  .j  a va  2 s.c  om*/
    DocumentBuilder dBuilder;
    try {
        dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Could not create XML document", e);
    }
}

From source file:Main.java

public static Object getBean(String className) {
    try {/*from   w w w  .jav a  2s.c om*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName(className);
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();

        Class c = Class.forName(chartType);
        return c.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}