Top Banner
JAX- Java APIs for XML by J. Pearce
22

JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

JAX- Java APIs for XML

by J. Pearce

Page 2: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Some 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)

Page 3: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

JAX 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

Page 4: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

JAX Consists of

• Document-oriented– JAXP (JAX Processing)

• Procedure-oriented– JAX-RPC (SOAP method calls)– JAX-M (SOAP messages)– JAX-R (business registries)

Page 5: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Major 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

Page 6: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

JAXP Includes:

• javax.xml.parsers– SAX API– DOM API

• javax.xml.transform– XSLT API

• javax.xml.namespace

Page 7: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Parsers

• SAX is an event-notification parser

• DOM is a tree-building parser

Page 8: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

SAX

SAXParserparse()

DefaultHandler

XMLReader<<Interface>>

parse()1

ContentHandler<<Interface>>

startDocument()startElement()endDocument()endElement()characters()

MyHandler

1

SAXParserFactory

<<creates>>

Page 9: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Parsing Using SAX

DefaultHandler handler = new MyHandler();

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser saxParser = factory.newSAXParser();

saxParser.parse( new File("tree.xml"), handler );

Page 10: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

A 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.}

Page 11: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Creating a DOM Document

Document<<Interface>>

DocumentBuilderFactorynewInstance()

DocumentImpl

DocumentBuildervalidating

parse()

<<creates>>

<<creates>>

Page 12: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

DOM Interfaces

Element<<Interface>>

Attr<<Interface>>

CharacterData<<Interface>>

Text<<Interface>>

Document<<Interface>>

Node<<Interface>>

NodeList<<Interface>>

** **

Comment<<Interface>>

CDataSection<<Interface>>

ProcessingInstruction

<<Interface>>

Page 13: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

makeDocument(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));

Page 14: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

The 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

Page 15: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

A DOM Visitor

VisitorExceptionVisitor

depthCounter

visit(n : Node)visit(n : Document)visit(n : Text)visit(n : NodeList)visit(n : Element)

SearchVisitorboolean: test(n : Element)visit(n : Element)

PrettyPrintVisitorprefix : String

visit(n : Text)visit(n : Element)

PersonSearchVisitorboolean test(n : Element)

Page 16: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Visiting a Documentpublic 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); }}

Page 17: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Visiting 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

Page 18: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Visiting an Element Node: case Node.ELEMENT_NODE: Element elem = (Element) node; depthCounter++; visit(elem); NamedNodeMap attributeNodes = node.getAttributes(); for(int i = 0; i < attributeNodes.getLength(); i++) { Attr attribute = (Attr) attributeNodes.item(i); depthCounter++; visit(attribute); depthCounter--; } depthCounter--; visit(node.getChildNodes()); break;

Page 19: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Visiting Text Nodes

case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: Text text = (Text) node; depthCounter++; visit(text); depthCounter--; break;

Page 20: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

Visiting Child Nodes

public void visit(NodeList nodes) throws VisitorException { for(int i = 0; i < nodes.getLength(); i++) { depthCounter++; visit(nodes.item(i)); depthCounter--; }}

Page 21: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

XSLT in JAX

Source<<Interface>>

SAXSource

StreamSource

DOMSource

Result<<Interface>>

StreamResult

SAXResult

DOMResult

Document<<Interface>>

TransformerFactory Transformer

transform(s : Source, r : Result)

<<creates>>

Page 22: JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.

MakeXMLFile()

static public void makeXMLFile(String xmlFile, Document doc) { try { Source xmlSource = new DOMSource(doc); Result result = new StreamResult( new FileOutputStream(xmlFile)); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.transform(xmlSource, result); } catch(Exception e) { }}