Example usage for org.w3c.dom Document getElementById

List of usage examples for org.w3c.dom Document getElementById

Introduction

In this page you can find the example usage for org.w3c.dom Document getElementById.

Prototype

public Element getElementById(String elementId);

Source Link

Document

Returns the Element that has an ID attribute with the given value.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  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");
    element = doc.createElement("root");
    doc.appendChild(element);

    Comment comment = doc.createComment("a comment");
    doc.insertBefore(comment, element);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from w ww .ja  va2s  . c o m*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    NamedNodeMap attrs = element.getAttributes();

    int numAttrs = attrs.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attrs.item(i);
        String attrName = attr.getNodeName();
        String attrValue = attr.getNodeValue();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*  w ww  . ja  v  a 2s  .com*/
    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);//from www .j a v  a  2  s  . co  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();
    factory.setValidating(true);//  w  ww .  j a v  a  2  s . c  o  m
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    cdata.setData("some data");
    int len = cdata.getLength();

}

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  a2s  . c  om
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    // All three types of nodes implement the CharacterData interface
    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    // Delete text
    int offset = 0;
    int len = 5;
    cdata.deleteData(offset, len);
}

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.  co m
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    // All three types of nodes implement the CharacterData interface
    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    cdata.setData("some data");
    int len = cdata.getLength();
    int offset = 5;
    len = 4;
    String s = cdata.substringData(offset, len);
}

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  a 2 s  .  c  om*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    // All three types of nodes implement the CharacterData interface
    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    // data

    int offset = 5;
    cdata.insertData(offset, "a ");
    cdata.appendData(" b");
}

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

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    CDATASection cdataNode = doc.createCDATASection("");
    Comment commentNode = doc.createComment("");
    Text textNode = doc.createTextNode("");

    // All three types of nodes implement the CharacterData interface
    CharacterData cdata = cdataNode;
    cdata = commentNode;
    cdata = textNode;

    // Replace text
    String replacement = "c";
    int offset = 10;
    int len = 6;
    cdata.replaceData(offset, len, replacement);
}

From source file:javatojs.DomUtil.java

public static void main(String[] args) {
    Document document = readDocument(
            "D:\\users\\sk\\netbeansProjects\\LiveConnect_Java_To_JavaScript_Example\\src\\AppletPage.html");
    if (document != null) {
        Element summaryElem = document.getElementById("summary");
        System.out.println("=====summaryElem: " + summaryElem);
        if (summaryElem != null) {
            Node summaryTextNode = document.createTextNode("this is a summary");
            summaryElem.appendChild(summaryTextNode);

        }//  w w w.  j av  a2  s.c om
    }
    writeDocument(document);
}