JAXP - Java API for XML Processing
•JAXP is for processing XML data using java applications.
•JAXP uses 2 parser standards
• SAX - Simple API for XML Parsing
• DOM - Document Object Model
•JAXP also supports the XSLT - XML Stylesheet Language Transformations.
•XSLT enables to convert the data to other XML documents or to other
formats, such as HTML.
JAXP Overview
SAX DOM
•SAX is the event-driven, serial-access mechanism that does element-by-
element processing. Since it does not consume more memory, it is used
for server-side and high-performance.
•DOM process the entire XML structure as a tree structure in memory. So
it is much more CPU and memory usage process.
•SAX needs lot more programming than DOM.
SAX
•SAX is fast, efficient reading of XML data. It requires little memory, because
it does not construct an internal representation (tree structure) of the XML
data. Instead, it simply sends data to the application as it is read -- your
application can then do whatever it wants to do with the data it sees.
•In effect, the SAX API acts like a serial I/O stream. You see the data as it
streams in, but you can't go back to an earlier position or leap ahead to a
different position. In general, it works well when you simply want to read data
and have the application act on it.
•The most important interface is the ContentHandler interface. That interface
requires a number of methods that the SAX parser invokes in response to
different parsing events. The major event handling methods are:
startDocument(), endDocument(), startElement(), endElement(), and
characters().
•The easiest way to implement that interface is to extend the DefaultHandler
class. DefaultHandler class implements ContentHandler,DTDHandler,
EntityResolver, and ErrorHandler interfaces.
SAX
•When a start tag or end tag is encountered, the name of the tag is passed
as a String to the startElement() or endElement() method, as appropriate.
When a start tag is encountered, any attributes it defines are also passed in
an Attributes list. Characters found within the element are passed as an
array of characters, along with the number of characters (length) and an
offset into the array that points to the first character.
public void startElement(String uri, String localName, String
qName,Attributes attributes) throws SAXException
public void endElement(String uri, String localName, String qName) throws
SAXException
public void characters(char buf[], int offset, int len) throws SAXException
SAX parser
package SAX.xml2java;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/*
* This class parse the XML and print them.
*/
public class MySaxParser extends DefaultHandler{
StringBuffer textBuffer;
public static void main(String[] args) {
DefaultHandler handler = new MySaxParser();
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(new File("./Util/sax/employees.xml"), handler);
} catch(ParserConfigurationException pcEx){
pcEx.printStackTrace();
} catch(IOException ioEx){
ioEx.printStackTrace();
} catch(SAXException saxEx){
saxEx.printStackTrace();
}
}
@Override
public void startElement(String uri, String localName, String tagName, Attributes attrs) throws SAXException {
System.out.println("startElement "+tagName);
for(int i=0; i<attrs.getLength(); i++){
System.out.println("Attribute "+attrs.getQName(i)+"'s value
"+attrs.getValue(attrs.getQName(i)));
}
}
@Override
public void endElement(String uri, String localName, String tagName) throws SAXException {
echoText(tagName);
System.out.println("endElement "+tagName);
}
@Override
public void characters(char buf[], int offset, int len) throws SAXException {
String s = new String(buf, offset, len);
if (textBuffer == null) {
textBuffer = new StringBuffer(s);
} else {
textBuffer.append(s);
}
}
private void echoText(String tagName) throws SAXException {
if (textBuffer == null || || textBuffer.toString().trim().length()<1) return;
String s = tagName+"'s text value :"+textBuffer.toString().trim();
System.out.println(s);
textBuffer = null;
}
}
SAX parser
employees.xml
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
output
The steps you need to take to validate an XML document using a XML
Schema:
•The parser factory must be configured to create a validating parser to notify the validation errors.
•The appropriate properties must be set on the SAX parser.
•The appropriate error handler must be set.
•The document must be associated with a schema.
The parser factory must be configured to create a validating parser to notify the validation errors
and namespace-aware parser.
factory.setValidating(true);
factory.setNamespaceAware(true);
The appropriate properties must be set on the SAX parser.
Define the constants to use when setting the properties:
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
SAXParser parser = factory.newSAXParser();
parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
SAX parser to validate XML using XSD
The appropriate error handler must be set.
catch(SAXParseException saxPEx){
System.out.println("n** Parsing error"
+ ", line " + saxPEx.getLineNumber()
+ ", uri " + saxPEx.getSystemId());
System.out.println(" " + saxPEx.getMessage() );
saxPEx.printStackTrace();
}
The document must be associated with a schema.
/*If schema declaration in XML doc, then no need to specify the schema here.
When the java pgm specifies the schema to use, it overrides any schema declaration in the XML doc.*/
static final String schemaSource = "./Util/employees.xsd";
static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schemaSource));
SAX parser to validate XML using XSD
SAX parser to validate XML using XSD
package SAX.xml2java;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/*
* This class validates XML with XSD, then parse and print the element data
*/
public class ParseXMLXSD extends DefaultHandler {
static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
StringBuffer textBuffer;
SAX parser to validate XML using XSD
//The document must be associated with a schema.
/*If schema declaration is in XML doc, then no need to specify the schema here.
When the java pgm specifies the schema to use, it overrides any schema declaration in the XML doc.*/
//static final String schemaSource = "./Util/sax/employees.xsd";
//static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
public static void main(String[] args) {
DefaultHandler handler = new ParseXMLXSD();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true); // The parser factory must be configured to create a validating parser to
factory.setNamespaceAware(true); // notify the validation errors and namespace-aware parser.
try{
SAXParser parser = factory.newSAXParser();
// The appropriate properties must be set on the SAX parser.
parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
//The document must be associated with a schema.
parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schemaSource));
parser.parse(new File("./Util/sax/employees1.xml"),handler);
} catch(ParserConfigurationException pcEx){
pcEx.printStackTrace();
} catch(SAXParseException saxPEx){ // The appropriate error handler must be set.
System.out.println("n** Parsing error"
+ ", line " + saxPEx.getLineNumber()
+ ", uri " + saxPEx.getSystemId());
System.out.println(" " + saxPEx.getMessage() );
saxPEx.printStackTrace();
} catch(SAXException saxEx){
saxEx.printStackTrace();
} catch(IOException ioEx){
ioEx.printStackTrace();
}
}
SAX parser to validate XML using XSD
public void startElement(String namespaceURI, String sName, String qName, Attributes attrs)throws SAXException{
String eName = sName; // element name
if ("".equals(eName)) eName = qName; // not namespaceAware
System.out.print("<"+eName+">");
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i); // Attr name
if ("".equals(aName)) aName = attrs.getQName(i);
System.out.print(aName+"=""+attrs.getValue(i)+""");
}
}
}
public void endElement(String namespaceURI, String sName, String qName) throws SAXException {
echoText();
String eName = sName; // element name
if ("".equals(eName)) eName = qName; // not namespaceAware
System.out.println("</"+eName+">");
}
public void characters(char buf[], int offset, int len) throws SAXException {
String s = new String(buf, offset, len);
if (textBuffer == null) {
textBuffer = new StringBuffer(s);
} else {
textBuffer.append(s);
}
}
private void echoText(String tagName) throws SAXException {
if (textBuffer == null || || textBuffer.toString().trim().length()<1) return;
String s = tagName+"'s text value :"+textBuffer.toString().trim();
System.out.print(s);
textBuffer = null;
}
}
employees1.xml
<?xml version="1.0" encoding=“UTF-8"?>
<employees>
<employee>
<name>Pon Akilan</name>
<age>33</age>
<id>123</id>
</employee>
<employee>
<name>Raj</name>
<age>38</age>
<id>888</id>
</employee>
</employees>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element name="employee" minOccurs="2" maxOccurs="2">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="age" type="xs:integer" />
<xs:element name="id" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
employees.xsd
output
<employees><employee><name>Pon Akilan</name>
<age>33</age>
<id>123</id>
</employee>
<employee><name>Raj</name>
<age>38</age>
<id>888</id>
</employee>
</employees>
•Document Object Model views the XML document in tree structure, where each
node contains one of the components from an XML structure.
•The two most common types of nodes are element nodes and text nodes.
•Using DOM functions lets you create nodes, remove nodes, change their
contents, and traverse the node hierarchy.
•DOM requires more memory, because it constructs an internal representation
(tree structure) of the XML data.
DOM
DOM parser
package DOM.xml2java;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import DOM.java2xml.Employee;
/*
* This class parse the XML, store the element data into java object and print them.
*/
public class MyDomParser {
public static void main(String[] args){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("./Util/dom/emps.xml")); // Parse the XML
Element root = doc.getDocumentElement(); // Get root Element of the XML
NodeList emps = root.getElementsByTagName("Employee"); //Get List of <Employee>
List<Employee> empList = new ArrayList<Employee>();
int id;
int age;
String name;
String type;
Employee e;
for(int i=0; i<emps.getLength(); i++){ // iterate each <Employee>
Element nameElem = (Element)root.getElementsByTagName("Name").item(i);
name = nameElem.getTextContent();
String attr = nameElem.getAttribute("employeeNo");
id = Integer.parseInt(attr);
Element ageElem = (Element)root.getElementsByTagName("Age").item(i);
age = Integer.parseInt(ageElem.getTextContent());
Element typeElem = (Element)root.getElementsByTagName("Type").item(i);
type = typeElem.getTextContent();
e = new Employee(id, name, age, type); //store all elements to employee obj
empList.add(e);
}
DOM parser
DOM parser
for(Employee emp:empList) //print all employee object
System.out.println(emp);
} catch(ParserConfigurationException pcEx){
pcEx.printStackTrace();
} catch(SAXParseException saxPEx){
System.out.println("n** Parsing error"
+ ", line " + saxPEx.getLineNumber()
+ ", uri " + saxPEx.getSystemId());
System.out.println(" " + saxPEx.getMessage() );
saxPEx.printStackTrace();
} catch(SAXException saxEx){
saxEx.printStackTrace();
} catch(IOException ioEx){
ioEx.printStackTrace();
}
}
}
package DOM.java2xml;
public class Employee {
private int id;
private int age;
private String name;
private String type;
public Employee(int id, String name, int age, String type){
this.id = id;
this.age = age;
this.name = name;
this.type = type;
}
/*
* This overridden method will be called when this class object is printed using System.out.println();
*/
public String toString(){
return this.id+"-"+this.name+"-"+this.age+"-"+this.type;
}
}
Employee.java
emps.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees>
<Employee>
<Name employeeNo="8818">Pon Akilan</Name>
<Age>34</Age>
<Type>permanent </Type>
</Employee>
<Employee>
<Name employeeNo="8819">Raj Kanna</Name>
<Age>38</Age>
<Type>contract</Type>
</Employee>
<Employee>
<Name employeeNo="8820">Siva samy</Name>
<Age>23</Age>
<Type>contract</Type>
</Employee>
<Employee>
<Name employeeNo="8821">Ranjan</Name>
<Age>47</Age>
<Type>permanent</Type>
</Employee>
</Employees>
output
8818-Pon Akilan-34-permanent
8819-Raj Kanna-38-contract
8820-Siva samy-23-contract
8821-Ranjan-47-permanent
DOM parser to validate XML using XSD
package DOM.xml2java;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/*
* This class validates XML with XSD, then parse and print the element data
*/
public class ParseXMLXSD {
static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
/*If schema declaration in XML doc, then no need to specify the schema here.
When the java pgm specifies the schema to use, it overrides any schema declaration in the XML doc.*/
static final String schemaSource = "./Util/dom/employees.xsd";
static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
public static void main(String[] args){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
try{
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));
}catch(IllegalArgumentException iaEx){
iaEx.printStackTrace();
}
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new SimpleErrorHandler());
Document doc = builder.parse(new File("./Util/dom/employees.xml"));
Element root = doc.getDocumentElement(); // Get root tag of the document
System.out.println("ROOT TAG : "+ root.getTagName());
NodeList nodes = root.getChildNodes(); // Get child nodes of the root tag
new ParseXMLXSD().printElements(nodes);
DOM parser to validate XML using XSD
} catch(ParserConfigurationException pcEx){
pcEx.printStackTrace();
} catch(SAXParseException saxPEx){
System.out.println("n** Parsing error"
+ ", line " + saxPEx.getLineNumber()
+ ", uri " + saxPEx.getSystemId());
System.out.println(" " + saxPEx.getMessage() );
saxPEx.printStackTrace();
} catch(SAXException saxEx){
saxEx.printStackTrace();
} catch(IOException ioEx){
ioEx.printStackTrace();
}
}
DOM parser to validate XML using XSD
/*
* This is recursive function. In this print all the tag elements with values
*/
private void printElements(NodeList nodes){
for(int i=0; i<nodes.getLength(); i++){
if(!nodes.item(i).getNodeName().startsWith("#")){//While printing node names,
//some #text is coming. So avoid it.
if(nodes.item(i).getChildNodes().getLength() == 1) // node with text value
System.out.println("TAG : "+(nodes.item(i)).getNodeName()+" TAG
VALUE : "+(nodes.item(i)).getTextContent());
else
// Parent node
System.out.println("TAG : "+(nodes.item(i)).getNodeName());
}
printElements(nodes.item(i).getChildNodes()); // call again recursively
}
}
}
DOM parser to validate XML using XSD
package DOM.xml2java;
import org.xml.sax.*;
public class SimpleErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
System.out.println("Oh my God!!! Warning " + e.getMessage());
}
public void error(SAXParseException e) throws SAXException {
System.out.println("Oh my God!!! Error " + e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("Oh my God!!! FatalError" + e.getMessage());
}
}
DOM parser to validate XML using XSD
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Employees">
<xs:complexType>
<xs:sequence>
<xs:element name=“Employee" minOccurs="2" maxOccurs=“100">
<xs:complexType>
<xs:sequence>
<xs:element name=“Name" type="xs:string" />
<xs:element name=“Age" type="xs:integer" />
<xs:element name=“Id" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
employees.xsd
employees.xml
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee>
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee>
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Employees>
Oh my God!!! Error cvc-complex-type.3.2.2: Attribute 'type' is not
allowed to appear in element 'Employee'.
ROOT TAG : Employees
TAG : Employee
TAG : Name TAG VALUE : Seagull
TAG : Id TAG VALUE : 3674
TAG : Age TAG VALUE : 34
TAG : Employee
TAG : Name TAG VALUE : Robin
TAG : Id TAG VALUE : 3675
TAG : Age TAG VALUE : 25
TAG : Employee
TAG : Name TAG VALUE : Crow
TAG : Id TAG VALUE : 3676
TAG : Age TAG VALUE : 28
output
package DOM.java2xml;
import java.io.File;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
/*
* This class gets employees details as java objects and convert to XML
*/
DOM parser to convert Java object to XML
public class MyDomParser {
public static void main(String[] args) {
try{
List<Employee> empList = Employee.getEmployees(); //get employee list
DocumentBuilderFactory facory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = facory.newDocumentBuilder();
Document document = builder.newDocument(); //create a new document
Element root = (Element)document.createElement("Employees"); //root element <Employees>
document.appendChild(root);
for(Employee e:empList){
Element emp = document.createElement("Employee"); //<Employee>
root.appendChild(emp);
Element name = document.createElement("Name"); //<Name> with attribute
name.setAttribute("employeeNo", Integer.toString(e.getId()));
Text nameVal = document.createTextNode(e.getName());
name.appendChild(nameVal);
emp.appendChild(name);
DOM parser to convert Java object to XML
Element age = document.createElement("Age"); //<Age>
Text ageVal = document.createTextNode(Integer.toString(e.getAge()));
age.appendChild(ageVal);
emp.appendChild(age);
Element type = document.createElement("Type"); //<Type>
Text typeVal = document.createTextNode(e.getType());
type.appendChild(typeVal);
emp.appendChild(type);
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer tr = tf.newTransformer();
DOMSource domSrc = new DOMSource(document);
StreamResult sr = new StreamResult(new File("./Util/dom/emps1.xml"));
tr.transform(domSrc, sr);
} catch(ParserConfigurationException pcEx){
pcEx.printStackTrace();
} catch(TransformerConfigurationException tcEx){
tcEx.printStackTrace();
} catch(TransformerException trEx){
trEx.printStackTrace();
}
}
}
DOM parser to convert Java object to XML
package DOM.java2xml;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/*
* This class reads employee details from a text file and generate employee objects
*/
public class Employee {
private int id;
private String name;
private int age;
private String type;
public Employee(int id, String name, int age, String type){
this.id = id;
this.name = name;
this.age = age;
this.type = type;
}
Employee.java
/*
* This method reads employee details from a text file and generate
* list of employee objects
*/
public static List<Employee> getEmployees(){
List<Employee> empList = new ArrayList<Employee>();
try{
FileReader fr = new FileReader(new File("./Helpfiles/employees.txt"));
BufferedReader br = new BufferedReader(fr);
String line = null;
String[] tokens;
while((line = br.readLine()) != null){
tokens = line.split("-");
Employee e = new Employee(Integer.parseInt(tokens[0]),
tokens[1],Integer.parseInt(tokens[2]),tokens[3]);
empList.add(e);
}
return empList;
}catch(FileNotFoundException fnfEx){
fnfEx.printStackTrace();
}catch(IOException ioEx){
ioEx.printStackTrace();
}
return empList;
}
}
Employee.java
output file – emps1.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees>
<Employee>
<Name employeeNo="8818">Pon Akilan</Name>
<Age>34</Age>
<Type>permanent </Type>
</Employee>
<Employee>
<Name employeeNo="8819">Raj Kanna</Name>
<Age>38</Age>
<Type>contract</Type>
</Employee>
<Employee>
<Name employeeNo="8820">Siva samy</Name>
<Age>23</Age>
<Type>contract</Type>
</Employee>
<Employee>
<Name employeeNo="8821">Ranjan</Name>
<Age>47</Age>
<Type>permanent</Type>
</Employee>
</Employees>

JAXP

