Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal First ... A Quote... ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Fall 2000.... XML in Server-side Architecture ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • External Interface – Request/Response XML XML Service XML Content • Service brokering Web Services or XML over HTTP – Select service • Content brokering – Broker content by service • Business Layer Service Broker Service Business Component Layer – Business Components • Enterprise Java Beans • JDO • Data Layer Content Broker Data Layer Server-side XML Patterns ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• •XML Bridge •Service Broker •Content Broker •XML Parser Factory WeddingNetwork IREG Transaction Progress Exporting Order Status to WeddingNetwork.. Or de rS tat u s Enterprise server-side Java component technology. Business logic written to standard ShopML Servlets manage HTML interface. Internet Retailer Enterprise Gateway Retail Client Gateway Service Interface ShopML Request ShopML Filter Business Logic RMI ShopML service handler Order Status Corba / IIOP Client Data Receive Orders ShopML Response Product Catalog Retail client www.wedding network.com Sync Registry Wedding Network Website! Servlet Recieve Order Receive Registry Business Logic (EJB) Servlet Order Retailer Order Registry Product Retail Client IGR Accounting Registry Java App Server The Internet Retail DB Retail DB Web Application Server Retail Partners Securely synchronize Orders, Product Catalogs, and Registry with IGR Wedding Network Open, standardized interface for integration into existing business processes. Gateway provides components to handle proprietary and ShopML (IGR standard XML) The Internet Registry Orders Products Copyright © 2000 All rights reserved. Deb Ayers and Tom Marrs Content Broker: Architecture Pattern ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Content Broker Content Broker (cont’d) ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• ShopMLContentHandlerFactory -contentHandlersTable : Hashtable +makeShopMLContentHandler(in serviceType : String) : IShopMLContentHandler 1 1 «interface» IContent +getContent() : Element * +creates 1 «utility» Hashtable «interface» IShopMLContentHandler +addToContent(in tagName : String, in fieldValues : BaseDictionary) +getProperties() : Enumeration QueryOrderShopMLContentHandler «uses» OrderShopMLContentHandler «uses» QueryShopMLContentHandler Fast Forward.... XML Processing 2003 ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• What is XML Beans ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• A simple, comprehensive, fast tool for accessing XML from Java. • Java/XML Binding – Use XML Schema to get Java Classes/Interfaces – Simple Getter/Setter “java bean-like” model • Direct XML level access • Schema/type reflection and validation Three major APIs 1. XmlObject 2. XmlCursor 3. Schema Type System XML Beans and BEA ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • BEA Donated to Open Source in July 2003 – [email protected] • Independent of other BEA Technologies – Download stand-alone XML Dev Kit http://dev2dev.bea.com/technologies/xmlbeans/overview.jsp – Can use within any Java application • Used by Bea’s Weblogic 8.1 Platform products – Needed strong Java/Xml development tool – Had good xml -> java development experience What Are the Alternatives ? ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • DOM (Document Object Model) or JDOM – Tedious to program – Typically need to wrapper with Java Classes to Hide Complexity • SAX (Simple API for XML) – Simple to program with a loss of control – rigid • Castor – Requires marshalling/unmarshalling – Lose access to original XML document • JAXB – Very similar to XMLBeans and will evolve to be inclusive – David Bau the chief architect of XmlBeans is a committer on Apache XmlBeans will be on the JAXB 2.0 Expert group (JSR 222) XMLBeans API Approach ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• XML APIs “100% XML” DOM, SAX, Xerces, XPath…: Robust but Clumsy XML Translators “Typed APIs” MindElectric, Breeze, MS .NET, JAXB: Easy but Fragile XMLBeans Both Typed Java PLUS 100% XML and Schema Easy and Robust XML Beans Goals ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • Ease of use Target 100% support for XML Schema High Performance Non-lossy approach: NO underlying XML lost Ability to work at different levels: – Java bound – Xml cursor – Validation/Schema level 100% Schema support ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • Namespaces • All types • Inheritance (by restriction, by list, by union, by extension) • All content models • Includes/imports • Key/KeyRef XMLBeans Data Model ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Strongly typed methods Class casts, Inheritance Natural Java types Convenient Typed JavaBean Tree Typed Java data Object Access Full XML infoset Cursor Access High speed parser Lightweight cursor access Synchronized Complete XML Token Stream Validation High speed saver Streamed XQuery Using XML Beans Compiling Schema ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• XML Schema(s) Imported/Included XML Schema(s) XmlTypes.jar Java Interfaces XML Beans Schema Compiler (Schemas are loaded as XML Beans using schema for schema) Java Classes XML Schema Binary Files (.xsb) (Put on classpath to access generated classes) Simple Demo Schema to Interface/Class generation ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • Namespace to Package Name – General JAXB type naming rules – Can override using .xsdconfig file • Schema Names to Java Names – General JAXB type naming rules – Can override using .xsdconfig file • Named Types – become standalone interfaces/classes • Global Elements – Synthesized type at the top level: e.g., FooDocument • Local Elements (Complex) – Inner classes of their parent Schema to Interface/Class generation (Example) ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <xs:schema targetNamespace="http://openuri.org/easypo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:po="http://openuri.org/easypo" elementFormDefault="qualified"> <xs:element name="purchase-order"> <xs:complexType> <xs:sequence> <xs:element name="customer" type="po:customer"/> <xs:element name="date" type="xs:dateTime" /> <xs:element name="line-item" type="po:line-item" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="shipper" type="po:shipper" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="customer"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="line-item"> <xs:sequence> <xs:element name="description" type="xs:string"/> <xs:element name="per-unit-ounces" type="xs:decimal"/> <xs:element name="price" type="xs:double"/> <xs:element name="quantity" type="xs:int"/> </xs:sequence> </xs:complexType> <xs:complexType name="shipper"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="per-ounce-rate" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:schema> target namespace -> java package: openuri.org.easypo global element: PurchaseOrderDocument class PurchaseOrderDocument.PurchaseOrder inner class named type: Customer class LineItem class Shipper class XML Beans Interfaces ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • All descend from XmlObject • getters and setters similar to java beans • Two ways to construct both using Factory: 1. FooDocument doc = FooDocument.Factory.newInstance() 2. FooDocument doc = FooDocument.Factory.parse() Creating an XML Bean from an existing source (Factory.parse) ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.parse() • • • • • • • File InputStream Node Reader String URL XMLInputStream PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.parse(…)); PurchaseOrderDocument.PurchaseOrder po = doc.getPurchaseOrder(); Customer c = po.getCustomer(); String custName = c.getName(); Parsing an XML Document Behind the scenes ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• PurchaseOrderDocument.Factory.parse() XmlToken Source 1. Consolidate QNames <name> splay tree 2. Build XmlStore <customer>\n <address> <purchase-order xmlns=“…”>\n Picollo <shipper>\n <date>\n <name> <per-ounce-rate> <purchase-order xmlns="http://openuri.org/easypo"> <customer> XML <name>Gladys Kravitz</name> <address>Anytown, PA</address> </customer> 3. Create Root XmlObject XmlObject <date>2001-12-17T09:30:47-05:00</date> <shipper> <name>UPS</name> <per-ounce-rate>0.74</per-ounce-rate> </shipper> </purchase-order> XmlObject PurchaseOrderDocument Char Buffer XmlLoader XmlStore Creating an XML Bean From Scratch (Factory.newInstance()) ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Use Factory.newInstance() to create new empty XML Bean PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.newInstance(); Use .addNewXXX to add a new complex types • Local element inner class, named type standalone class PurchaseOrderDocument.PurchaseOrder po = doc.addNewPurchaseOrder(); Customer c = po.addNewCustomer(); Simple types/content have direct setters c.setAddress("123 Main St"); c.setName("Dave Remy"); Demo Reading an XML Bean ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Use Factory.parse() to get Document and then read using typical java techniques: getter for complex types (objects) File f = new File("c:\text.xml"); PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.parse(f); PurchaseOrderDocument.PurchaseOrder po = doc.getPurchaseOrder(); Customer c = po.getCustomer(); getter for simple String address = c.getAddress(); types String name = c.getName(); LineItem[] liArray = po.getLineItemArray(); for (int i = 0; i < liArray.length; i++) { getter for .... repeating element } (array) Reading an XML Bean Behind the scenes ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Customer c = myPurchaseOrder.getCustomer(); Lazy instantiation: Complex Type: (Customer) 1. 2. If Object already in store return it Otherwise create object and retrieve info from XML Store String name = c.getName(); Simple Type: (Name) 1. get from the parent object (Customer) directly Object Store Customer -name Customer -address -name -address XML Store … <customer> <name> Fred </name> <address> 123 main </address> </customer> … Updating/Setting Xml Beans ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.newInstance(); PurchaseOrderDocument.PurchaseOrder po = doc.addNewPurchaseOrder(); Customer c = po.addNewCustomer(); setter for simple c.setName(“Dave Remy”); types Address mailingAddress = c.addNewAddress(); mailingAddress.setType = “mailing”; mailingAddress.setStreet1 = “123 Main St”; mailingAddress.setCity = “Kirkland”; mailingAddress.setState = “WA”; mailingAddress.setPostal = “98040”; Address billingAddress = c.addNewAddress(); billingAddress.set(mailingAddress); setter for complex billingAddress.setType = “billing”; type Updating XML Beans Behind the scenes ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• setCustomer(myCustomer); Object Store c.setName(“Dave”); Customer -name -address xmlCursor.selectPath(“.//customer”); Updates to XML Store work similarly - “invalid bits” used to synchronize XML Store … *invalid* <customer> <name> Fred </name> <address> 123 main </address> </customer> … Validating a Document ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Just call validate() on XmlObject … document.validate(); To capture list of errors pass in collection as an option: ArrayList errors = new ArrayList(); validateOptions.put(XmlOptions.ERROR_LISTENER, errors); boolean isValid = doc.validate(validateOptions); if (!isValid) { Iterator i = errors.iterator(); while (i.hasNext()) { System.out.println("Validation Error: " + i.next().toString()); } } Reality Check ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Using XML Cursor XML Cursor ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • DOM like access technique Token oriented High speed (Uses the Xml Store) Can get to any part of the XML document (NO Loss) Untyped Cursor concept – represents a location in the document <Foo> <Bar>some Text</Bar> </Foo> Cursor at <Bar> element Reading an Xml Document with XML Cursor ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• public String discoverTokenTypes(XmlObject xmlDoc) throws Exception { XmlCursor documentCursor = xmlDoc.newCursor(); use .newCursor to create cursor while (documentCursor.hasNextToken()) { ... documentCursor.toNextToken(); } documentCursor.dispose(); } A new Cursor can be created on ANY XmlObject (using xmlObject.newCursor()) – mix Xml Cursor with strongly typed Xml Objects token model Types of Tokens ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • Document Start Document END Element Start Element End Text Attribute Comment Namespace Processing Instructions Navigating in Xml Cursor ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • hasNextToken(); toNextToken() toChild(QName) toFirstChild() toFirstAttribute() toNextChild() toNextSibling() Etc., etc., etc. Schema Type System ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • You can fully introspect on the schema type system PurchaseOrderDocument.schemaType(); Possible uses: • Generate Sample XML • Show constraints for a particular field – Enumerations – Facets – Etc • Other tool type uses … Whence cometh XQuery ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • XQuery is derived from an XML query language called Quilt • Quilt borrowed features from: – – – – – Xpath 1.0 XQL XML-QL SQL OQL What is XQuery. . . ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • XQuery is a query language designed to query XML documents in a simple and concise manner. • XQuery contains, as a subset, XPath 2.0. A valid XPath query and a valid XQuery query will return the same result. . . . What is XQuery ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • The basic building block of XQuery is an expression. – This is similar to any programming language. • XQuery is a functional language that allows nesting and iteration. • XQuery is a strongly typed language supporting – strings, integers, floating point, and boolean. Selecting Nodes ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • XPath uses a virgule delimited path for specifying nodes in a Location Path. • Specify the Context – from root node • / – descendants • // Root Location Path <bib> ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <book year="1922"> <title>Ulysses</title> <author><last>Joyce</last><first>James</first></author> <publisher>Shakespeare and Company</publisher> <price>8.24</price> </book> <book year="1925"> <title>The Great Gatsby</title> <author><last>Fitzgerald</last><first>F. Scott</first></author> <publisher>Scribner</publisher> <price>5.25</price> </book> <book year="1978"> <title>The C Programming Language</title> <author><last>Kernigan</last><first>Brian</first></author> <author><last>Richie</last><first>Dennis</first></author> <publisher>Prentice-Hall</publisher> <price>9.95</price> </book> </bib> / Descendent Context—XML Content <bib> ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <book year="1922"> <title>Ulysses</title> <author><last>Joyce</last><first>James</first></author> <publisher>Shakespeare and Company</publisher> <price>8.24</price> </book> <book year="1925"> <title>The Great Gatsby</title> <author><last>Fitzgerald</last><first>F. Scott</first></author> <publisher>Scribner</publisher> <price>5.25</price> </book> <book year="1978"> <title>The C Programming Language</title> <author><last>Kernigan</last><first>Brian</first></author> <author><last>Richie</last><first>Dennis</first></author> <publisher>Prentice-Hall</publisher> <price>9.95</price> </book> </bib> /bib Selecting Elements <bib> ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <book year="1922"> <title>Ulysses</title> <author><last>Joyce</last><first>James</first></author> <publisher>Shakespeare and Company</publisher> <price>8.24</price> </book> <book year="1925"> <title>The Great Gatsby</title> <author><last>Fitzgerald</last><first>F. Scott</first></author> <publisher>Scribner</publisher> <price>5.25</price> </book> <book year="1978"> <title>The C Programming Language</title> <author><last>Kernigan</last><first>Brian</first></author> <author><last>Richie</last><first>Dennis</first></author> <publisher>Prentice-Hall</publisher> <price>9.95</price> </book> </bib> /bib/book Selecting Nodes within Nodes <bib> ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <book year="1922"> <title>Ulysses</title> <author><last>Joyce</last><first>James</first></author> <publisher>Shakespeare and Company</publisher> <price>8.24</price> </book> <book year="1925"> <title>The Great Gatsby</title> <author><last>Fitzgerald</last><first>F. Scott</first></author> <publisher>Scribner</publisher> <price>5.25</price> </book> <book year="1978"> <title>The C Programming Language</title> <author><last>Kernigan</last><first>Brian</first></author> <author><last>Richie</last><first>Dennis</first></author> <publisher>Prentice-Hall</publisher> <price>9.95</price> </book> </bib> /bib/book/title Using Descendent References <bib> ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <book year="1922"> <title>Ulysses</title> <author><last>Joyce</last><first>James</first></author> <publisher>Shakespeare and Company</publisher> <price>8.24</price> </book> <book year="1925" city="New York"> <title>The Great Gatsby</title> <author><last>Fitzgerald</last><first>F. Scott</first></author> <publisher>Scribner</publisher> <price>5.25</price> </book> <book year="1978"> <title>The C Programming Language</title> <author><last>Kernigan</last><first>Brian</first></author> <author><last>Richie</last><first>Dennis</first></author> <publisher>Prentice-Hall</publisher> <price>9.95</price> </book> </bib> /bib//last Obtaining Data in an Indexed Reference <bib> ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <book year="1922"> <title>Ulysses</title> <author><last>Joyce</last><first>James</first></author> <publisher>Shakespeare and Company</publisher> <price>8.24</price> </book> <book year="1925"> <title>The Great Gatsby</title> <author><last>Fitzgerald</last><first>F. Scott</first></author> <publisher>Scribner</publisher> <price>5.25</price> </book> <book year="1978"> <title>The C Programming Language</title> <author><last>Kernigan</last><first>Brian</first></author> <author><last>Richie</last><first>Dennis</first></author> <publisher>Prentice-Hall</publisher> <price>9.95</price> </book> </bib> /bib/book[title/text()=“Ulysses”] A Simple XQuery Program ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• <CUSTOMERS> { for $doc in document ("XM-BB-C")/db/CUSTOMER let $customer := $doc where $doc/STATE = "TX" return $customer } </CUSTOMERS> Determines # of tuples Joining XML Documents ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • One of the powerful features available in XQuery is the ability to do joins of XML documents. • A join unites two XML documents based on a shared key value and outputs the desired results. ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Last But Not Least XQuery and XPath with XML Beans ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • Can run an XQuery on any XmlObject using myXmlObject.execQuery(“<Items>{./line-items/*}</Items>”); Returns array of untyped XmlObjects • If shape is known (.xsd) then cast using myXmlObject.changeType(FooDocument.type()); • Can run Xpath using myXmlObject.selectPath(“./line-items/*”); Returns array of typed XmlObjects Demo More XML Beans, XmlCursor, XQuery Content Broker : Architecture Pattern ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Case Solved Content Broker References ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • XML Case Study – When Click Meets Brick • http://dev2dev.bea.com Q&A