Example usage for org.w3c.dom Document getElementsByTagName

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

Introduction

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

Prototype

public NodeList getElementsByTagName(String tagname);

Source Link

Document

Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.

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  .j a v  a 2 s . co  m

    factory.setExpandEntityReferences(false);

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

    Element dup = (Element) element.cloneNode(true);

    element.getParentNode().insertBefore(dup, element.getNextSibling());
}

From source file:Main.java

public static void main(String[] args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document xmlDoc = docBuilder.parse(new File("sample.xml"));
    NodeList nodes = xmlDoc.getElementsByTagName("fr");
    for (int i = 0, length = nodes.getLength(); i < length; i++) {
        ((Element) nodes.item(i)).setTextContent("Modified");
    }/*from www  .java  2  s .c  o m*/
    xmlDoc.getDocumentElement().normalize();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    DOMSource domSource = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(new File("sample.xml"));
    transformer.transform(domSource, result);
    System.out.println("Modification Done");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<xml xmlns:log='http://sample.com'><test log:writer='someWriter'/></xml>";
    StringReader xmlReader = new StringReader(xml);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w  ww.  j  a  v  a  2 s . c o m
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(xmlReader));

    Element currentNode = (Element) doc.getElementsByTagName("test").item(0);
    String attributeValue = currentNode.getAttributes().getNamedItemNS("http://sample.com", "writer")
            .getNodeValue();
    System.out.println("Attribute value is " + attributeValue);
    xmlReader.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new File("D:/test.xml"));

    NodeList elt = doc.getElementsByTagName("EMPLOYEE");
    for (int k = 0; k < elt.getLength(); k++) {
        Node firstNode3 = elt.item(k);
        Element elt1 = (Element) firstNode3;
        String att = elt1.getAttribute("PERMANENT");
        System.out.println("\n\nPERMANENT: " + att);

        NodeList nodes = elt1.getElementsByTagName("DETAILS");
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childNode = nodes.item(i);
            Element elt2 = (Element) childNode;
            System.out.println("---" + elt2.getNodeName());
            System.out.println("NAME:" + elt2.getAttribute("NAME"));
            System.out.println("ID:" + elt2.getAttribute("ID"));
            System.out.println("AGE:" + elt2.getAttribute("AGE"));
        }//  www . jav a  2s.  c o m
    }
}

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  va2 s .  c  o m*/

    factory.setExpandEntityReferences(false);

    Document doc1 = factory.newDocumentBuilder().parse(new File("filename"));
    NodeList list = doc1.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    Document doc2 = factory.newDocumentBuilder().parse(new File("infilename2.xml"));

    Node dup = doc2.importNode(element, true);

    doc2.getDocumentElement().appendChild(dup);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  w ww. ja va  2 s  .  com*/
    factory.setExpandEntityReferences(false);

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

    Element element = (Element) doc.getElementsByTagName("junk").item(0);

    element.getParentNode().removeChild(element);
}

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  a  v a  2  s.c o  m

    factory.setExpandEntityReferences(false);

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

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);

        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}

From source file:MainClass.java

public static void main(String[] args)
        throws IOException, ParserConfigurationException, org.xml.sax.SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);/*from  w  ww  . j a v a 2  s.  c om*/
    factory.setCoalescing(true); // Convert CDATA to Text nodes
    factory.setNamespaceAware(false); // No namespaces: this is default
    factory.setValidating(false); // Don't validate DTD: also default

    DocumentBuilder parser = factory.newDocumentBuilder();

    Document document = parser.parse(new File(args[0]));

    NodeList sections = document.getElementsByTagName("sect1");
    int numSections = sections.getLength();
    for (int i = 0; i < numSections; i++) {
        Element section = (Element) sections.item(i); // A <sect1>

        Node title = section.getFirstChild();
        while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
            title = title.getNextSibling();

        if (title != null)
            System.out.println(title.getFirstChild().getNodeValue());
    }
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(Main.class.getResourceAsStream("/foo.xml"));
    NodeList nodeNodeList = document.getElementsByTagName("node");
    for (int i = 0; i < nodeNodeList.getLength(); i++) {
        Node nNode = nodeNodeList.item(i);
        System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue());
        System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());
    }/* w w w.  ja  v  a 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);//from  w  w  w  . j  a  va  2  s  .  c o 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();

}