home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeJava and XML, 2nd EditionSearch this book

16.3. XML Schema Bindings

Moving more into the Java side of things, one major aspect of XML programming I expect to see is a set of datatypes defined in Java that represent XML Schema constructs. This is somewhat similar to the DOM Level 2 HTML bindings I talked about in Chapter 6, "Advanced DOM". I consider these infinitely more useful. Because an XML Schema is itself an XML document, it can be parsed and dealt with like any other XML document. However, trying to work with an XML Schema as just any old XML document turns out to be a bit of a pain. You can't, for example, query an element definition and determine if it is a complex type. Instead, you have to get the element, see if it has any children, determine if one of those children is named complexType, and so on. This gets even worse when things like sequencing are used; suddenly the complex type definition appears nested two levels deep.

What I expect to see (and in fact, already hear rumblings of) is a grammar and set of Java objects built to specifically match up with XML Schema datatypes. This would presumably be built on an existing object-based API, like DOM or JDOM. So, for the sake of example, assume that DOM Level 4 or JDOM 1.1 define such objects. You might see code like this:

// THIS CODE IS PURELY HYPOTHETICAL
XSDDocumentParser schemaParser =
    new org.apache.xerces.parsers.XSDParser( );
parser.parse(mySchema);
XSDDocument doc = parser.getXSDDocument( );

Now, instead of working with root elements and attributes in XML, you would deal with this document (where all the classes are prefixed by XSD, for XML Schema Datatypes) using schema concepts, as shown here:

// Get "root" element
XSDSchema schema = doc.getXSDSchema( );

// Get target namespace for this document
String targetNamespaceURI = schema.getTargetNamespace().getURI( );

// Get some named element definition
XSDElementDef def = schema.getElementDef("movie");
if (def.isComplexType( )) {
    List attributeDefs = def.getAttributeDefs( );
    List nestedElementDefs = def.getElementDefs( );
} else {
    XSDType elementType = def.getType( );
}

Obviously this is a bit contrived, because I'm making up syntax as I go. However, it's clear that my Java code is working on an XML Schema, and taking advantage of schema semantics. I'm not working with basic XML semantics (although if these classes extended basic DOM or JDOM classes, you could also work in that medium), but using what the XML Schema specification says about legal schemas to work a bit smarter. Hopefully the third edition of this book will have details about this API, because it would be very useful.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru