XML DOM Parser



XML DOM Parser - Tree Based Parser

  • Effective for apps that need random access to doc element. However, building a complete tree for every doc is expensive in terms of memory.
  • Event-based parsers avoid this as they do not create data structure for entire document
 Tree based parser

Learn XML - XML tutorial - Tree based parser - XML examples - XML programs

Dom Parser

  • The Document Object Model (DOM) is an application programming interface (API) for HTML and XML documents.
  • It defines the logical structure of documents and the way a document is accessed and manipulated .
  • A DOM implementation will have a method to pass a XML file to a factory object that will return a Document object that represents root element of whole document
 XML DOM Parser

Learn XML - XML tutorial - XML DOM Parser - XML examples - XML programs

DOM Parser Tree

 DOM Parser Tree

Learn XML - XML tutorial - DOM Parser Tree - XML examples - XML programs

DOM Parse Tree

Learn XML - XML tutorial - DOM Parse Tree - XML examples - XML programs

Example For DOM Parse Tree

 Tree based parser
Example For DOM Parse Tree

Learn XML - XML tutorial - Tree based parser Example For DOM Parse Tree - XML examples - XML programs

Main features of DOM parser:

  • A DOM parser creates an internal structure in memory which is a DOM document object.
  • Client applications get the information of the original XML document by invoking methods on this Document object or on other objects it contains.
  • DOM parser is tree-based (or DOM obj-based)
  • Client application seems to be pulling the data actively, from the data flow point of view

Example for XML DOM Parser

 XML DOM Parser - Sample XML

Learn XML - XML tutorial - XML DOM Parser Example - XML examples - XML programs

<?xml version = "1.0"?>
    <!-- DOM Parser Example -->

    <!DOCTYPE contacts [
       <!ELEMENT contacts ( contact+ )>
       <!ELEMENT contact ( FirstName, LastName )>
          <!ATTLIST contact gender ( M | F ) "M">
       <!ELEMENT FirstName ( #PCDATA )>
       <!ELEMENT LastName ( #PCDATA )>
    ]>
    <!-- And here is the XML content -->
    <contacts>
       <contact gender = "M">
         <FirstName> Venkat </FirstName>
         <LastName>  Prabu </LastName>
       </contact>
       <contact gender = "F">
         <FirstName> Krishiv </FirstName>
         <LastName>  Venkat </LastName>
       </contact>
    </contacts>

DOM Parser – XML Elements Explanation

1. The XML Declaration.

  • The XML declaration (which, in fact, is optional): <?xml version = "1.0"?> tells a parser that the document contains XML content.
  • The declaration needs to appear right at the beginning of the documents.

2. Comments in XML Files. 

  • XML comments look just like HTML comments: <!-- DOM Parser Example -->

3. Document Type Definition (DTD) :

  • Usually, the document type definition is specified in a file that is separate from the XML document.
  • However, in this simple example we lump the DTD and XML markup into a single file called “tutorial.xml.“
<!DOCTYPE contacts [
       <!ELEMENT contacts ( contact+ )>
       <!ELEMENT contact ( FirstName, LastName )>
          <!ATTLIST contact gender ( M | F ) "M">
       <!ELEMENT FirstName ( #PCDATA )>
       <!ELEMENT LastName ( #PCDATA )>
  • The line of code:
<!ELEMENT contacts ( contact+ )>
  • It defines "contacts" as a list containing one or more "contact" items. Each contact have "FirstName" and "LastName" elements.
<!ELEMENT contact ( FirstName, LastName )>
  • And the element have one argument: "gender" which can be set to "M" or "F."
<!ATTLIST contact gender ( M | F ) "M">
  • The "FirstName" and "LastName" arguments contain just plain text.(Parsed Character Data or PCDATA).
<!ELEMENT FirstName ( #PCDATA )> 
<!ELEMENT LastName ( #PCDATA )>
  • Validating parsers read the DTD before they read your document so that they can identify where every element type ought to come and how each relates to the other, so that applications which need to know this in advance (most editors, search engines, navigators, databases) can set themselves up correctly.
 XML DOM Parser - Sample XML

Learn XML - XML tutorial - XML DOM Parser - Sample XML - XML examples - XML programs



Related Searches to XML DOM Parser