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[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//  w w  w  .  j av  a2s .  c  o m
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getElementById("key1");
    boolean has = element.hasAttribute("value");
    String attrValue = element.getAttribute("value");
    element.setAttribute("value", "newValue1");

    element = doc.getElementById("key2");
    has = element.hasAttribute("value");
    attrValue = element.getAttribute("value");

    element.setAttribute("value", "a<\"'&>z");

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.newDocument();
    Element root = document.createElement("order");
    document.appendChild(root);//from w ww  . j a  va2  s  . c om
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.parse("sample.xml");
    Element tree = document.getDocumentElement();
}

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 a2s.c o m*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    Attr attr = (Attr) element.getAttributeNode("attrName");
    boolean wasSpecified = attr != null && attr.getSpecified();
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new File("data.xml"));
    doc.getDocumentElement().normalize();

    System.out.println("City: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
            .getChildNodes().item(0).getAttributes().getNamedItem("data").getNodeValue());

    System.out.println("Postal Code: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
            .getChildNodes().item(1).getAttributes().getNamedItem("data").getNodeValue());

    System.out.println("Date: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
            .getChildNodes().item(4).getAttributes().getNamedItem("data").getNodeValue());
}

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"));
    NodeList list = doc.getElementsByTagName("*");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setValidating(false);//  w  ww .  j av  a2 s. co m
    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
    Document dDoc = dBuilder.newDocument();

    // The root document element. 
    Element pageDataElement = dDoc.createElement("page-data");
    pageDataElement.appendChild(dDoc.createTextNode("Example Text."));

    dDoc.appendChild(pageDataElement);

    System.out.println(dDoc.getDocumentElement().getTextContent());
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//www  .  java2s.c o m

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.newDocument();

    String docNS = "http://www.my-company.com";

    Element order = document.createElementNS(docNS, "order");
    document.appendChild(order);
    order.setAttribute("xmlns", docNS);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*  w ww.  j a v  a  2s.  c o  m*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.getParentNode().insertBefore(doc.createTextNode("B"), element);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/* ww w .  j ava2s . c o m*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.appendChild(doc.createTextNode("D"));

}