Java API for XML Processing (JAXP)
Java API for XML Processing (JAXP)
The Java API for XML Processing (JAXP) allows applications to parse and transform XML documents
using an API that is independent of any particular XML processor implementation, without changing the XML processor implementations & without impacting their applications. JAXP 1.1 supports the following standards:
SAX version 2.0
DOM Level 2
XSLT 1.0
When using the SAX API through JAXP, one must:
Create a new SAX parser factory
Configure the factory
Create a new parser from the factory
Set the parser’s document handler, error handler, DTD handler and entity resolver
Parse the XML document(s)
SAX parser is a Event Driven Parser. You provide the callback methods, and the parser invokes them as it reads the XML data. In SAX parser you cannot rearrange any serial data stream or rearrange characters you have read from that stream. The following Packages need to be imported in your Java File to use the SAX parser.
Invoking a and parsing the document as a stream of SAX events:
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
boolean validating;
String fileToProcess;
...
SAXParserFactory factory
= SAXParserFactory.newInstance();
factory.setValidating(validating);
SAXParser parser = factory.newSAXParser(); ...
parser.parse(fileToProcess, new HandlerBase() {
... // Custom implementation of the HandlerBase
// to process the document as SAX events
});
...
DOM through JAXP are:
Create a new DOM document builder factory
Configure the factory
Create a new document builder from the factory
Set the underlying parser’s error handler and entity resolver
Parse the XML document(s) to generate a DOM tree
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import org.w3c.dom.*;
import javax.xml.parsers.*;
boolean validating;
String fileToProcess; ...
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
factory.setValidating(validating);
DocumentBuilder builder
= factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() { ... });
...
Document document = builder.parse(fileToProcess);
... // Processing the document as a DOM tree
JAXP to perform XSL Transformations :
A TransformerFactory object is instantiated, and used to create a Transformer. The source object is the input to the transformation process. A source object can be created from SAX reader, from a DOM, or from an inputstream.Similarly, the result object is the result of the transformation process. That object can be a SAX event handler, a DOM, or an output stream.When the transformer is created,it may be created from a set of transformation instructions, in which case the specified transformations are carried out. If it is created without any specific instructions, then the transformer object simply copies the source to the result.
STEPS:
Create a new transformer factory
Configure the factory
Create a new transformer from the factory with a particular style sheet
Set the error listener and URI resolver
Apply the style sheet to the XML document(s) to generate DOM trees, SAX events or write to an output stream
import javax.xml.transform.*;
String styleSheetFile;
String fileToProcess;
OutputStream out;
Properties properties; ...
TransformerFactory factory
= TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(
new SAXSource(new InputSource(styleSheetFile)));
for (Enumeration i = properties.propertyNames();
i.hasMoreElements();) {
String name = (String) i.nextElement();
transformer.setParameter(name,
"\'" + properties.getProperty(name) + "\'");
}
transformer.transform(
new SAXSource(new InputSource(fileToProcess)),
new StreamResult(out)); ...
Complete SAX Parser Implementation:
public class BooksLibrary extends HandlerBase
{
protected static final String XML_FILE_NAME = "D:\\Sudhir\\webs\\MyXML\\s1\\library1.xml";
public static void main (String argv [])
{
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// Set up output stream
out = new OutputStreamWriter (System.out, "UTF8");
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(XML_FILE_NAME), new BooksLibrary() );
} catch (Throwable t) {
t.printStackTrace ();
}
System.exit (0);
}
static private Writer out;
//===========================================================
// Methods in SAX DocumentHandler
//===========================================================
public void startDocument ()
throws SAXException
{
showData ("<?xml version='1.0' encoding='UTF-8'?>");
newLine();
}
public void endDocument ()
throws SAXException
{
try {
newLine();
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
public void startElement (String name, AttributeList attrs)
throws SAXException
{
showData ("<"+name);
if (attrs != null) {
for (int i = 0; i < attrs.getLength (); i++) {
showData (" ");
showData (attrs.getName(i)+"=\""+attrs.getValue (i)+"\"");
}
}
showData (">");
}
public void endElement (String name)
throws SAXException
{
showData ("</"+name+">");
}
public void characters (char buf [], int offset, int len)
throws SAXException
{
String s = new String(buf, offset, len);
showData (s);
}
//===========================================================
// Helpers Methods
//===========================================================
// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void showData (String s)
throws SAXException
{
try {
out.write (s);
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
// Start a new line
private void newLine ()
throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try {
out.write (lineEnd);
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
}
/*
* Called when the Parser starts parsing the Current XML File.
*/
public void startDocument () throws SAXException
{
……
}
/*
*Called when the Parser Completes parsing the Current XML File.
*/
public void endDocument () throws SAXException
{
……
}
/*
* Called when the starting of the Element is reached. For Example if we have Tag
* called <Title> … </Title>, then this method is called when <Title> tag is
* Encountered while parsing the Current XML File. The AttributeList Parameter has
* the list of all Attributes declared for the Current Element in the XML File.
*/
public void startElement (String name, AttributeList attrs) throws SAXException
{
……
}
/*
* Called when the Ending of the current Element is reached. For example in the
* above explanation, this method is called when </Title> tag is reached
*/
public void endElement (String name) throws SAXException
{
……
}
/*
* While Parsing the XML file, if extra characters like space or enter Character
* are encountered then this method is called. If you don’t want to do anything
* special with these characters, then you can normally leave this method blank.
*/
public void characters (char buf [], int offset, int len) throws SAXException
{
……
}
/*
* In the XML File if the parser encounters a special Processing Instruction which is
* price”. You can invoke a External Program from this Method if required.
*/
public void processingInstruction (String target, String data) throws SAXException
{
…….
}
0 comments:
Post a Comment