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 void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("input.xml"));
    NodeList nodeList = document.getElementsByTagName("Item");
    for (int x = 0, size = nodeList.getLength(); x < size; x++) {
        System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue());
    }/*from   ww  w  .  j  a  v a 2s.c om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("yourFile.xml"));
    NodeList nodeList = document.getElementsByTagName("message");
    for (int x = 0, size = nodeList.getLength(); x < size; x++) {
        System.out.println(nodeList.item(x).getAttributes().getNamedItem("to").getNodeValue());
    }/*from   w w w. j  a va  2 s . c  o m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*  w  w w .java 2s. c  o m*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();

    Text text1 = (Text) element.getFirstChild();
    String string = text1.getData();

    String word = "some";
    Text text2 = text1.splitText(string.indexOf(word));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  w  ww.  j  a v  a2  s.  co m*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = (Element) doc.getElementsByTagName("b").item(0);

    Node parent = element.getParentNode();

    parent.removeChild(element);

    parent.normalize();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document originalDocument = db.parse(new File("input.xml"));
    Node originalRoot = originalDocument.getDocumentElement();

    Document copiedDocument = db.newDocument();
    Node copiedRoot = copiedDocument.importNode(originalRoot, true);
    copiedDocument.appendChild(copiedRoot);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from ww w  .j  a  v a  2  s  .  c  o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();

    Text text1 = (Text) element.getFirstChild();
    String string = text1.getData();
    String word = "some";
    Text text2 = text1.splitText(string.indexOf(word));

    Element newElement = doc.createElement("b");
    newElement.appendChild(text2);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*w  ww .j  ava2s. c o  m*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");

    NamedNodeMap attrs = element.getAttributes();
    String[] names = new String[attrs.getLength()];
    for (int i = 0; i < names.length; i++) {
        names[i] = attrs.item(i).getNodeName();
    }
    for (int i = 0; i < names.length; i++) {
        attrs.removeNamedItem(names[i]);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*  w  w w .ja  v  a 2  s  .  c  o m*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element root = null;

    NodeList list = doc.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i) instanceof Element) {
            root = (Element) list.item(i);
            break;
        }
    }
    root = doc.getDocumentElement();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);// w  w w . j a va 2  s  .c  o m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();

    Text text1 = (Text) element.getFirstChild();
    String string = text1.getData();
    String word = "some";
    Text text2 = text1.splitText(string.indexOf(word));

    Element newElement = doc.createElement("b");
    newElement.appendChild(text2);

    element.insertBefore(newElement, text2);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from w  w  w. j av a2 s.  c  om*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    NamedNodeMap notations = doc.getDoctype().getNotations();
    for (int i = 0; i < notations.getLength(); i++) {
        Notation notation = (Notation) notations.item(i);

        String notationName = notation.getNodeName();
        String notationPublicId = notation.getPublicId();
        String notationSystemId = notation.getSystemId();
    }
}