1 / 27

Java Access XML

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

aretha-rios
Download Presentation

Java Access 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. Java Access XML 數位芝麻網路公司(www.d11e.com) 軟體工程師 陳威誌(cwc@d11e.com)

  2. 課程目的 • 瞭解DOM (Document Object Model)、SAX(The Simple API for XML)及Xerces、Castor等API,並以 JAVA 處理 XML 文件,實作如何存、取及動態產生XML文件內容。

  3. 議題 • 前言 • DOM • SAX • Xerces-J • 實作 • Castor • 實作

  4. 前言 • 學習JAVA及 XML,我們會有一個理想,也就是藉由 JAVA跨平台及XML不受程式語言及資料庫類別的限制的特性達到 JAVA + XML = Portable Code + Portable Data 要達到這樣的目的,我們必須有一些工具可以幫助我們有效的處理 XML 的文件,這也就是今天研習的主要議題。

  5. DOM 及 SAX 簡述 • W3C 定義了 DOM (Document Object Model) 的標準,並定義了一組API (目前只有 Interface),讓大家在處理XML時,有一個共同的標準。於是各家( 如 IBM、SUN…等)有心發展XML Parser 的廠商、組織,便可以執基於這一個標準,以實作 DOM 的 Interface 來發展符合 DOM 規範的 XML Parser。W3C的著眼點在於標準的建立。 • 相對於 DOM,SAX 是由一群對XML事務有興趣的人,為了解決處理XML文件的需求,自行發展出一組API,它本身已有實作部份的類別(Class)。同時SAX處理XML文件確有其異於DOM的優點存在,因此也有許多組織實作SAX的Interface來繼續發展更方便XML Parser。

  6. 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/)

  7. DOM (Document Object Model) <TABLE> <TBODY> <TR> <TD>Shady Grove</TD> <TD>Aeolian</TD> </TR> <TR> <TD>Over the River, Charlie</TD> <TD>Dorian</TD> </TR> </TBODY> </TABLE> 圖文出處:http://w3c.org/

  8. DOM API Overview • 執基於樹狀模型(tree model),提供對於XML文件的隨機式(random)存取 ,並且能夠動態修改文件的內容。 • API JavaDoc Overview • 舉例說明

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

  10. 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("<xml version=\"1.0\">\n"); // recurse on each child NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i=0; i<nodes.getLength(); i++) { printNode(nodes.item(i), ""); } } break;

  11. DOM API 應用實例 case Node.ELEMENT_NODE: String name = node.getNodeName(); System.out.print(indent + "<" + name); : System.out.println(">"); NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i<children.getLength(); i++) { printNode(children.item(i), indent + " "); } } System.out.println(indent + "</" + name + ">"); break; : }

  12. SAX (The Simple API for XML) • 由XML-DEV mailing list的成員共同發展。 • 提供一個事件導向(event-based)的架構來對XML文件做循序式(sequential)的存取。 • API JavaDoc Overview • 舉例說明

  13. SAX (The Simple API for XML) 圖文出處:htthttp://www.developerlife.com

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

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

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

  17. SAX VS.DOM DOM 將整份文件化為樹狀結構存於暫存區域 中,可以作隨機的存取,並且效率也較循序 式的SAX為高。但是由於整份文件在暫存區 中,當XML文件過大時,系統資源的耗費可 想而知。因此在取捨SAX與DOM時,效率與系 統資源的平衡是一個關鍵。

  18. Xerces-J API • Apache XML Project的一部份 • API Overvie • 實作:我們會有一份記錄電腦訂單資料的XML文件,包含客戶姓名「NAME」、 「CPU」、 「RAM」三個欄位。我們將利用Xerces-J中的DOM Parser來新增一位客戶的資料,並用SAX Paser來計算這份文件中共有幾位客戶。

  19. Xerces-J 實例練習 <CWC> ------------  Element(Node) <CPU> --------  Element(Node) INTEL ----  Text(Node) </CPU> <RAM> --------  Element(Node) 64 -------  Text(Node) </RAM> </CWC>

  20. Castor API 簡介 • 由Exolab group(www.exolab.org)所發展的一個 open source 的 porject。 • 目的在提供一個簡便的方法,來處理 Java[tm] objects, XML documents, SQL tables 和 LDAP directories 之間的轉換與互動。

  21. Castor API Overview

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

  23. Castor Source Code Generator • 會以xml schema定義的資料結構來產生一組 Java classes,同時產生 Class Descriptors,對於產生的class 提供 marshallingframework必須參考到 的一些資訊。

  24. Castor Source Code Generator

  25. Castor 實例練習 在這個練習中,我們會以Castor 將一個XML Schema (Article.xsd) generate出一組Java Code,這組自動產生的Java Code即包含處理 這個 XML document 的資料所需的一些基本的 Method,再以這些自動產生的物件來讀取 XML document (unmarshalling),並在修改資料後 產生一個新的XML File (marshalling)。

  26. 總結 • JAVA 與 XML 的應用非常廣泛,以上所研討的部份,可以實際應用於各種跨平台、跨資料庫的系統間的整合,以及動態文件的產生。 • Q & A

  27. 取得更多的相關資訊 • BOOK:java and xml -- Brett McLaughlin,O'REILLY • http://www.w3.org • http://www.megginson.com/SAX • http://www.xml.org • http://xml.apache.org • http://castor.exolab.org

More Related