1 / 22

JAX- Java APIs for XML

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)

frisco
Download Presentation

JAX- Java APIs for XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAX- Java APIs for XML by J. Pearce

  2. 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)

  3. 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

  4. JAX Consists of • Document-oriented • JAXP (JAX Processing) • Procedure-oriented • JAX-RPC (SOAP method calls) • JAX-M (SOAP messages) • JAX-R (business registries)

  5. 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

  6. JAXP Includes: • javax.xml.parsers • SAX API • DOM API • javax.xml.transform • XSLT API • javax.xml.namespace

  7. Parsers • SAX is an event-notification parser • DOM is a tree-building parser

  8. SAX

  9. Parsing Using SAX DefaultHandler handler = new MyHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.parse( new File("tree.xml"), handler );

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

  11. Creating a DOM Document

  12. DOM Interfaces

  13. 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));

  14. 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

  15. A DOM Visitor

  16. 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); }}

  17. 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

  18. 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;

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

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

  21. XSLT in JAX

  22. 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) { }}

More Related