  • 1.
    JAXP - JavaAPI for XML Processing
  • 2.
    •JAXP is forprocessing XML data using java applications. •JAXP uses 2 parser standards • SAX - Simple API for XML Parsing • DOM - Document Object Model •JAXP also supports the XSLT - XML Stylesheet Language Transformations. •XSLT enables to convert the data to other XML documents or to other formats, such as HTML. JAXP Overview
  • 3.
    SAX DOM •SAX isthe event-driven, serial-access mechanism that does element-by- element processing. Since it does not consume more memory, it is used for server-side and high-performance. •DOM process the entire XML structure as a tree structure in memory. So it is much more CPU and memory usage process. •SAX needs lot more programming than DOM.
  • 4.
    SAX •SAX is fast,efficient reading of XML data. It requires little memory, because it does not construct an internal representation (tree structure) of the XML data. Instead, it simply sends data to the application as it is read -- your application can then do whatever it wants to do with the data it sees. •In effect, the SAX API acts like a serial I/O stream. You see the data as it streams in, but you can't go back to an earlier position or leap ahead to a different position. In general, it works well when you simply want to read data and have the application act on it. •The most important interface is the ContentHandler interface. That interface requires a number of methods that the SAX parser invokes in response to different parsing events. The major event handling methods are: startDocument(), endDocument(), startElement(), endElement(), and characters(). •The easiest way to implement that interface is to extend the DefaultHandler class. DefaultHandler class implements ContentHandler,DTDHandler, EntityResolver, and ErrorHandler interfaces.
  • 5.
    SAX •When a starttag or end tag is encountered, the name of the tag is passed as a String to the startElement() or endElement() method, as appropriate. When a start tag is encountered, any attributes it defines are also passed in an Attributes list. Characters found within the element are passed as an array of characters, along with the number of characters (length) and an offset into the array that points to the first character. public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException public void endElement(String uri, String localName, String qName) throws SAXException public void characters(char buf[], int offset, int len) throws SAXException
  • 6.
    SAX parser package SAX.xml2java; importjava.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /* * This class parse the XML and print them. */ public class MySaxParser extends DefaultHandler{ StringBuffer textBuffer; public static void main(String[] args) { DefaultHandler handler = new MySaxParser(); try{ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse(new File("./Util/sax/employees.xml"), handler); } catch(ParserConfigurationException pcEx){ pcEx.printStackTrace(); } catch(IOException ioEx){ ioEx.printStackTrace(); } catch(SAXException saxEx){ saxEx.printStackTrace(); } }
  • 7.
    @Override public void startElement(Stringuri, String localName, String tagName, Attributes attrs) throws SAXException { System.out.println("startElement "+tagName); for(int i=0; i<attrs.getLength(); i++){ System.out.println("Attribute "+attrs.getQName(i)+"'s value "+attrs.getValue(attrs.getQName(i))); } } @Override public void endElement(String uri, String localName, String tagName) throws SAXException { echoText(tagName); System.out.println("endElement "+tagName); } @Override public void characters(char buf[], int offset, int len) throws SAXException { String s = new String(buf, offset, len); if (textBuffer == null) { textBuffer = new StringBuffer(s); } else { textBuffer.append(s); } } private void echoText(String tagName) throws SAXException { if (textBuffer == null || || textBuffer.toString().trim().length()<1) return; String s = tagName+"'s text value :"+textBuffer.toString().trim(); System.out.println(s); textBuffer = null; } } SAX parser
  • 8.
    employees.xml <?xml version="1.0" encoding="UTF-8"?> <Personnel> <Employeetype="permanent"> <Name>Seagull</Name> <Id>3674</Id> <Age>34</Age> </Employee> <Employee type="contract"> <Name>Robin</Name> <Id>3675</Id> <Age>25</Age> </Employee> <Employee type="permanent"> <Name>Crow</Name> <Id>3676</Id> <Age>28</Age> </Employee> </Personnel>
  • 9.
  • 10.
    The steps youneed to take to validate an XML document using a XML Schema: •The parser factory must be configured to create a validating parser to notify the validation errors. •The appropriate properties must be set on the SAX parser. •The appropriate error handler must be set. •The document must be associated with a schema. The parser factory must be configured to create a validating parser to notify the validation errors and namespace-aware parser. factory.setValidating(true); factory.setNamespaceAware(true); The appropriate properties must be set on the SAX parser. Define the constants to use when setting the properties: static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; SAXParser parser = factory.newSAXParser(); parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); SAX parser to validate XML using XSD
  • 11.
    The appropriate errorhandler must be set. catch(SAXParseException saxPEx){ System.out.println("n** Parsing error" + ", line " + saxPEx.getLineNumber() + ", uri " + saxPEx.getSystemId()); System.out.println(" " + saxPEx.getMessage() ); saxPEx.printStackTrace(); } The document must be associated with a schema. /*If schema declaration in XML doc, then no need to specify the schema here. When the java pgm specifies the schema to use, it overrides any schema declaration in the XML doc.*/ static final String schemaSource = "./Util/employees.xsd"; static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schemaSource)); SAX parser to validate XML using XSD
  • 12.
    SAX parser tovalidate XML using XSD package SAX.xml2java; import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; /* * This class validates XML with XSD, then parse and print the element data */ public class ParseXMLXSD extends DefaultHandler { static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; StringBuffer textBuffer;
  • 13.
    SAX parser tovalidate XML using XSD //The document must be associated with a schema. /*If schema declaration is in XML doc, then no need to specify the schema here. When the java pgm specifies the schema to use, it overrides any schema declaration in the XML doc.*/ //static final String schemaSource = "./Util/sax/employees.xsd"; //static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; public static void main(String[] args) { DefaultHandler handler = new ParseXMLXSD(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); // The parser factory must be configured to create a validating parser to factory.setNamespaceAware(true); // notify the validation errors and namespace-aware parser. try{ SAXParser parser = factory.newSAXParser(); // The appropriate properties must be set on the SAX parser. parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); //The document must be associated with a schema. parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schemaSource)); parser.parse(new File("./Util/sax/employees1.xml"),handler); } catch(ParserConfigurationException pcEx){ pcEx.printStackTrace(); } catch(SAXParseException saxPEx){ // The appropriate error handler must be set. System.out.println("n** Parsing error" + ", line " + saxPEx.getLineNumber() + ", uri " + saxPEx.getSystemId()); System.out.println(" " + saxPEx.getMessage() ); saxPEx.printStackTrace(); } catch(SAXException saxEx){ saxEx.printStackTrace(); } catch(IOException ioEx){ ioEx.printStackTrace(); } }
  • 14.
    SAX parser tovalidate XML using XSD public void startElement(String namespaceURI, String sName, String qName, Attributes attrs)throws SAXException{ String eName = sName; // element name if ("".equals(eName)) eName = qName; // not namespaceAware System.out.print("<"+eName+">"); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { String aName = attrs.getLocalName(i); // Attr name if ("".equals(aName)) aName = attrs.getQName(i); System.out.print(aName+"=""+attrs.getValue(i)+"""); } } } public void endElement(String namespaceURI, String sName, String qName) throws SAXException { echoText(); String eName = sName; // element name if ("".equals(eName)) eName = qName; // not namespaceAware System.out.println("</"+eName+">"); } public void characters(char buf[], int offset, int len) throws SAXException { String s = new String(buf, offset, len); if (textBuffer == null) { textBuffer = new StringBuffer(s); } else { textBuffer.append(s); } } private void echoText(String tagName) throws SAXException { if (textBuffer == null || || textBuffer.toString().trim().length()<1) return; String s = tagName+"'s text value :"+textBuffer.toString().trim(); System.out.print(s); textBuffer = null; } }
  • 15.
    employees1.xml <?xml version="1.0" encoding=“UTF-8"?> <employees> <employee> <name>PonAkilan</name> <age>33</age> <id>123</id> </employee> <employee> <name>Raj</name> <age>38</age> <id>888</id> </employee> </employees>
  • 16.
    <?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="employees"> <xs:complexType> <xs:sequence> <xs:element name="employee" minOccurs="2" maxOccurs="2"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="age" type="xs:integer" /> <xs:element name="id" type="xs:integer" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> employees.xsd
  • 17.
  • 18.
    •Document Object Modelviews the XML document in tree structure, where each node contains one of the components from an XML structure. •The two most common types of nodes are element nodes and text nodes. •Using DOM functions lets you create nodes, remove nodes, change their contents, and traverse the node hierarchy. •DOM requires more memory, because it constructs an internal representation (tree structure) of the XML data. DOM
  • 19.
    DOM parser package DOM.xml2java; importjava.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import DOM.java2xml.Employee; /* * This class parse the XML, store the element data into java object and print them. */ public class MyDomParser { public static void main(String[] args){ try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./Util/dom/emps.xml")); // Parse the XML Element root = doc.getDocumentElement(); // Get root Element of the XML NodeList emps = root.getElementsByTagName("Employee"); //Get List of <Employee> List<Employee> empList = new ArrayList<Employee>();
  • 20.
    int id; int age; Stringname; String type; Employee e; for(int i=0; i<emps.getLength(); i++){ // iterate each <Employee> Element nameElem = (Element)root.getElementsByTagName("Name").item(i); name = nameElem.getTextContent(); String attr = nameElem.getAttribute("employeeNo"); id = Integer.parseInt(attr); Element ageElem = (Element)root.getElementsByTagName("Age").item(i); age = Integer.parseInt(ageElem.getTextContent()); Element typeElem = (Element)root.getElementsByTagName("Type").item(i); type = typeElem.getTextContent(); e = new Employee(id, name, age, type); //store all elements to employee obj empList.add(e); } DOM parser
  • 21.
    DOM parser for(Employee emp:empList)//print all employee object System.out.println(emp); } catch(ParserConfigurationException pcEx){ pcEx.printStackTrace(); } catch(SAXParseException saxPEx){ System.out.println("n** Parsing error" + ", line " + saxPEx.getLineNumber() + ", uri " + saxPEx.getSystemId()); System.out.println(" " + saxPEx.getMessage() ); saxPEx.printStackTrace(); } catch(SAXException saxEx){ saxEx.printStackTrace(); } catch(IOException ioEx){ ioEx.printStackTrace(); } } }
  • 22.
    package DOM.java2xml; public classEmployee { private int id; private int age; private String name; private String type; public Employee(int id, String name, int age, String type){ this.id = id; this.age = age; this.name = name; this.type = type; } /* * This overridden method will be called when this class object is printed using System.out.println(); */ public String toString(){ return this.id+"-"+this.name+"-"+this.age+"-"+this.type; } } Employee.java
  • 23.
    emps.xml <?xml version="1.0" encoding="UTF-8"standalone="no"?> <Employees> <Employee> <Name employeeNo="8818">Pon Akilan</Name> <Age>34</Age> <Type>permanent </Type> </Employee> <Employee> <Name employeeNo="8819">Raj Kanna</Name> <Age>38</Age> <Type>contract</Type> </Employee> <Employee> <Name employeeNo="8820">Siva samy</Name> <Age>23</Age> <Type>contract</Type> </Employee> <Employee> <Name employeeNo="8821">Ranjan</Name> <Age>47</Age> <Type>permanent</Type> </Employee> </Employees>
  • 24.
  • 25.
    DOM parser tovalidate XML using XSD package DOM.xml2java; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /* * This class validates XML with XSD, then parse and print the element data */ public class ParseXMLXSD { static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
  • 26.
    /*If schema declarationin XML doc, then no need to specify the schema here. When the java pgm specifies the schema to use, it overrides any schema declaration in the XML doc.*/ static final String schemaSource = "./Util/dom/employees.xsd"; static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; public static void main(String[] args){ try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); try{ factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource)); }catch(IllegalArgumentException iaEx){ iaEx.printStackTrace(); } DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new SimpleErrorHandler()); Document doc = builder.parse(new File("./Util/dom/employees.xml")); Element root = doc.getDocumentElement(); // Get root tag of the document System.out.println("ROOT TAG : "+ root.getTagName()); NodeList nodes = root.getChildNodes(); // Get child nodes of the root tag new ParseXMLXSD().printElements(nodes); DOM parser to validate XML using XSD
  • 27.
    } catch(ParserConfigurationException pcEx){ pcEx.printStackTrace(); }catch(SAXParseException saxPEx){ System.out.println("n** Parsing error" + ", line " + saxPEx.getLineNumber() + ", uri " + saxPEx.getSystemId()); System.out.println(" " + saxPEx.getMessage() ); saxPEx.printStackTrace(); } catch(SAXException saxEx){ saxEx.printStackTrace(); } catch(IOException ioEx){ ioEx.printStackTrace(); } } DOM parser to validate XML using XSD
  • 28.
    /* * This isrecursive function. In this print all the tag elements with values */ private void printElements(NodeList nodes){ for(int i=0; i<nodes.getLength(); i++){ if(!nodes.item(i).getNodeName().startsWith("#")){//While printing node names, //some #text is coming. So avoid it. if(nodes.item(i).getChildNodes().getLength() == 1) // node with text value System.out.println("TAG : "+(nodes.item(i)).getNodeName()+" TAG VALUE : "+(nodes.item(i)).getTextContent()); else // Parent node System.out.println("TAG : "+(nodes.item(i)).getNodeName()); } printElements(nodes.item(i).getChildNodes()); // call again recursively } } } DOM parser to validate XML using XSD
  • 29.
    package DOM.xml2java; import org.xml.sax.*; publicclass SimpleErrorHandler implements ErrorHandler { public void warning(SAXParseException e) throws SAXException { System.out.println("Oh my God!!! Warning " + e.getMessage()); } public void error(SAXParseException e) throws SAXException { System.out.println("Oh my God!!! Error " + e.getMessage()); } public void fatalError(SAXParseException e) throws SAXException { System.out.println("Oh my God!!! FatalError" + e.getMessage()); } } DOM parser to validate XML using XSD
  • 30.
    <?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Employees"> <xs:complexType> <xs:sequence> <xs:element name=“Employee" minOccurs="2" maxOccurs=“100"> <xs:complexType> <xs:sequence> <xs:element name=“Name" type="xs:string" /> <xs:element name=“Age" type="xs:integer" /> <xs:element name=“Id" type="xs:integer" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> employees.xsd
  • 31.
  • 32.
    Oh my God!!!Error cvc-complex-type.3.2.2: Attribute 'type' is not allowed to appear in element 'Employee'. ROOT TAG : Employees TAG : Employee TAG : Name TAG VALUE : Seagull TAG : Id TAG VALUE : 3674 TAG : Age TAG VALUE : 34 TAG : Employee TAG : Name TAG VALUE : Robin TAG : Id TAG VALUE : 3675 TAG : Age TAG VALUE : 25 TAG : Employee TAG : Name TAG VALUE : Crow TAG : Id TAG VALUE : 3676 TAG : Age TAG VALUE : 28 output
  • 33.
    package DOM.java2xml; import java.io.File; importjava.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; /* * This class gets employees details as java objects and convert to XML */ DOM parser to convert Java object to XML
  • 34.
    public class MyDomParser{ public static void main(String[] args) { try{ List<Employee> empList = Employee.getEmployees(); //get employee list DocumentBuilderFactory facory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = facory.newDocumentBuilder(); Document document = builder.newDocument(); //create a new document Element root = (Element)document.createElement("Employees"); //root element <Employees> document.appendChild(root); for(Employee e:empList){ Element emp = document.createElement("Employee"); //<Employee> root.appendChild(emp); Element name = document.createElement("Name"); //<Name> with attribute name.setAttribute("employeeNo", Integer.toString(e.getId())); Text nameVal = document.createTextNode(e.getName()); name.appendChild(nameVal); emp.appendChild(name); DOM parser to convert Java object to XML
  • 35.
    Element age =document.createElement("Age"); //<Age> Text ageVal = document.createTextNode(Integer.toString(e.getAge())); age.appendChild(ageVal); emp.appendChild(age); Element type = document.createElement("Type"); //<Type> Text typeVal = document.createTextNode(e.getType()); type.appendChild(typeVal); emp.appendChild(type); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer tr = tf.newTransformer(); DOMSource domSrc = new DOMSource(document); StreamResult sr = new StreamResult(new File("./Util/dom/emps1.xml")); tr.transform(domSrc, sr); } catch(ParserConfigurationException pcEx){ pcEx.printStackTrace(); } catch(TransformerConfigurationException tcEx){ tcEx.printStackTrace(); } catch(TransformerException trEx){ trEx.printStackTrace(); } } } DOM parser to convert Java object to XML
  • 36.
    package DOM.java2xml; import java.util.ArrayList; importjava.util.List; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /* * This class reads employee details from a text file and generate employee objects */ public class Employee { private int id; private String name; private int age; private String type; public Employee(int id, String name, int age, String type){ this.id = id; this.name = name; this.age = age; this.type = type; } Employee.java
  • 37.
    /* * This methodreads employee details from a text file and generate * list of employee objects */ public static List<Employee> getEmployees(){ List<Employee> empList = new ArrayList<Employee>(); try{ FileReader fr = new FileReader(new File("./Helpfiles/employees.txt")); BufferedReader br = new BufferedReader(fr); String line = null; String[] tokens; while((line = br.readLine()) != null){ tokens = line.split("-"); Employee e = new Employee(Integer.parseInt(tokens[0]), tokens[1],Integer.parseInt(tokens[2]),tokens[3]); empList.add(e); } return empList; }catch(FileNotFoundException fnfEx){ fnfEx.printStackTrace(); }catch(IOException ioEx){ ioEx.printStackTrace(); } return empList; } } Employee.java
  • 38.
    output file –emps1.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Employees> <Employee> <Name employeeNo="8818">Pon Akilan</Name> <Age>34</Age> <Type>permanent </Type> </Employee> <Employee> <Name employeeNo="8819">Raj Kanna</Name> <Age>38</Age> <Type>contract</Type> </Employee> <Employee> <Name employeeNo="8820">Siva samy</Name> <Age>23</Age> <Type>contract</Type> </Employee> <Employee> <Name employeeNo="8821">Ranjan</Name> <Age>47</Age> <Type>permanent</Type> </Employee> </Employees>