1 / 30

第五章

第五章. XML 处理. 回顾. EJB2.0 不仅提供了远程接口,还提供了本地接口,以便 EJB 的客户端程序可以使用远程或本地接口去访问 EJB 当企业 Bean 必须在分布式环境中使用时,它将定义一个远程客户端 EJB 远程客户端的特性包括:远程客户端可在不同的机器上运行,它可以是 Web 组件、应用程序客户端或企业 Bean 本身 在本地客户端视图中,企业 Bean 和客户端必须驻留在同一个 JVM 上 在 J2EE 应用中,往往使用实体 Bean 封装业务实体,使用会话 Bean 封装业务逻辑,而使用会话 Bean 去调用实体 Bean.

kesler
Download Presentation

第五章

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. 第五章 XML 处理

  2. 回顾 • EJB2.0不仅提供了远程接口,还提供了本地接口,以便EJB的客户端程序可以使用远程或本地接口去访问EJB • 当企业 Bean 必须在分布式环境中使用时,它将定义一个远程客户端 • EJB远程客户端的特性包括:远程客户端可在不同的机器上运行,它可以是 Web 组件、应用程序客户端或企业 Bean 本身 • 在本地客户端视图中,企业 Bean 和客户端必须驻留在同一个 JVM 上 • 在J2EE应用中,往往使用实体Bean封装业务实体,使用会话Bean封装业务逻辑,而使用会话Bean去调用实体Bean

  3. 目标 • 理解 XML解析器 • 运用SAX解析XML文件 • 运用 DOM解析XML文件 • 运用XSL实现XML 转换

  4. XML 解析器简介2-1 • XML解析器是指可以解析XML文件的程序 XML 数据 解析器 基于 XML的 应用程序

  5. XML 解析器简介2-2 • XML解析器常见类型 • SAX解析器 (Simple API for XML,用于XML 的简单API) • DOM解析器 (Document Object Module,文档对象模型)

  6. SAX解析器 2-1 输入 XML 文档 特定于应用程序的处理器 XML 解析器 输出 SAX API XML 处理器

  7. SAX解析器 parse(...) SAX解析器 2-2 • SAX 工作原理:顺序解析,事件驱动 程序 XML 事件 输入 startDocument(...)startElement(...)characters(...)endElement( )endDocument( ) 用户 Sax 解析器 提供的 main(...) 处理程序

  8. 利用SAX解析XML的常用步骤 • 编写一个类继承org.xml.sax.helpers.DefaultHandler。 • 获取javax.xml.parsers.SAXParserFactory;类的一个实例 • 创建一个javax.xml.parsers.SAXParser对象 • 解析XML文件

  9. 使用SAX解析XML的示例6-1 选定“Add saved file to project”选项 选择“File”“New”或右键单击 .jpx 文件并选择“New”“File” 单击“OK”按钮 在“Name”文本框中键入 Books, 并从“Type”下拉式列表框中选择“xml” 选择要将文件保存到的工程目录 新建一个名为 XMLSAXParse.jpx 的工程

  10. 使用SAX解析XML的示例6-2 打开文件编辑器并添加以下文本: <?xml version = "1.0"?> <library> <book> <booktitle>史记</booktitle> <author>司马迁</author> <price>199.00</price> </book> <book> <booktitle>平凡的世界</booktitle> <author>路遥</author> <price>56.80</price> </book> <book> <booktitle>西方经济学</booktitle> <author>保罗.萨缪而森</author> <price>98.80</price> </book> </library> 通过选择“File”“Save Project ‘XMLSAXParse.jpx’”来保存此工程

  11. 使用SAX解析XML的示例6-3 • 使用 SAX 处理程序向导 键入MySaxParser1 作为类名称 选择“File”“New”,打开“Object Gallery”对话框 选定 ContentHandler 接口并展开 ContentHandler 节点 单击“Finish”按钮 单击“OK”按钮 在左窗格中选择“XML”,然后在右窗格中选择“SAX Handler” 选择以下五个选项:characters、endDocument、endElement、 startDocument 和 startElement,并为它们创建方法

  12. 使用SAX解析XML的示例6-4 • 该向导生成一个名为 MySaxParser1.java 的默认 SAX 解析器文件,然后将其添加至工程,并在编辑器中打开 • 修改main方法,其关键代码如下页

  13. 使用SAX解析XML的示例6-5 try { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); MySaxParser1 MySaxParser1Instance = new MySaxParser1(); SAXParser parser = parserFactory.newSAXParser(); parser.parse(uri, MySaxParser1Instance); } catch (IOException exception) { exception.printStackTrace(); } catch (SAXException exception) { exception.printStackTrace(); } catch (ParserConfigurationException exception) { exception.printStackTrace(); } catch (FactoryConfigurationError exception) { exception.printStackTrace(); }}

  14. 使用SAX解析XML的示例6-6 public void characters(char[] ch, int start, int length) throws SAXException { String strObj = new String(ch, start, length); if (!strObj.startsWith("\n")) System.out.println(" 值: " + strObj); } public void endDocument() throws SAXException { System.out.println("解析xml文档结束\n"); } public void endElement(String url, String localName, String qName) throws SAXException { System.out.println(qName + "元素解析结束\n"); } public void startDocument() throws SAXException { System.out.println("解析xml 文档开始: \n"); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(qName + " 元素解析开始"); }

  15. DOM解析器 2-1 XML 构建树 输入 文档 生成器 • 文档对象模型 (DOM) • 通过遵循树结构来访问 XML文档 • 由元素节点和文本节点组成 • 可来回遍历该树 • 更大的内存要求 执行操作的 DOM

  16. 对象 文本 对象 对象 对象 对象 对象 DOM解析器2-2 解析器 应用程序 XML 数据 文档“对象”

  17. DOM工作原理 DocumentBuilder Factory .newDocumentBuilder() .newDocument() .parse(”f.xml”) f.xml

  18. 使用DOM解析XML的示例 5-1 新建一个名为 XMLDOMParse.jpx 的工程。 选定“Add saved file to project”选项 选择“File”“New”或右键单击 .jpx 文件。 单击“OK”按钮 选择“New”“File”,此时将打开“Create a New File”对话框。 在“Name”框中键入 NewBook,并从“Type”下拉式列表框中选择“xml”

  19. 使用DOM解析XML的示例 5-2 在打开的文件编辑器窗口中添加以下文本: <?xml version = "1.0"?> <library> <book> <booktitle>京华烟云</booktitle> <author>林语堂</author> <price>99.00</price> </book> <book> <booktitle>老人与海</booktitle> <author>海明威</author> <price>50.00</price> </book> <book> <booktitle>罗密欧与朱丽叶</booktitle> <author>莎士比亚</author> <price>80.00</price> </book> </library> 添加 ParseDomDocument 作为类名称 在“Object Gallery”对话框左边的窗格中选择“General”, 然后在右边的窗格中选择“Class” 单击“OK”按钮 单击“OK”按钮 单击“File”“New”,此时将显示“Object Gallery”对话框

  20. 使用DOM解析XML的示例 5-3 public static ArrayList getBookArrayList(String uri) { ArrayList list = new ArrayList(); try { DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder DBbuilder = DBfactory.newDocumentBuilder(); Document doc = DBbuilder.parse(uri); NodeList nodeList = doc.getElementsByTagName("book"); for (int i = 0; i < nodeList.getLength(); i++) { String booktitle = doc.getElementsByTagName("booktitle").item(i). getFirstChild().getNodeValue(); String author = doc.getElementsByTagName("author").item(i). getFirstChild().getNodeValue(); String price = doc.getElementsByTagName("price").item(i).getFirstChild(). getNodeValue(); Book book = new Book(); book.setBookTitle(booktitle); book.setAuthor(author); book.setPrice(price); list.add(book); } } catch (Exception e) { e.printStackTrace(); } return list; }

  21. 使用DOM解析XML的示例 5-4 • 在 main() 方法中添加代码 • 获取一个ArrayList对象 • 遍历该对象 • 输出每个Book对象的值 • 把每个Book对象写入到XML文件中

  22. 使用DOM解析XML的示例 5-5 单击“File”“Save Project ‘XMLDOMParse.jpx’” 右键单击 ParseDomDocument.java 并选择“Run using defaults”

  23. SAX 与 DOM 的比较 DOM SAX 事件驱动 易于修改文档 • 适用于小型文档 • 多次操作XML数据 • 适合大型文档 • 节省大量内存空间 加载完整的 XML文档 有限的标准功能

  24. XSL 概述 • XSL(eXtensible Stylesheet Language),称为可扩展样式表语言。 • 用途: 转换和表示 XML 文档,与CSS类似。 • 用法: <?xml-stylesheet type = "text/xsl" href =”xsl文件路径"?>

  25. XSL 文件的写法 <?xml version = "1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match = "/"> <xsl:value-of select=”student” /> </xsl:template> </xsl:stylesheet>

  26. 创建 XSL文件以转换XML数据 4-1 <?xml version = "1.0"?> <?xml-stylesheet type = "text/xsl" href = "Customer_format.xsl "?> <Customers> <customer> <id>999</id> <name>王小三</name> <address>海淀区成府路207号</address> <city>北京</city> <phone>13908888888</phone> </customer> <customer> <id>888</id> <name>李小四</name> <address>梁家巷888号</address> <city>成都</city> <phone>13909999999</phone> </customer> </Customers> 创建一个名为 StyleSheetTransformation.jpx 的工程 • 创建样式表文件 <?xml version = "1.0" ?> <xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method = "html" omit-xml-declaration = "yes"/> <xsl:template match = "/"> <xsl:apply-templates/> </xsl:template> <xsl:template match = "Customers"> <table border = "1" width = "70%"> <xsl:for-each select = "customer"> <tr> <td> <xsl:value-of select = "id"/></td> <td> <xsl:value-of select = "name"/></td> <td> <xsl:value-of select = "city"/> </td> <td> <xsl:value-of select = "phone"/> </td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> 选择“File”“New”或右键单击 .jpx 文件并选择“New”“File” 选择“File”“New”或右键单击 .jpx 文件并选择“New”“File” 在“Name”文本框中键入 CustomerDetail_format, 并从“Type”下拉式列表框中选择“xsl” 在“Name”文本框中键入 CustomerDetails, 并从“Type”下拉式列表框中选择“xml” 选择要保存文件的工程目录 选择要保存文件的工程目录 选定“Add saved file to project”选项 选定“Add saved file to project”选项 单击“OK”按钮。 单击“OK”按钮 单击“File”  “Save Project StyleSheetTransformation.jpx” 在打开的文件编辑器窗口中添加以下文本: • 在打开的文件编辑器中添加以下代码:

  27. 创建 XSL文件以转换XML数据 4-2 • 启用 XML 浏览器 选择“Tools”“Preferences”,此时将打开“Preferences”对话框 在左窗格中选择“XML”,并选定“Enable browser view” 点击“OK”按钮

  28. 创建 XSL文件以转换XML数据 4-3 • 将样式表与 XML 文档关联 选择“Transform View”选项卡。 选择“Transform View”工具栏上的“Add StyleSheet”按钮。 此时将出现“Configure node stylesheets”对话框。 单击“Add”按钮,此时将打开“Choose XSL file”对话框。 选择 xsl 文件。 单击“OK”按钮。 此时“Configure node stylesheets”对话框中将出现文件名。 单击“OK”按钮

  29. 创建 XSL文件以转换XML数据 4-4 • 使用样式表转换文档 单击“Default stylesheet”按钮关闭树型视图。 从工具栏旁边的下拉式列表框中选择 CustomerDetail_format.xsl。 “Transform View”显示由已应用的样式表确定的转换文档。

  30. 总结 • XML解析器是可理解为解析XML数据的程序 • 用于XML 的简单 API (SAX) 是一组操作 XML 的 API • 文档对象模型(DOM) 是一个 API,它用于表示文档,并访问和操作组成文档的各种元素 • SAX 使用一种事件机制,程序员必须对方法进行编码以处理由解析器引发的事件,而 DOM 解析 XML 文档并返回 Document 类的实例 • 可扩展样式语言 (XSL) 用于转换和表示 XML 文档

More Related