1 / 96

第 6 章 J2EE and The Next Generation

第 6 章 J2EE and The Next Generation. 北航计算机学院 linxl@buaa.edu.cn 2006 年 4 月 1 日. SOAP, UDDI, and more. XML Support. JAX Pack. WS Pack. Web Services. J2EE. J2EE 1.2. J2EE 1.3. J2EE 1.4. J2EE 5.0. 1999. 2000. 2001. 2002. 2003. 2004. 2005. 内容列表. 6.1 XML and J2EE 1.3

reece-davis
Download Presentation

第 6 章 J2EE and The Next Generation

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. 第6章 J2EE and The Next Generation 北航计算机学院 linxl@buaa.edu.cn 2006年 4 月 1 日

  2. SOAP, UDDI, and more XML Support JAX Pack WS Pack Web Services J2EE J2EE 1.2 J2EE 1.3 J2EE 1.4 J2EE 5.0 1999 2000 2001 2002 2003 2004 2005

  3. 内容列表 • 6.1 XML and J2EE 1.3 • 6.2 Web Service and J2EE 1.5 • 6.3 Java EE 5.0 • 6.4 J2EE vs .NET • 6.5 小结

  4. 6.1 XML & J2EE 1.3 • XML • DTD, Schema • XML Parser • J2EE 1.3

  5. [01] <?XML version="1.0" standalone="yes"?> [11] <Students Class=”SY9061”> [12] <Student SId="12345"> [13] <Name>Lin</Name> [14] <Age>20</Age> [15] </Student> [16] <Student SId="12345"> [17] <Name>Lin</Name> [18] </Student> [19] </Students>

  6. What Is XML? • XML was developed by an XML Working Group of the World Wide Web Consortium (W3C) in 1996 • It was chaired by Jon Bosak of Sun Microsystems • XML is a family of technologies: XSL, XSLT, XPath, Xlink, Xpointer, DOM, etc. • XML is license-free, platform-independent and well-supported

  7. The eXtensible Markup Language (XML) is the universal format for structured documents and data on the Web • As with HTML, you identify data using tags (identifiers enclosed in angle brackets, like this: <...>) • The tags are called "markup" • But unlike HTML, XML tags tell you what the data means, rather than how to display it

  8. XML Document • A data object is an XML document if it is well-formed, as defined in XML specification • A well-formed XML document may in addition be valid if it meets certain further constraints • The document is composed of declarations, elements, comments, character references, and processing instructions, all of which are indicated in the document by explicit markup

  9. Well-Formed XML Documents • A "Well Formed" XML document has correct XML syntax • A textual object is a well-formed XML document if it has the correct XML syntax: • It contains one or more elements • There is exactly one element, called the root, or document element • The name in an element's end-tag must match the element type in the start-tag; Names are case-sensitive • Each of the parsed entities which is referenced directly or indirectly within the document is well-formed

  10. XML Document Contents • Processing Instructions • XML declaration • Elements • Tags: Start-Tags, End-Tags • Attributes • Empty-Element Tags • PCDATA • CDATA • White Spaces • Comments Prolog Content

  11. 序言 • XML声明: • XML文档应该以XML声明作为文档的开始,声明中指定了版本、编码等信息 • 文档类型声明: • DOCTYPE声明必须位于XML声明之后,且在任何文档元素之前。但是,XML声明和DOCTYPE声明之间可以插入注释和处理指令。 • 混合项 • 注释 • 处理指令 • 空白字符

  12. 主体 • XML文档的主体是文档元素,每个XML文档都有且仅有一个文档元素,文档元素可以包含子元素(多个)。

  13. 尾声 • 包括注释、处理指令、与/或空白 ,即序言中的混合项

  14. An Example XML Document • <?xml version="1.0" encoding="ISO-8859-1"?> • <tradeBatch> • <trade account="2520034" action="buy" Duration = "good-till-canceled"> • <symbol>SUNW</symbol> • <quantity>1000</quantity> • <limit>20</limit> • <date>2001-03-05</date> • </trade> • <trade account="9240196" action="sell" duration="day"> • <symbol>CSCO</symbol> • <quantity>500</quantity> • <date>2001-03-05</date> • </trade> • </tradeBatch> • <!-- This is a comment -->

  15. 有效的文档 • 有效的文档: • 格式良好 • 包含文档类型声明 • 文档实例符合文档类型声明的约束。有效的文档: • 有效性约束的两种方法: • DTD • Schema

  16. DTD • 文档类型定义(Document Type Definition ,DTD)是XML 1.0规范的一部分 • DTD可以定义词汇和词汇之间的语法规则,从而定义XML文档的结构。

  17. 1: <?XML version="1.0" standalone="yes"?> 2: <!DOCTYPE Students [ 4: <!ELEMENT Students (Student+)> 5: <!ATTLIST Students Class CDATA #REQUIRED> 6: <!ELEMENT Student (Name, Age?)> 7: <!ATTLIST Student SId CDATA #REQUIRED> 9: <!ELEMENT Name (#PCDATA)> 13: <!ELEMENT Age (#PCDATA)> 18: ]> 19: <Students Class=”SY9061”> 20: <Student SId="12345"> 21: <Name>Lin</Name> 22: <Age>20</Age> 23: <Address> 24: <Country>China</Country> 25: <City>BeiJing</City> 26: </Address> 27: </Student> 28: <Student SId="12345"> 29: <Name>Lin</Name> 30: </Student> 31: </Students>

  18. DTD各要素 • ELEMENT –元素 • 元素类型声明 • ATTLIST • 特定元素类型可设置的属性及这些属性的允许值声明 • ENTITY • 可重用的内容声明 • NOTATION • 不需要解析的外部内容(例如:二进制数据)的格式声明,以及用于处理这些内容的外部应用程序

  19. DTD与文档的关联 • DOCTYPE 标记 • 内部DTD子集 • <!DocType catalog[… 内部声明 …]> • 外部DTD子集 • <!DOCTYPE catalog SYSTEM “http://myserver/decs/pubcatalog.dtd”>

  20. 有效性验证 • 利用D T D 和验证有效性的解析器,就能够立即对文档的完整性进行可靠的检查 。如果某个X M L 文档引用了D T D ,验证有效性的解析器应该读取D T D ,并确保文档符合D T D 中描述的语法

  21. DTD的缺点 • DTD有自己的特殊的语法,本身不是XML文档因此,我们编写XML文档时,需要使用两套解析器:一套用来分析XML,判断文档是否格式良好;另一套用于分析DTD,再用分析的结果去检查XML文档,判断XML是否有效。 • DTD只提供了有限的数据类型 • 结构封闭,不具备扩展性。DTD不支持名域机制,因此,每个XML文档只能有一个DTD,从而限制了XML文档的继承和重用。

  22. 2: <!DOCTYPE Students [ 4: <!ELEMENT Students (Student+)> 5: <!ATTLIST Students Class CDATA #REQUIRED> 6: <!ELEMENT Student (Name, Age?)> 7: <!ATTLIST Student SId CDATA #REQUIRED> 9: <!ELEMENT Name (#PCDATA)> 13: <!ELEMENT Age (#PCDATA)> 18: ]> !定义元素和内容模型 1: <?XML version="1.0" standalone="yes"?> 19: <Students Class=”SY9061”> 20: <Student SId="12345"> 21: <Name>Lin</Name> 22: <Age>20</Age> 23: <Address> 24: <Country>China</Country> 25: <City>BeiJing</City> 26: </Address> 27: </Student> 28: <Student SId="12345"> 29: <Name>Lin</Name> 30: </Student> 31: </Students>

  23. [01]<xsd:schema xmlns:xsd=http://www.w3.org/2000/08/XMLSchema [02] targetNamespace=”http://cscw.buaa.edu.cn/Schema-Stud”> [03] <xsd:element name="students" type="StudentsType"/> [09] <xsd:complexType name="StudentsType"> [10] <xsd:sequence> [11] <xsd:element name="student" type="StudentType" maxOccus=”unbounded”/> [12] </xsd:sequence> [13] <xsd:attribute name="class" type="xsd:NMTOKEN"/> [14] </xsd:complexType> [15] <xsd:complexType name="StudentType"> [16] <xsd:sequence> [17] <xsd:element name="name" type="xsd:string"/> [18] <xsd:element name="age" type="xsd:int" minOccus=”0”/> [19] </xsd:sequence> [20] <xsd:attribute name="sId" type="xsd:NMTOKEN"/> [21] </xsd:complexType> [22]</xsd:schema> !名域声明 ! 声明元素 !定义类型

  24. Schema的基本概念 • 类型定义 • 元素声明 • 名域 • ……

  25. 类型定义 <xsd:complexType name="StudentType"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="age“ type="xsd:int“ minOccus=”0”/> </xsd:sequence> <xsd:attribute name=“sId“ type="xsd:NMTOKEN"/> </xsd:complexType> Class StudentType{ String name; int age; String sid; }

  26. 元素声明 • 元素声明: <xsd:element name=“age" type=”xsd:int”/> 对应于java中的变量声明: int age; • 属性声明: <xsd:attribute name=“b" type="xsd:NMTOKEN“ use="fixed" value="US"/> 对应于Java的变量声明 NMTOKEN b;

  27. Namespace • Namespace • Schema中通过元素<schema>的属性targetNamspace指定名域: targetNamespace=“http://www.example.com/PO1” Namespace对应于Java中的package,如: package cscw.mypackage (C#没有package,改成namespace)

  28. Schema • Java程序是类的集合,Schema是类型定义和元素声明的集合 ,因此,Schema可以类比于Java程序

  29. XML文档实例 • 元素实例 • 进一步类推,可以得出XML文档的元素实例类比于Java中的类实例: • student = new StudentType(); • XML文档元素与Java类实例的类比是差别最大的类比关系:前者是静态的文档,以字符形式存在;后者是程序的动态运行,以二进制形式存在于内存中。其实这个差别正是XML得以广泛应用的原因之一:二进制的数据很难在网络的不同应用之间传递,而文本数据则很容易。 • 文档实例 • XML文档实例类比于运行中的Java程序。

  30. Schema与Java类比汇总

  31. Schema与XML文档的关联 [01]<?xml version="1.0"?> [02]<students [03] xmlns:stud=”http://act.buaa.edu.cn/Schema-Stud” [04] class=”SY9061”> [05] <student SId="12345"> [06] <name>Lin</name> [07] <age>20</age> [08] </student> [09] <student sId="345657"> [10] <name>Li</name> [11] </student> [12]</students>

  32. Schema的优点 • 本身是XML文档 • 内建丰富的数据类型;可以方便地定义数据类型 • 支持名域(namespace)

  33. XML文档的处理 • 回调 • 回调是作为一种事件驱动模型工作的。当分析 XML 文档时,某些事件将触发回调方法。 • SAX,用于 XML 的一种简单 API,是这种 XML 使用方法的事实上的标准。 • 树 • 根据XML 文档,创建对应的树状结构。 • 最流行的 API 是文档对象模型 (DOM)。 • 数据绑定 • 将XML Schema编译成一个或多个Java 类,其后,XML对象和Java对象可以相互转换。 • Sun 公司的DataBinding

  34. SAX——Simple API for XML • SAX(Simple Application interface for XML)接口规范是XML分析器和XML处理器提供的较XML更底层的接口。 • SAX诞生是在XML-DEV讨论组上,提出它的原因是有一些情况不适用DOM接口,而且DOM实现太大而且比较慢。 • SAX是一种事件驱动的接口,它的基本原理是,由接口的用户提供符合定义的处理器,XML分析时遇到特定的事件,就去调用处理器中特定事件的处理函数。一般SAX都是JAVA的接口,但其实C++也可以用SAX接口,但C++的分析器比较少。

  35. Parsing • Reading and well-formed'ness checking and validating • Event-driven Parser • You provide the event Handler • Sequential read access only => one-time access • Fast and Lightweight • Documents does not have to be entirely in memory • Does not support modification of document • Started as a Community-driven project • org.xml.sax • SAX is free!

  36. SAX Example • <?xml version="1.0"?> • <doc> • <para>Hello, world!</para> • </doc> • An event-based interface will break the structure of this document down into a series of linear events: • start document • start element: doc • start element: para • characters: Hello, world! • end element: para • end element: doc • end document

  37. SAX需要用户提供一下几个处理器类的实现: • DocumentHandlerXML:文件事件的处理器; • DTDHandler:DTD中事件的处理器; • ErrorHandler:出错处理器。 • 写程序就是以下这么几步了: • 首先需要从这几个类继承出自己的子类, • 重载其中自己感兴趣的事件的处理方法。 • 向分析器,注册此处理器类,其实告诉分析器使用你的处理器。 • 启动分析器。

  38. SAXParser parser=new SAXParser(); //注册事件处理器 parser.setDocumentHandler(new MyDocumentHandler()); parser.setErrorHandler(new MyErrorHandler()); //解析文档 try{ parser.parser(fn); }catch(Exception e){ System.out.println(“Exception invoking parser.”); }

  39. DOM: Document Object Model • The Document Object Model (DOM) is an application programming interface (API) for processing well-formed and valid XML documents • It defines the logical structure of documents and the way a document is accessed and manipulated • With the Document Object Model, programmers can build documents, navigate their structure, and add, modify, or delete elements and content

  40. DOM Example:Example table, taken from an HTML document • <TABLE> • <TBODY> • <TR> • <TD>Shady Grove</TD> • <TD>Aeolian</TD> • </TR> • <TR> • <TD>Over the River, Charlie</TD> • <TD>Dorian</TD> • </TR> • </TBODY> • </TABLE>

  41. DOM规范定义了对象和接口的集合,通过他们可以访问和操作文档对象DOM规范定义了对象和接口的集合,通过他们可以访问和操作文档对象 • DOM定义了12种节点类型,有些类型的节点有不同的子节点,另外一些节点没有再低层的子节点,所有这些节点构成了树型结构,描述了文档的逻辑结构。 • Document • Element • Attr • Text

  42. import java.xml.parsers.*; import org.w3c.dom.*; DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); factory.setValidating(true); //构造解析器 DocumentBuilder builder = factory.newDocumnetBuilder(); //解析文档,得到文档对象 Document doc=builder.parse("mydoc.xml");

  43. JAXB • Java Architecture for XML Binding • JAXB provides a fast and convenient way to create a two-way mapping between XML documents and Java objects • WARNING: Significant changes could occur between the current working draft and the final 1.0 specification

  44. Motivations of JAXB • SAX and DOM • Too low-level APIs • Requires XML knowledge • Hard to program • You have to build your high-level Java objects yourself • Hard to maintain • Schema change forces reprogramming • Forces a lot of coding • Need for APIs that provide higher-level abstraction

  45. XML Binding——What Is It? • Map components of an XML document to in-memory objects • Object should represent intended meaning of document according to its schema • What should the classes be? • Some cases obvious: String, Date, Vector. • In general, classes should be generated directly from the schema, i.e.: • A Java. technology-level binding of the schema

  46. Schema vs. Class

  47. The Binding Framework Cycle

  48. Others • Namespace in XML • XML Information set • Canonical XML • XPath • XML Digital Signature • XML Encryption • XSL • XSLT • XPointer • XLink • XBase

More Related