You are on page 1of 2

XML Parsers Information till this point should have given you a brief idea of what are XML

tags and how they can be declared. If you need more information we recommend you to visit this XML Tutorial Here's how a simple XML file would look like. Notice how all the Tags are Closed. This is called Well Formed ness. All the Tags in the XML file needs to be closed. Else the parser would throw an Exception while parsing this XML file.
<?xml version='1.0' encoding='us-ascii'?> <!DOCTYPE Library> <Library> <!-- Book 1 Comments --> <Book ISBN="8763-343-2343" > <Title>Professional JINI</Title> <Author>Sing Li</Author> <Publisher>Wrox Publications</Publisher>

<Date_Published>22/10/1999</Date_Published> </Book> <!-- Book 2 Comments --> <Book ISBN="6834-423-3434"> <Title>XML Programming</Title> <Author>Sudhir Ancha</Author> <Publisher>Mann Publications</Publisher> <Date_Published/> </Book> </Library>

Couple of things to be noticed in the Above XML File : 1. In the above XML file, after the XML Prolog "<?xml version='1.0' encoding='usascii'?>" we have added one more line called "<!DOCTYPE Library >" Here DOCTYPE Library indicates that all the Tags inside this XML file will be under the Tag "Library". Which means "Library" will be the parent or root of all other Tags in this XML file. Each XML file can have only one DOCTYPE. 2. Also in the XML File we have added comments for Book1 using the Following syntax <!-- Book 1 Comments --> 3. The Element called "Book" has both Attributes and More Tags under it. For Example in the above XML file, for the Book Element, ISBN is attribute and Title, Author and

Publisher are sub Tags under the Book Element. If the Tags and Elements need to be added compulsorily or not in the XML file along with the Element is defined by DTD (Document Type Definition) file. For Example in the above XML file, For Book Element, ISBN might be compulsory if the search Based on ISBN is supported. And Date Published Tag may not be necessary at all times if there's no search facility based on get the Most Recent Books. We will explaining how to create DTD's after next few sections. 4. Notice how we declared a Empty Tag for <Date_Published/>, under Second Book. This Statement is equivalent to writing <Date_Published><Date_Published/>. This Feature Could save your XML File Size if there is no Data required between the Tags. From this point we will be explaining how to use different Kinds of Parsers that are available for processing the XML files and how to use them. XML file is just plain Text File. Now the next step is to retrieve the data provided in this XML file. This is where XML parsers come into picture. Currently in Market there are lot's of XML Parsers available. Two of them are most popular among Java Developers. They are JavaSofts's XML Parser and IBM's Xerces Parser. In this Tutorial we will be explaining how to use the Javasoft's XML parser. JavaSoft currently provides three parsers. They are

SAX (Simple API for XML) Parser DOM (Document Object Model) Parser and XSLT (XML Style Sheet) Parsers.