Friday 17 October 2014

DOM : Get Element by Id

Document interface provides getElementById method to get the element by id.

Element getElementById(String elementId)
Returns the Element that has an ID attribute with the given value. If no such element exists, this returns null . If more than one element has an ID attribute with that value, what is returned is undefined.  The DOM implementation is expected to use the attribute Attr.isId to determine if an attribute is of type ID. 
Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.
Observe the note, we must define id, using DTD or xml.
 
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.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");

        /* Get the Element with id 1 */
        Element ele = document.getElementById("1");

        System.out.println("Node name : " + ele.getNodeName());
        System.out.println("employee id : " + ele.getAttribute("id"));
        System.out.println("Position Of Employee : " + ele.getAttribute("type"));
        
    }
}


Output
Employee id 1 Details
Node name : employee
employee id : 1
Position Of Employee : permanent


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment