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 createDocument() {
    try {/* w w w . j  a  v  a2s  . c om*/
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException ignored) {
    }
    return null;
}

From source file:Main.java

public static Document loadString(String paramString) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);/*www. j av a2s  .c o  m*/
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    char[] arrayOfChar = new char[paramString.length()];
    paramString.getChars(0, paramString.length(), arrayOfChar, 0);
    InputSource input = new InputSource(new CharArrayReader(arrayOfChar));
    return builder.parse(input);
}

From source file:Main.java

public static DocumentBuilder getBuilder() {
    if (builder == null)
        try {/*w w  w.  j a v a 2s .  c om*/
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO come up with some sort of recovery process
            e.printStackTrace();
        }
    return builder;
}

From source file:Main.java

public static Object getBean() {
    try {//  w ww.j  a va 2s.  c o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document document;
        document = builder.parse(new File("config.xml"));

        NodeList nl = document.getElementsByTagName("StrategyClassName");
        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();

        System.out.println(cName);
        Class c = Class.forName("com.seven.strategy.sort." + cName);
        Object object = c.newInstance();
        return object;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);//from  w w w.  j ava  2 s .  c  om
    setUpSecurity(dbFactory);
    return dbFactory.newDocumentBuilder();
}

From source file:Main.java

public static Object getBean() {
    try {//  www .j  a v a 2s. c om
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document document;
        document = builder.parse(new File("config.xml"));

        NodeList nl = document.getElementsByTagName("TemplateClassName");

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

        System.out.println(cName);
        Class c = Class.forName("com.seven.templatemethod.one." + cName);
        Object object = c.newInstance();
        return object;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void testx() {
    try {//from  w  w  w.java  2 s  .  co m
        File fXmlFile = new File("C:\\Users\\is96092\\Desktop\\music\\Megaman2.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

public static void getXMLContent(String filePath) {
    try {//ww w  . j av a2s  .  co m
        File f = new File(filePath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document doc = builder.parse(f);
        NodeList nodes_1 = doc.getChildNodes();
        for (int i = 0; i < nodes_1.getLength(); i++) {
            org.w3c.dom.Node nodes_2 = nodes_1.item(i);
            NodeList nodes_3 = nodes_2.getChildNodes();
            for (int j = 0; j < nodes_3.getLength(); j++) {
                org.w3c.dom.Node node = nodes_3.item(j);
                NodeList xmlMeta = node.getChildNodes();
                for (int k = 0; k < xmlMeta.getLength(); k++) {
                    //                  value = xmlMeta.item(k).getNodeValue();
                    System.out.println(xmlMeta.item(k).getNodeName() + ":" + xmlMeta.item(k).getNodeValue());
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static synchronized DocumentBuilder db() {
    if (null != db)
        return db;
    try {/*from w  ww.  ja  va 2s . c om*/
        db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return db;
}

From source file:Main.java

public static Document buildXmlDocument(InputStream stream)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(stream);
}