DOSUG XML Beans overview by Om Sivanesian

20
XMLBeans - Accessing XML by binding it to Java types.

description

A quick introduction to XML Beans at the Denver Open Source Users Group by Om Sivanesian

Transcript of DOSUG XML Beans overview by Om Sivanesian

Page 1: DOSUG XML Beans overview by Om Sivanesian

XMLBeans

- Accessing XML by binding it to Java types.

Page 2: DOSUG XML Beans overview by Om Sivanesian

XMLBeans

IntroductionIntroduction XMLBeans ArchitectureXMLBeans Architecture Compiling XML SchemaCompiling XML Schema Conversions & ValidationConversions & Validation XmlCursorXmlCursor Competing TechnologiesCompeting Technologies Sample CodeSample Code QuestionsQuestions

Page 3: DOSUG XML Beans overview by Om Sivanesian

Introduction to XMLBeans

Utilizes the full power of XML in Java

Maps the features of XML Schema to Java Types.

Underlying XML Store allows full access to the entire XML infoset when needed

Generated Java Classes allow Java-friendly access to XML

David Bau started it in BEA and was donated to Apache.

Page 4: DOSUG XML Beans overview by Om Sivanesian

Advantages of XMLBeans

Full XML Schema Support

Full XML Infoset Access

Open technology Enterprise Standard

XML Technology Make XML Schema

easy to use

Page 5: DOSUG XML Beans overview by Om Sivanesian

Important APIs of XMLBeans

XmlObject– Super-most type of the

generated interfaces/ classes

XmlCursor– Represents a postion in the

XML & provides low-level access to the XML Infoset

SchemaType– Provides a full Schema

Object Model

Page 6: DOSUG XML Beans overview by Om Sivanesian

XMLBeans Architecture

XML Schema

XML Schema BinariesGenerated

classes

Application XmlBeans jarSchema compilation

SchemaTypeXMLObject

XML API

XMLCursor

extends

Page 7: DOSUG XML Beans overview by Om Sivanesian

SchemaTypeXMLObject

XML API

XMLCursor

XML Doc XML StoreValidation

Parse

&Load

Reads/Updates

XMLBeans Architecture (contd.)

Page 8: DOSUG XML Beans overview by Om Sivanesian

Schema Compilation

XMLSchema Schema Validation

Schema Object Model

Generated Interfaces/Classes

Schema Binaries (.xsb)Compiled

Interfaces/Classes

XML Types jar

Built-in Schema Types

Page 9: DOSUG XML Beans overview by Om Sivanesian

XMLBeans Generated Classes

Schema’s targetNamespace becomes package name

Global elements and types become top-level classes

Anonymous types become inner classes

Local elements and attributes become properties

Simple type properties have both Java and XML set/get

Page 10: DOSUG XML Beans overview by Om Sivanesian

XML To Java Conversions

A Document Interface represents the XML Use its Factory to parse the XML From the Document Object get the root

object. Access other information using the getters. Use the validate() method of the Document

Object to check for validity.

Page 11: DOSUG XML Beans overview by Om Sivanesian

Java To XML Conversions

Create a new instance of the Document using its Factory.

From the Document create the Root Object. Assign values and other objects to it. The toString() method of the Document gives

the XML. Use the validate() method of the Document

Object to check for validity.

Page 12: DOSUG XML Beans overview by Om Sivanesian

XML Cursor

A different way to work with XML– A location in an XML Instance not a Node– Lighter weight than Node based model

Fewer Objects = Less Memory, Less Garbage Collection

Access to full XML Infoset (comments, PIs, whitespace)

Similar to moving a cursor through a text document

<Basket><Item>

<Category>First</Category></Item>

</Basket> XmlCursor’s programming model is token

based.

Page 13: DOSUG XML Beans overview by Om Sivanesian

XML Tokens

STARTDOC ENDDOC START END TEXT ATTR NAMESPACE COMMENT PROCINST

Page 14: DOSUG XML Beans overview by Om Sivanesian

Uses of XmlCursor

XmlToken’s newCursor() gives an XmlCursor

Navigation– Many methods are provided

to navigate. e.g: aCursor.toParent(); aCursor.toFirstChild(); aCursor.toNextChar(); aCursor.toNextAttribute(); aCursor.toNextSibling();

Page 15: DOSUG XML Beans overview by Om Sivanesian

Uses of XmlCursor (contd.)

To add new elements– Use toNextToken() to go past

the STARTDOC token.– Use

beginElement(elementName, namespace) to create a new element.

– Use insertChars(“value”) to create a text value.

Using XPath to obtain data.– selectPath(aXPath) method

returns selections.– Use toNextSelection() to

iterate through the selections.

Page 16: DOSUG XML Beans overview by Om Sivanesian

XMLBeans Tools

inst2xsd (Instance to Schema Tool) – Generates XML schema from XML instance files.

scomp (Schema Compiler) – Compiles a schema into XMLBeans classes and metadata.

scopy (Schema Copier) – Copies the XML schema at the specified URL to the specified file.

sdownload (Schema Downloader) – Maintains "xsdownload.xml," an index of locally downloaded XSD files. URLs that are specified are

downloaded if they aren't already cached. If no files or URLs are specified, all indexed files are relevant.

sfactor (Schema Factoring Tool) – Factors redundant definitions out of a set of schemas and uses imports instead.

svalidate (Streaming Instance Validator) – Validates a schema definition and instances within the schema.

validate (Instance Validator) – Validates an instance against a schema.

xmlbean Ant task – Compiles a set of XSD and/or WSDL files into XMLBeans types.

Page 17: DOSUG XML Beans overview by Om Sivanesian

Competing Technologies

JAXB Betwixt Castor Zeus JaxME

Page 18: DOSUG XML Beans overview by Om Sivanesian

Resources

http://dev2dev.bea.com/pub/a/2004/05/ryan_xml.html By Scott Ryan

http://xmlbeans.apache.org/ Documentation & Resources

Page 19: DOSUG XML Beans overview by Om Sivanesian

Sample Code

Page 20: DOSUG XML Beans overview by Om Sivanesian

Questions