220 likes | 385 Views
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 Linking & Presentation XPATH, XLINK, XBASE, XHTML Semantic Web RDF (Resource Description Framework) OWL (Ontology Web Language)
E N D
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 • Linking & Presentation • XPATH, XLINK, XBASE, XHTML • Semantic Web • RDF (Resource Description Framework) • OWL (Ontology Web Language) • XTP (XML Topic Maps)
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
JAX Consists of • Document-oriented • JAXP (JAX Processing) • Procedure-oriented • JAX-RPC (SOAP method calls) • JAX-M (SOAP messages) • JAX-R (business registries)
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
JAXP Includes: • javax.xml.parsers • SAX API • DOM API • javax.xml.transform • XSLT API • javax.xml.namespace
Parsers • SAX is an event-notification parser • DOM is a tree-building parser
Parsing Using SAX DefaultHandler handler = new MyHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.parse( new File("tree.xml"), handler );
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.}
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));
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
Visiting 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); }}
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
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;
Visiting Text Nodes case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: Text text = (Text) node; depthCounter++; visit(text); depthCounter--; break;
Visiting Child Nodes public void visit(NodeList nodes) throws VisitorException { for(int i = 0; i < nodes.getLength(); i++) { depthCounter++; visit(nodes.item(i)); depthCounter--; }}
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) { }}