Identifying Document and Event Locations The SAX package contains an interface called Locator. This interface provides methods with which you can associate an event with its location in the document. Specifically, by using the methods of the Locator interface, you can find out the following information: -
The public identifier (URN) of the document being parsed -
The system identifier (URL) of the document being parsed -
The line number in the XML document where a particular event occurred -
The column number in the XML document where a particular event occurred A URN is an Internet resource that is persistent and location-independent. A URL is a type of URN. For example, an URN could look like the following: urn:def://turbo_engines In this scheme, def could be a registry of all dictionaries in the Internet, and turbo_engines the specific term whose meaning you wanted to find. The result of using this URN could be the whole result set of definitions from all dictionaries. The important part is that as a user you do not need to worry about the server names or exact location of the resources. A URL, on the other hand, requires that you know where a resource is located and its filename. For example, consider this hypothetical URL that will get the meaning of the term turbo_engine from the samsdictionary Web site: http://www.samsdictionary.com/turbo_engine.htm In this case, you need to know the name of the server, the path to the file, its name and suffix. | This information can be used by an application in various ways. For example, the URL information can be used to find something relative to the current document. You can think of this in the way a Web browser resolves an anchor tag such as <a href = linktoFile>somelink</a>. The browser uses the location of the current document to find linktoFile. Similarly, the line number information can be used to print diagnostic messages. The Locator object is passed to the application by using the setDocumentLocator() method of the ContentHandler interface. This method takes an instance of the Locator object as its only parameter. The application can then use the instance of the Locator object to find out the location of any ContentHandler event. It is important to remember that the setDocumentLocator() method is called only once at the beginning of the parse. Therefore, if the application needs to find out the location of other ContentHandler events, it needs to save the reference to the Locator object when the setDocumentLocator() method is called. You will now enhance the MyXMLHandler to do the following: To do so, add the lines of code displayed in bold in Listing 4.1. Listing 4.1 Using Locatorpublic class MyXMLHandler extends DefaultHandler { Locator l1; public void setDocumentLocator(Locator l) { System.out.println("Locator :" + l.getSystemId() + "\n"); l1 = l; } public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes elementAttributes) { System.out.println("Location of event at line number:" + l1.getLineNumber( )); System.out.println("Start Element-> "+qualifiedName); ............................. } Here we've declared a reference to the Locator object to store the Locator object that will be passed by the parser. Next, in the setDocumentLocator() method, the getSystemId() method of the Locator object is used to get the URL of the CarParts.xml file, and the Locator object is assigned to the reference to the Locator object that was declared earlier. Finally, in the startElement() method, the saved Locator object is used to get the line number in the XML file wherever a ContentHandler event is occurring. The line number is then displayed in the output. NOTE The code discussed so far is available in the example0401 folder. This folder also contains the sample CarParts.xml file. Compile and run the program. The result of using the Locator object is displayed in bold in Listing 4.2. Listing 4.2 Output of MyXMLHandler Application with LocatorVersion 2A03.0 of MyXMLHandler in example02A03 Locator :file:///D:/sams_work/Java Api/Chapter 4- SAX APIs - Advanced Use/Example0401/ CarParts.xml Start Document: ----- Reading the document CarParts.xml with MyXMLHandler------ Location of event at line number :47 Start Element-> carparts Total Number of Attributes: 0 Location of event at line number :49 Start Element-> supplier Total Number of Attributes: 2 Attribute: name = Heaven Car Parts (TM) Attribute: URL = http://carpartsheaven.com Characters: End Element-> supplier Location of event at line number :51 Start Element-> engines Total Number of Attributes: 0 Location of event at line number :52 Start Element-> engine Price= 3500 Characters: Engine 1 Characters: End Element-> engine End Element-> engines Location of event at line number :56 Start Element-> carbodies Total Number of Attributes: 0 Location of event at line number :57 Start Element-> carbody Total Number of Attributes: 3 Attribute: id = C32 Attribute: type = Tallboy Attribute: color = blue Characters: Car Body 1 Characters: End Element-> carbody End Element-> carbodies Location of event at line number :61 Start Element-> wheels Total Number of Attributes: 0 Location of event at line number :62 Start Element-> wheel Price= 120 Characters: Wheel Set 1 Characters: End Element-> wheel End Element-> wheels Location of event at line number :66 Start Element-> carstereos Total Number of Attributes: 0 Location of event at line number :67 Start Element-> carstereo Total Number of Attributes: 4 Attribute: id = C2 Attribute: manufacturer = MagicSound Attribute: model = T76w Attribute: Price = 500 Characters: Car Stereo 1 Characters: End Element-> carstereo End Element-> carstereos End Element-> carparts End Document: ---------------- Finished Reading the document--------------------- |