Tuesday, November 07, 2006

Java/Xerces 0, .Net/System.Xml 2

I've spent much of this afternoon trying to figure out how to use an XSD schema to get a pscvi-augmented SAX event stream. Doing this is .Net with System.Xml is _easy_. There are samples in MSDN. Writing this code would have taken me less 30 minutes.

Now I realize that I am lot more familiar with System.Xml and Xerces' schema APIs, but this should not be a hair-pulling experience! I've been working on this for hours and still have no idea how this is supposed to work. The only sample I've seen (xjparse) has to peek into the XSD documents to figure out if the target-namespace is empty or not. This if ridiculous! I'm amazed any Java people use XSD at all! If the libraries make it this hard, I'd be tempted not to touch it either.

Yet againt, .Net's XML apis are vastly better than Java. System.Xml.XmlReader is night-and-day easier to use than SAX and then there is this. Oh right... Java's DOM doesn't include the Microsoft specific helper methods 'selectSingleNode' and 'selectNodes'... so maybe I should make that Java:0, .Net 3.

I'm going to go back to pulling my hair out tying to figure out how to use a specified schema file to add PSVI annotations to SAX events (via the PSVIProvider interface).

I appear to have figured out the API incantations. I ended up downloading the latest sources and stepping through the code to figure out what was failing and searching around to figure how to make it do what I wanted. My working code looks like this:

SymbolTable symbolTable = new SymbolTable();
XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();

if (xsdPath != null)
{
XMLSchemaLoader grammarLoader = new XMLSchemaLoader(symbolTable);
XMLInputSource xsdSrc = new XMLInputSource(xsdPath, null, null);
xsdSrc.setByteStream(new FileInputStream(xsdPath));
Grammar grammar = grammarLoader.loadGrammar(xsdSrc);
grammarPool.putGrammar(grammar);
}

SAXParser parser = new SAXParser(symbolTable, grammarPool);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setContentHandler(new MyHandler((PSVIProvider)parser));
parser.parse(systemId);

1 Comments:

Anonymous Duma said...

In the Microsoft .NET framework, there is no good way to go back up the tree
to skip to a completely different branch.

dumiduperera@gmail.com

12:42 AM  

Post a Comment

<< Home