views:

42

answers:

3

I'm trying to validate one xml that I create with a local schema, but some freak error is throwing. My code:

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);

SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

factory.setSchema(schemaFactory.newSchema(
   new Source[] {new StreamSource("\\.\\schema\\xsd_me_ene_diaria.xsd")}));

And my stack trace is the follow.

java.lang.UnsupportedOperationException: This parser does not support specification "null" version "null"
at javax.xml.parsers.SAXParserFactory.setSchema(Unknown Source)
at SaxValidacao.validateSchema(SaxValidacao.java:36)

The error throws just after setSchema is called.

Some clue or another tip for XML validation in Java?

+1  A: 

One thing that sometimes happens is mixing of different versions of the parser. If you use java 5 or higher, try removing references to any external xalan or xerces libraries. (All you need to process xml is included in the standard distribution of java 5)

Nikita Rybak
Exactly! I had a incorrect lib being referenced. After your I realized this!
Luís Custódio
+1  A: 

Can you turn off validation and parse the stream? If yes, it's not likely to be a JAR conflict.

I'm thinking that your issue is access to the schema.

duffymo
+1  A: 

A possible issue is that your JAXP parser is very old and doesn't support setSchema method. Look at the javadoc for SAXParsesrFactory. For setSchema (and many other methods), it says:

Throws:

java.lang.UnsupportedOperationException - For backward compatibility, when implementations for earlier versions of JAXP is used, this exception will be thrown.

Check the parser implementation that you are using and try updating to a newer version.

Samit G.