dosug xml beans overview by om sivanesian
DESCRIPTION
A quick introduction to XML Beans at the Denver Open Source Users Group by Om SivanesianTRANSCRIPT
XMLBeans
- Accessing XML by binding it to Java types.
XMLBeans
IntroductionIntroduction XMLBeans ArchitectureXMLBeans Architecture Compiling XML SchemaCompiling XML Schema Conversions & ValidationConversions & Validation XmlCursorXmlCursor Competing TechnologiesCompeting Technologies Sample CodeSample Code QuestionsQuestions
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.
Advantages of XMLBeans
Full XML Schema Support
Full XML Infoset Access
Open technology Enterprise Standard
XML Technology Make XML Schema
easy to use
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
XMLBeans Architecture
XML Schema
XML Schema BinariesGenerated
classes
Application XmlBeans jarSchema compilation
SchemaTypeXMLObject
XML API
XMLCursor
extends
SchemaTypeXMLObject
XML API
XMLCursor
XML Doc XML StoreValidation
Parse
&Load
Reads/Updates
XMLBeans Architecture (contd.)
Schema Compilation
XMLSchema Schema Validation
Schema Object Model
Generated Interfaces/Classes
Schema Binaries (.xsb)Compiled
Interfaces/Classes
XML Types jar
Built-in Schema Types
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
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.
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.
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.
XML Tokens
STARTDOC ENDDOC START END TEXT ATTR NAMESPACE COMMENT PROCINST
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();
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.
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.
Competing Technologies
JAXB Betwixt Castor Zeus JaxME
Resources
http://dev2dev.bea.com/pub/a/2004/05/ryan_xml.html By Scott Ryan
http://xmlbeans.apache.org/ Documentation & Resources
Sample Code
Questions