Title: JAX- Java APIs for XML
1JAX- Java APIs for XML
2Some XML Standards
- Basic
- SAX (sequential access parser)
- DOM (random access parser)
- XSL (XSLT, XPATH)
- DTD
- Schema
- Linking Presentation
- XPATH, XLINK, XBASE, XHTML
- Semantic Web
- RDF (Resource Description Framework)
- OWL (Ontology Web Language)
- XTP (XML Topic Maps)
3JAX Supports Java Web Services
- Java Web Service is a B2B service implemented in
Java - Web clients, apps, and services communicate and
share data using XML - JAX is a layer of APIs that sits on top of J2EE
4JAX Consists of
- Document-oriented
- JAXP (JAX Processing)
- Procedure-oriented
- JAX-RPC (SOAP method calls)
- JAX-M (SOAP messages)
- JAX-R (business registries)
5Major JAX packages
- javax.xml.soap
- javax.xml.transform
- javax.xml.rpc
- javax.xml.messaging
- javax.xml.namespace
- javax.xml.parsers
- org.w3c.dom
- org.xml.sax
6JAXP Includes
- javax.xml.parsers
- SAX API
- DOM API
- javax.xml.transform
- XSLT API
- javax.xml.namespace
7Parsers
- SAX is an event-notification parser
- DOM is a tree-building parser
8SAX
9Parsing Using SAX
DefaultHandler handler new MyHandler() SAXParse
rFactory factory SAXParserFactory.newInstance
() SAXParser saxParser factory.newSAXParser()
saxParser.parse( new File("tree.xml"), handler )
10A SAX Event Handler
class MyHandler extends DefaultHandler
public void startElement(...) // start
processing detected element public void
endElement(...) // finish processing
detected element public void
characters(...) // process text element
// etc.
11Creating a DOM Document
12DOM Interfaces
13makeDocument(String xmlFile)
// obtain parser factory
DocumentBuilderFactory factory
DocumentBuilderFactory.newInstance() //obtain
parser DocumentBuilder builder
factory.newDocumentBuilder() // parse an xml
document into a DOM tree Document doc
builder.parse(new File(xmlFile))
14The Visitor Design Pattern
- Problem We want to process each node in a tree,
but there are many types of nodes. - Solution Put the node processing methods in a
separate object called a visitor. When a visitor
visits a node of type T, the correct method is
automatically invoked.Visitors can use depth- or
bredth-first traversal
15A DOM Visitor
16Visiting a Document
public class Visitor protected String name,
value protected int depthCounter 0
public void visit(Document doc) try
visit((Node)doc.getDocumentElement())
catch(VisitorException e)
handle(e) public void visit(Node
node) throws VisitorException ... public
void visit(NodeList nodes) throws
VisitorException ... // overridables
protected void visit(Element node) throws
VisitorException protected void visit(Text
node) throws VisitorException protected
void visit(Attr node) throws VisitorException
protected void handle(VisitorException e)
System.err.println("visitor exception "
e)
17Visiting a Node
public void visit(Node node) throws
VisitorException name node.getNodeName()
value node.getNodeValue() switch
(node.getNodeType()) case
Node.ELEMENT_NODE ... case
Node.CDATA_SECTION_NODE case
Node.TEXT_NODE ... // etc. // switch
// visit node
18Visiting an Element Node
case Node.ELEMENT_NODE Element elem
(Element) node depthCounter
visit(elem) NamedNodeMap attributeNodes
node.getAttributes() for(int i 0 i lt
attributeNodes.getLength() i) Attr
attribute (Attr) attributeNodes.item(i)
depthCounter visit(attribute)
depthCounter-- depthCounter--
visit(node.getChildNodes()) break
19Visiting Text Nodes
case Node.CDATA_SECTION_NODE case
Node.TEXT_NODE Text text (Text) node
depthCounter visit(text)
depthCounter-- break
20Visiting Child Nodes
public void visit(NodeList nodes) throws
VisitorException for(int i 0 i lt
nodes.getLength() i) depthCounter
visit(nodes.item(i)) depthCounter--
21XSLT in JAX
22MakeXMLFile()
static public void makeXMLFile(String xmlFile,
Document doc) try Source xmlSource
new DOMSource(doc) Result result new
StreamResult( new FileOutputStream(xmlFil
e)) TransformerFactory transFactory
TransformerFactory.newInstance()
Transformer transformer
transFactory.newTransformer()
transformer.setOutputProperty("indent", "yes")
transformer.transform(xmlSource, result)
catch(Exception e)