The main three main APIs that we shall focus on are:
1.JAXP – Parsing API.
2.JAXM – Messaging API.
3.JAXB – Binding API.
Java API for XML Processing
As a developer, you would program to a special interface. This interface isolates you from specific parsers and coding changes. SAX and DOM are language independent interfaces.
SAX and DOM have two different APIs to access the information from the XML parser. The different APIs use different approaches to access the information in the XML document.
SAX is a low level API and DOM is a high level API. The next sections will cover SAX and DOM in more detail.
XML applications will create a parser object, throw some XML at the parser and then process the results
Simple API for XML (SAX)
SAX is a standard interface for event based XML parsing.
SAX defines a number of events. It’s up to you to listen for them and respond to them.
The documents are accessed serially and an event is triggered at different parts of the document.
The common events are:
1.start of the document
2.start elements
3.characters
4.end elements
5.end of the document.
Basically you would write a program that has event handlers.
SAX is fast and has a low memory requirement.
SAX parsing is harder to setup.
Document Object Model (DOM)
Designed to be a portable interface for manipulating document structures.
Using DOM, the application builds a tree structure of the XML document in memory. The different parts of the XML file are stored in nodes in the DOM document. It then walks back and forth through the nodes in tree.
<site>
Basics of programming using DOM
After you have made sure that your environment has been set up correctly (see Setting up the environment for XML and Java section), you may write your first Java and XML example.
For this example we will use the DOM API discussed in the previous section.
This is a simple example that will read the text “Hello World” from a xml file called “hello.xml”.
1) Import package org.w3c.dom
The Java interfaces have been defined by W3C and are contained in the package org.w3c.dom.
import org.w3c.dom.*;
2) Import Vendor dependent Parser.
The next step is to import a vendor dependent XML parser. In our case it will be the xerces DOM parser that we configured.
import org.apache.xerces.parsers.DOMParser;
Custom Search
Java XML
JAVA XML API
<resource>
<site>
<sitename>visualbuilder.com</sitename>
<emailaddress>webmaster@visualbuilder.com</emailaddress>
</site>
<sitename>activepace.com</sitename>
<emailaddress>info@activepace.com</emailaddress>
</site>
</resource>
Subscribe to:
Post Comments (Atom)
Cross Rates Powered by Forex Pros - The Forex Trading Portal.
0 comments
Post a Comment