53
Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

Embed Size (px)

Citation preview

Page 1: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

Innovations and Standards for XML Processing in Java

XML Beans and XQuery

Deb AyersPrincipal

Page 2: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

First ... A Quote...

Page 3: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Fall 2000....XML in Server-side Architecture

• External Interface– Request/Response XML

• Service brokering– Select service

• Content brokering– Broker content by

service

• Business Layer – Business Components

• Enterprise Java Beans• JDO

• Data Layer

ServiceContentBroker

Data Layer

XML Content

XML ServiceWeb Services

orXML over HTTP

ServiceBroker

Business Component Layer

Page 4: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Server-side XML Patterns

Retail client

Retail ClientIGR Accounting

Retail DB

Transaction Progress

Exporting OrderStatus toWeddingNetwork..

The Internet

Retail Partners

Securely synchronize Orders, ProductCatalogs, and Registry with IGR WeddingNetwork

Open, standardized interface for integrationinto existing business processes.

Gateway provides components to handleproprietary and ShopML (IGR standardXML)

WeddingNetwork IREG Enterprise server-side Java component

technology. Business logic written to standard

ShopML Servlets manage HTML interface.

www.weddingnetwork.com

Wedding NetworkWebsite!

Retail DB

The Internet

Web ApplicationServer

Products

OrdersRegistry

I nternet Retailer Enterprise Gateway

ShopMLservicehandler

Business Logic (EJB)

Order Retailer

Product Registry

Java App Server

Retail Client

ShopMLRequest

ShopMLResponse

ShopMLFilter

RMI

Recieve OrderReceive Registry

OrderRegistry

Gateway ServiceInterface

Order Status

Receive Orders

Product Catalog

Sync Registry

Corba / IIOP

Servlet

Servlet

BusinessLogic

ClientData

Copyright © 2000 All rights reserved. Deb Ayers and Tom Marrs

•XML Bridge•Service Broker

•Content Broker•XML Parser Factory

Page 5: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Content Broker: Architecture Pattern

Content Broker

Page 6: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Content Broker (cont’d)

+addToContent(in tagName : String, in fieldValues : BaseDictionary)+getProperties() : Enumeration

«interface»IShopMLContentHandler

+getContent() : Element

«interface»IContent

OrderShopMLContentHandler QueryShopMLContentHandler

QueryOrderShopMLContentHandler

«uses» «uses»

+makeShopMLContentHandler(in serviceType : String) : IShopMLContentHandler-contentHandlersTable : Hashtable

ShopMLContentHandlerFactory

1

+creates*«utility»

Hashtable

1

1

Page 7: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Fast Forward....XML Processing 2003

Page 8: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

What is XML Beans

• 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 APIs1. XmlObject2. XmlCursor3. Schema Type System

A simple, comprehensive, fast tool for accessing XML from Java.

Page 9: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

Page 10: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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)

Page 11: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

XMLBeansBoth

Typed Java PLUS100% XML and Schema

Easy and Robust

Page 12: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

Page 13: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

100% Schema support

• Namespaces• All types • Inheritance (by restriction, by list, by union, by

extension)• All content models• Includes/imports• Key/KeyRef

Page 14: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

XMLBeans Data Model

Complete XML Token Stream

Convenient Typed JavaBean Tree

High speed parser High speed saver

Lightweight cursor access Streamed XQuery

Full XML infoset

Strongly typed methods Class casts, Inheritance

Natural Java types Validation

Syn

chro

nize

dCursor Access

Object Access

Typed Java data

Page 15: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

Using XML Beans

Page 16: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

XmlTypes.jar

Compiling Schema

XML Schema(s)Imported/IncludedXML Schema(s)

XML BeansSchema

Compiler

Java Interfaces

Java Classes

XML SchemaBinary Files (.xsb)

(Put on classpath to access

generated classes)

(Schemas are loaded as XML Beans using

schema for schema)

Page 17: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

Simple Demo

Page 18: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

Page 19: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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 classLineItem classShipper class

Page 20: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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()

Page 21: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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();

Page 22: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Parsing an XML DocumentBehind the scenes

XmlTokenSource

Picollo

<purchase-order xmlns="http://openuri.org/easypo">

<customer>

<name>Gladys Kravitz</name>

<address>Anytown, PA</address>

</customer>

<date>2001-12-17T09:30:47-05:00</date>

<shipper>

<name>UPS</name>

<per-ounce-rate>0.74</per-ounce-rate>

</shipper>

</purchase-order>

XML

