Friday, 17 October 2014

DOM : Get All the elements with given tag

Document interface provides “getElementsByTagName” method to get all the elements with given tag.
 
NodeList getElementsByTagName(String tagname)
Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.

employee.dtd
<?xml version='1.0' encoding='UTF-8'?>

<!ELEMENT employees (employee)*>

<!ELEMENT employee (address,lastname,firstname)>
<!ATTLIST employee id ID #REQUIRED>

<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT address (country,state,city,area)>
<!ATTLIST address type CDATA #REQUIRED>

<!ELEMENT area (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT country (#PCDATA)>


employee.xml
<?xml version="1.0"?>
<!DOCTYPE books SYSTEM "employee.dtd">

<employees>

  <employee id="1" type="permanent">
    <firstname>Hari Krishna</firstname>
    <lastname>Gurram</lastname>
    <address type="permanent">
      <area>Marthali</area>
      <city>Bangalore</city>
      <state>Karnataka</state>
      <country>India</country>
    </address>
  </employee>
  
  <employee id="2" type="permanent">
    <firstname>Joel Babu</firstname>
    <lastname>Chelli</lastname>
    <address type="temporary">
      <area>Jubli Hills</area>
      <city>Hyderababd</city>
      <state>Andhra Pradesh</state>
      <country>India</country>
    </address>
  </employee>
  
  <employee id="3" type="contract">
    <firstname>Susantha</firstname>
    <lastname>Sarm</lastname>
    <address type="permanent">
      <area>Gandhi Nagar</area>
      <city>Bhuvaneswar</city>
      <state>Orissa</state>
      <country>India</country>
    </address>
  </employee>

</employees>

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParser {

    static Document parseDoc(String doc) throws ParserConfigurationException, SAXException, IOException{
        /* 1. Get the instance of BuilderFactory class. */
        DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();

        /* 2. Instantiate DocumentBuilder object. */
        DocumentBuilder docBuilder = builder.newDocumentBuilder();

        /* Get the Document object  */
        Document document = docBuilder.parse("employee.xml");

        return document;
    }

    public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException{
        Document document = parseDoc("employee.xml");

        NodeList nodes = document.getElementsByTagName("address");
        System.out.println("Total nodes with tag address : " +nodes.getLength());

        for(int i = 0; i<nodes.getLength(); i++){
            Node n1 = nodes.item(i);
            if(n1.getNodeType() == Node.ELEMENT_NODE){
                Element eElement = (Element) n1;

                System.out.println(eElement.getElementsByTagName("area").item(0).getTextContent());
                System.out.println(eElement.getElementsByTagName("city").item(0).getTextContent());
                System.out.println(eElement.getElementsByTagName("state").item(0).getTextContent());
                System.out.println(eElement.getElementsByTagName("country").item(0).getTextContent());
            }
        }
    }
}


Output
Total nodes with tag address : 3
Marthali
Bangalore
Karnataka
India
Jubli Hills
Hyderababd
Andhra Pradesh
India
Gandhi Nagar
Bhuvaneswar
Orissa
India

 



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment