java access xml

Click here to load reader

Upload: aretha-rios

Post on 30-Dec-2015

72 views

Category:

Documents


1 download

DESCRIPTION

Java Access XML. 數位芝麻網路公司 (www.d11e.com) 軟體工程師 陳威誌 ([email protected]). 課程目的. 瞭解 DOM (Document Object Model) 、 SAX(The Simple API for XML) 及 Xerces 、 Castor 等 API ,並以 JAVA 處理 XML 文件,實作如何存、取及動態產生 XML 文件內容。. 議題. 前言 DOM SAX Xerces-J 實作 Castor 實作. 前言. - PowerPoint PPT Presentation

TRANSCRIPT

  • Java Access XML(www.d11e.com) ([email protected])

  • DOM (Document Object Model)SAX(The Simple API for XML)XercesCastorAPI JAVA XML XML

  • DOMSAXXerces-JCastor

  • JAVA XML JAVAXML

    JAVA + XML Portable Code + Portable Data

    XML

  • DOM SAX W3C DOM (Document Object Model) API ( Interface)XML( IBMSUN )XML Parser DOM Interface DOM XML ParserW3C DOMSAX XMLXMLAPI(Class)SAXXMLDOMSAXInterfaceXML Parser

  • DOM (Document Object Model)What is the Document Object Model? The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page. (http://www.w3.org/DOM/)

  • DOM (Document Object Model) Shady Grove Aeolian Over the River, Charlie Dorian :http://w3c.org/

  • DOM API Overview(tree model),XML(random) ,API JavaDoc Overview

  • DOM API // DOM Parser DOMParser parser = new DOMParser(); try { //XML(uri) parser.parse(uri); //( dom tree ) Document doc = parser.getDocument(); // printNode(doc, ""); } catch (IOException e) { System.out.println("Error reading URI: " + e.getMessage()); } catch (SAXException e) { System.out.println("Error in parsing: " + e.getMessage()); }

  • DOM API public void printNode(Node node, String indent) { // Determine action based on node type switch (node.getNodeType()) { case Node.DOCUMENT_NODE: System.out.println("\n"); // recurse on each child NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i=0; i
  • DOM API case Node.ELEMENT_NODE: String name = node.getNodeName(); System.out.print(indent + ""); NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i
  • SAX (The Simple API for XML)XML-DEV mailing list(event-based)XML(sequential)API JavaDoc Overview

  • SAX (The Simple API for XML):htthttp://www.developerlife.com

  • SAX (The Simple API for XML)http://industry.ebi.ac.uk/~senger/xml/docs/tutorial/overview/3_apis.html

  • SAX API ContentHandler contentHandler = new MyContentHandler(); try { // Instantiate a parser XMLReader parser = XMLReaderFactory.createXMLReader (org.apache.xerces.parsers.SAXParser"); // Register the content handler parser.setContentHandler(contentHandler); // Parse the document parser.parse(uri); } catch (IOException e) { System.out.println("Error reading URI: " + e.getMessage()); } catch (SAXException e) { System.out.println("Error in parsing: " + e.getMessage()); }

  • SAX API class MyContentHandler implements ContentHandler { public void startDocument() throws SAXException {class MyContentHandler implements ContentHandler { System.out.println(" begins..."); } public void endDocument() throws SAXException { System.out.println("...Parsing ends."); } : : public void startElement(String amespaceURI,StringlocalName, String rawName, Attributes atts) throw SAXException { System.out.print("startElement: " + localName); }}

  • SAX VS.DOMDOM SAXXMLSAXDOM

  • Xerces-J APIApache XML ProjectAPI OvervieXMLNAME CPU RAMXerces-JDOM ParserSAX Paser

  • Xerces-J ------------ Element(Node) -------- Element(Node) INTEL ---- Text(Node) -------- Element(Node) 64 ------- Text(Node)

  • Castor API Exolab group(www.exolab.org) open source porject Java[tm] objects, XML documents, SQL tables LDAP directories

  • Castor API Overview

  • Castor API -- Using XML1.Generate source code from an XML Schema (org.exolab.castor.builder.SourceGenerator in castor.jar)2.Marshalling and Unmarshalling

  • Castor Source Code Generatorxml schema Java classes,Class Descriptorsclass marshallingframework

  • Castor Source Code Generator

  • Castor Castor XMLSchema (Article.xsd) generateJavaCodeJava Code XML document Method XML document (unmarshalling)XML File (marshalling)

  • JAVA XML Q & A

  • BOOK:java and xml -- Brett McLaughlin,O'REILLYhttp://www.w3.orghttp://www.megginson.com/SAXhttp://www.xml.orghttp://xml.apache.orghttp://castor.exolab.org

    (http://xml.apache.org)