<purchase-order xmlns=“…”>\n

Char B

uffer

XmlStore

XmlLoader

PurchaseOrderDocument.Factory.parse()

<customer>\n

<name>

splay tree<address>

<date>\n

XmlObject

<shipper>\n<name>

<per-ounce-rate>

XmlObject PurchaseOrderDocument

1. Consolidate QNames

2. Build XmlStore

3. Create Root XmlObject

Page 23: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

Page 24: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Reading an XML Bean

Use Factory.parse() to get Document and then read using typical java techniques:

getter for complex types

(objects)

getter for simple types

getter for repeating element

(array)

File f = new File("c:\text.xml");

PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.parse(f);

PurchaseOrderDocument.PurchaseOrder po = doc.getPurchaseOrder();

Customer c = po.getCustomer();

String address = c.getAddress();

String name = c.getName();

LineItem[] liArray = po.getLineItemArray();

for (int i = 0; i < liArray.length; i++) {

....

}

Page 25: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Reading an XML BeanBehind the scenes

Customer c = myPurchaseOrder.getCustomer();Lazy instantiation:

Complex Type: (Customer)

1. If Object already in store return it2. Otherwise create object and

retrieve info from XML Store

Simple Type: (Name)

1. get from the parent object (Customer) directly

Object Store XML Store

String name = c.getName();

Customer-name

-address

…<customer> <name> Fred </name> <address> 123 main </address></customer> …

Customer-name

-address

Page 26: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Updating/Setting Xml Beans

setter for simple types

setter for complex type

PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.newInstance();

PurchaseOrderDocument.PurchaseOrder po = doc.addNewPurchaseOrder();

Customer c = po.addNewCustomer();

c.setName(“Dave Remy”);

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);

billingAddress.setType = “billing”;

Page 27: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Updating XML BeansBehind the scenes

setCustomer(myCustomer);

c.setName(“Dave”);

Object Store

Customer-name

-address

XML Store

…<customer> <name> Fred </name> <address> 123 main </address></customer> …

*invalid*

xmlCursor.selectPath(“.//customer”);

refresh

Updates to XML Store work similarly - “invalid bits” used

to synchronize

Page 28: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

Page 29: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Reality Check

Page 30: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

Using XML Cursor

Page 31: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

Page 32: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Reading an Xml Documentwith XML Cursor

public String discoverTokenTypes(XmlObject xmlDoc) throws Exception

{

XmlCursor documentCursor = xmlDoc.newCursor();

while (documentCursor.hasNextToken()) {

...

documentCursor.toNextToken();

}

documentCursor.dispose();

}

use .newCursor to create cursor

token model

A new Cursor can be created on ANY XmlObject (using xmlObject.newCursor()) – mix Xml Cursor with strongly typed Xml

Objects

Page 33: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Types of Tokens

• Document Start• Document END• Element Start• Element End• Text• Attribute• Comment• Namespace• Processing Instructions

Page 34: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Navigating in Xml Cursor

• hasNextToken(); toNextToken()• toChild(QName)• toFirstChild()• toFirstAttribute()• toNextChild()• toNextSibling()• Etc., etc., etc.

Page 35: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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 …

Page 36: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

Page 37: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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.

Page 38: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

. . . 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.

Page 39: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Selecting Nodes

• XPath uses a virgule delimited path for specifying nodes in a Location Path.

• Specify the Context – from root node

• /

– descendants• //

Page 40: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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>

<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>

/

Page 41: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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><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

Page 42: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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 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

Page 43: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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 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

Page 44: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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><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

Page 45: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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 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”]

Page 46: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

A Simple XQuery Program

<CUSTOMERS> {

for $doc in document ("XM-BB-C")/db/CUSTOMER

let $customer := $doc

where $doc/STATE = "TX"

return $customer

} </CUSTOMERS>

<CUSTOMERS> {

for $doc in document ("XM-BB-C")/db/CUSTOMER

let $customer := $doc

where $doc/STATE = "TX"

return $customer

} </CUSTOMERS>

Determines # of tuples

Page 47: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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.

Page 48: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Last But Not Least

Page 49: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

XQuery and XPath with XMLBeans

• 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

Page 50: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

DemoMore XML Beans,

XmlCursor, XQuery

Page 51: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Content Broker : Architecture PatternContent Broker

Case Solved

Page 52: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

References

• XML Case Study – When Click Meets Brick• http://dev2dev.bea.com

Page 53: Innovations and Standards for XML Processing in Java XML Beans and XQuery Deb Ayers Principal

Q & A