Docstoc

jax

Document Sample
jax Powered By Docstoc
					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
                    SAX
                                      <<Interface>>
SAXParser<<creates>> SAXParser
 Factory                             XMLReader
                    parse()        1 parse()




                                      <<Interface>>
                                   ContentHandler
                  DefaultHandler   startDocument()
                                   startElement()
                                   endDocument()
                                   endElement()
                                   characters()


                    MyHandler
            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.
}
Creating a DOM Document
     DocumentBuilderFactory
     newInstance()
                        <<creates>>


            DocumentBuilder
           validating

           parse()

                        <<creates>>


              DocumentImpl




                        <<Interface>>
                     Document
                  DOM Interfaces
  <<Interface>>                         <<Interface>>
  NodeList        *                 *
                                          Node


          <<Interface>>                 <<Interface>>                <<Interface>>
          Element                          Attr                   CharacterData



 <<Interface>>
                          <<Interface>>           <<Interface>>                <<Interface>>
Processing            Document                      Text                     Comment
Instruction


                                                  <<Interface>>
                                            CDataSection
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
A DOM
Visitor                            Visitor
                               depthCounter                     VisitorException
                               visit(n :   Node)
                               visit(n :   Document)
                               visit(n :   Text)
                               visit(n :   NodeList)
                               visit(n :   Element)




                                              PrettyPrintVisitor
          SearchVisitor                       prefix : String
         boolean: test(n : Element)
         visit(n : Element)                   visit(n : Text)
                                              visit(n : Element)




   PersonSearchVisitor
   boolean test(n : Element)
                 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--;
  }
}
              XSLT in JAX
Transformer   <<creates>>
  Factory
                                Transformer
                            transform(s : Source, r : Result)




                                                                 Stream
    Stream                  <<Interface>>      <<Interface>>     Source
    Result                     Result            Source


      SAXResult                                                 SAXSource


                        DOMResult                   DOMSource




                                         <<Interface>>
                                         Document
          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) { }
}

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:2/1/2013
language:Portuguese
pages:22
xuxianglp xuxianglp http://
About