Pages

XML Programming

Monday, 21 October 2013

We’ll discuss how to use an XML parser to:
• Process an XML document
• Create an XML document
• Manipulate an XML document

XML Architecture
An XML application is typically built around an XML parser. It has an interface to its users, and an interface to some sort of back-end data store.

U I   < ---->  XML Application | XML Parser    <-------->   Data Source

Definitions -
  1. An XML parser is a piece of code that reads a document and analyzes its structure. 
  2. Software that reads an XML document, identifies all the XML tags and passes the data to application.
  3. Parse means to break (a sentence) down into its component parts of speech with an explanation of the form, function, and syntactical relationship of each part. 
Q. How to use a parser?
Ans . Follow the steps-
1. Create a parser object
2. Pass your XML document to the parser
3. Process the results

Kinds of parsers
There are several different ways to categorize parsers:

• Validating versus non-validating parsers
• Parsers that support the Document Object
Model (DOM)
• Parsers that support the Simple API for XML
(SAX)
• Parsers written in a particular language (Java,
C++, Perl, etc.)

Validating parsers validate XML documents as they parse them. Non-validating parsers ignore any validation errors.

The Document Object Model (DOM)

The Document Object Model is an official recommendation of the World Wide WebConsortium (W3C). It defines an interface that enables programs to access and update the style, structure, and contents of XML documents. XML parsers that support the DOM implement that interface.

Q What you get from a DOM parser?
Ans When you parse an XML document with a DOM parser, you get back a tree structure that contains all of the elements of your document. The DOM provides a variety of functions you can use to examine the contents and structure of the document.

Warning
DOM Parser is slow and consumes a lot of memory when it loads an XML document which contains a lot of data. Please consider SAX parser as solution for it, SAX is faster than DOM and use less memory.

DOM interfaces
The DOM defines several Java interfaces. Here are the most common:

• Node:       The base datatype of the DOM.
• Element:    The vast majority of the objects you’ll deal with are Elements.
• Attr:          Represents an attribute of an element.
• Text:         The actual content of an Element or
  Attr.          Document: Represents the entire XML document. A Document object is referred DOM tree.

 staff.xml
<?xml version="1.0"?>
<company>
 <staff id="1001">
  <firstname>Yuvraj</firstname>
  <lastname>Kakkar</lastname>
  <nickname>Yuvi</nickname>
  <salary>700000</salary>
 </staff>
 <staff id="2001">
  <firstname>Azad</firstname>
  <lastname>Chauhan</lastname>
  <nickname>Jaadu</nickname>
  <salary>200000</salary>
 </staff>
</company>



ReadXMLFile.java
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
 
public class ReadXMLFile {
 
  public static void main(String argv[]) {
 
    try {
 File fXmlFile = new File("yourdir/staff.xml");
 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
 Document doc = dBuilder.parse(fXmlFile);
 

 doc.getDocumentElement().normalize();
 
 System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
 
 NodeList nList = doc.getElementsByTagName("staff");
 
 System.out.println("----------------------------");
 
 for (int temp = 0; temp < nList.getLength(); temp++) {
 
  Node nNode = nList.item(temp);
 
  System.out.println("\nCurrent Element :" + nNode.getNodeName());
 
  if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
   Element eElement = (Element) nNode;
 
   System.out.println("Staff id : " + eElement.getAttribute("id"));
   System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
   System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
   System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
   System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

  }
 }
    } catch (Exception e) {
 e.printStackTrace();
    }
  }
 
}

RESULT
Root element :company
----------------------------
Current Element :staff
Staff id : 1001
First Name : Yuvraj
Last Name : Kakkar
Nick Name : Yuvi
Salary : 700000
 
Current Element :staff
Staff id : 2001
First Name : Azad
Last Name : Chauhan
Nick Name : Jaadu
Salary : 200000



The Simple API for XML (SAX)
The SAX API is an alternate way of working with the contents of XML documents.

Q What you get from a SAX parser ?
Ans When you parse an XML document with a SAX parser, the parser generates events at various points in your document. It’s up to you to decide what to do with each of those events.



No comments:

Post a Comment

 

Blogger news

Blogroll

Most Reading

Tags