Discussions

News: The Java XML Validation API

  1. The Java XML Validation API (5 messages)

    In this article, Elliotte Rusty Harold shows how to check documents for conformance to schemas with the Java XML validation API. The API by which programs request validation has varied with the schema language and parser - until now. The javax.xml.validation package provides a schema-language-independent interface to validation services.
    The javax.xml.validation API uses three classes to validate documents: SchemaFactory, Schema and Validator. It also makes extensive use of the javax.xml.transform.Source interface from TrAX to represent the XML documents. In brief, a SchemaFactory reads the schema document (often an XMl file) from which it creates a Schema object. The Schema object creates a Validator object. Finally, the Validator object validates an XML document represented as a Source.
    Harold goes on to explain how abstract factories enable one API to support many different schema languages and object models, and how error handlers can help determine more detailed information beyond the default SAXException response from a schema. And where the W3C XML Schema Language is heavily based on types, Harold also shows how the Java Validation API can identify and report these types.
    Types are identified by an org.w3c.dom.TypeInfo object. This simple interface...tells you the local name and namespace URI of a type. You can also tell whether and how a type is derived from another type. Beyond that, understanding the type is up to your program. The Java language doesn't tell you what it means or convert the data to a Java type such as double or java.util.Date. To get TypeInfo objects, you ask the Schema object for a ValidatorHandler rather than a Validator. ValidatorHandler implements SAX's ContentHandler interface. Then, you install this handler in a SAX parser. You also install your own ContentHandler in the ValidatorHandler (not the parser); the ValidatorHandler will forward the augmented events on to your ContentHandler. The ValidatorHandler makes available a TypeInfoProvider that your ContentHandler can call at any time to find out the type of the current element or one of its attributes. It can also tell you whether an attribute is an ID, and whether the attribute was explicitly specified in the document or defaulted in from the schema.
    javax.xml.validation offers an API that can handle the various useful XML schema languages that are out there. Are you using this package for validation?

    Threaded Messages (5)

  2. Am I using this for validation?[ Go to top ]

    no.
  3. no.
    Too bad, now we can really take advantage of the validator API and always validate things like ISBN against their schemas :) http://www.xfront.com/isbn.xsd
  4. Re: The Java XML Validation API[ Go to top ]

    Wow, that's a pretty cool little nugget that I had no idea was in 1.5. It's not one of the main new features that folks talk about! Thanks for the tip!
  5. Re: The Java XML Validation API[ Go to top ]

    I guess it's good that it's reiterated -- I was well aware of JARV interface, but I have been working with XML for a while, so it was one of the features that have been around for a bit, but were finally included in JDK too. Which is good, all around. On a related note, I also wrote about similar way of using Stax streaming parser implementation (Woodstox) for validating documents against DTDs or RelaxNG schemas (W3C Schema is still in the works). It can be found at: http://www.cowtowncoder.com/blog/archives/2006/08/entry_15.html and shows an interface quite similar to JARV, except for being related to Stax API, instead of DOM or SAX.
  6. Re: The Java XML Validation API[ Go to top ]

    Wow, that's a pretty cool little nugget that I had no idea was in 1.5. It's not one of the main new features that folks talk about! Thanks for the tip!
    +1 I'll definately look at it.