Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] [Nux] [StaX] Validating an XML document with Xom

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Wolfgang Hoschek <wolfgang.hoschek AT mac.com>
  • To: Verachten Bruno <Bruno.Verachten AT atosorigin.com>
  • Cc: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] [Nux] [StaX] Validating an XML document with Xom
  • Date: Wed, 15 Mar 2006 19:23:02 -0800

On Mar 15, 2006, at 2:00 AM, Verachten Bruno wrote:

Hi,

I'm fairly new to NUX & XOM, I only downloaded the jars yesterday.
I'm trying to validate a BIG xml file thanks to XOM.
I found a snippet in the Javadoc, so I tried this :

Map schemaLocations = new HashMap();
schemaLocations.put(new File("/tmp/p2pio.xsd"),
"http://dsd.lbl.gov/p2pio-1.0";);
Builder builder = new
BuilderFactory().createW3CBuilder(schemaLocations);

That sounds good to me, except that I need a StaxBuilder.

StaxBuilder staxBuilder = new StaxBuilder();
Document document = staxBuilder.build(reader);

How can I link the two builders?

StAX isn't required for that. Both SAX and STAX are fully streaming APIs, hence can operate on arbitrary large input documents, and so can a Builder based on them, provided it has an appropriate NodeFactory.

If you only want to validate against a W3C XML Schema, configure the builder to have a NodeFactory that simply throws all events away, e.g. a XOMUtil.getNullNodeFactory(). If you'd like to build all or parts of the document, configure it with whatever is the corresponding NodeFactory. All this can be done by overriding BuilderFactory.newBuilder(). Example:

protected Builder newBuilder(XMLReader parser, boolean validate) {
return new Builder(parser, validate,
XOMUtil.getNullNodeFactory());
}

If a STAX impl becomes available that can validate against W3C schema, such STAX impl could be plugged in instead by feeding its XMLInputFactory into the following nux-1.5 STAX method:

/**
* Constructs and returns a Builder implementation that uses a STAX parser instead
* of a SAX parser. Can be used for polymorphic pluggability of SAX vs. STAX.
*
* @param inputFactory
* a factory constructing STAX {@link XMLStreamReader} instances.
* May be <code>null</code> in which case a default factory is
* used, producing a parser that will be namespace aware,
* non-validating, support DTDs, and in text coalescing mode.
* In this case the preferred implementation is Woodstox, if available.
* @param factory
* the node factory to stream into. May be <code>null</ code> in
* which case the default XOM NodeFactory is used, building the
* full XML tree.
* @return a Builder implementation using STAX instead of SAX
*/
public static Builder createBuilder(XMLInputFactory inputFactory, NodeFactory factory)



I think Tatu is getting close to releasing a woodstox STAX impl using generic Sun-MSV validation, so with some luck that might cover not only RelaxNG but also W3C XML Schema. Tatu?

This week I'm swamped and at a family funeral. I hope I might get around to upload 1.5 some time around end of next week.

Thanks to Tatu's incredible and tireless efforts around the STAX spec and woodstox impl, it will soon be a conformant, reliable, complete and efficient alternative to SAX.

Wolfgang.





Archive powered by MHonArc 2.6.24.

Top of Page