开发者

Error while trying to validate XML in Java

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.

So开发者_JS百科me clue or another tip for XML validation in Java?


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)


I found a solution in a CodeWall article, add one line Java code to your codebase.

System.setProperty("javax.xml.parsers.SAXParserFactory","com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");

Please refer to get more detail: https://coderwall.com/p/kqsrrw/jdom2-this-parser-does-not-support-specification-null-version-null


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