Skip to main content

JAVA XML VALIDATION with 1.5

9 replies [Last post]
jamesjmp
Offline
Joined: 2007-05-30
Points: 0

hello,
I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in Linux there is an error. The code that fails is the following:

SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCH EMA_NS_URI);

I´m sure that code is ok. In fact, I´ve found that in several tutorials and guides such as:
http://www.ibm.com/developerworks/library/x-javaxmlvalidapi.html?ca=dgr-...
http://www-128.ibm.com/developerworks/java/library/x-javaxmlvalidapi.htm...
http://www.java-tips.org/java-se-tips/javax.xml.validation/how-to-create...

In Linux it doesn´t work and it has this exception:

[java] java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
[java] at javax.xml.validation.SchemaFactory.newInstance(Sch emaFactory.java:186)
....

Both in Windows and Linux I´m using this java version:
java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode)

My GNU/Linux platform is the following:
Red Hat Enterprise Linux ES release 4 (Nahant)

W3C_XML_SCHEMA_NS_URI constant is defined in http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/XMLConstants.html#W3C_...

On the other hand I´ve found that there is a bug reported and supposed to be fixed related with this:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026581

If it is fixed I guess that my java version is former to the fix. The java version I´m using is the last downloadable and I don´t want to use JDK 6.

I´ve downloaded a recent JAXP release from https://jaxp.dev.java.net/servlets/ProjectDocumentList?folderID=4585&exp...
Exactly I´ve downloaded JAXP_141.jar. I´ve unpacked it and there are 2 jars:
jaxp-ri.jar and jaxp-api.jar. I have tried to use them as endorsed but again the same exception. Maybe I haven´t endorse then properly. I´ve moved that 2 jars to an endorsed dir I´ve created in the /jre/lib. the dir is called "endorsed" so I think there is no need using a property or invoking with -Djava.endorsed.dirs=endorsed
and the result is the same.

Any idea? I just want to validate a xml file with a xsd with java 1.5 and run it in Linux!

thanks in advance

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sanketdaru
Offline
Joined: 2007-06-07
Points: 0

Hi,
I am using JDK 1.4.2_05 and I am having the exact same problem. Can anyone please help me out???

Thanks.
Regards,
Sank.

jamesjmp
Offline
Joined: 2007-05-30
Points: 0

If you are working with release 1.4 maybe this could be happening
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026581

Anyway, have you tried my solution? I mean invoking it with fork=true.

joehw
Offline
Joined: 2004-12-15
Points: 0

Interesting problem. The JAXP_141.jar you installed comes with some samples, one of which is SchemaValidator.java under [your jaxp 1.4.1 installed directory]/samples/validation. Using java version 1.5.0_11-b03, I had no problem running this sample without endorsing the installed jaxp jars. I recommend try the sample and see if it'd be helpful to your problem. To run the sample, do "ant compile" under the samples directory, and then under samples/build directory, run "java validation.SchemaValidator ../data/personal.xsd ../data/personal-schema.xml". Hope this helps. good luck.

jamesjmp
Offline
Joined: 2007-05-30
Points: 0

Finally the problem was not having set fork. I was running the application from a shell, I´ve added fork="true" and after that it works and there is no need of endorsments or anything else.
thank you anyway!

joehw
Offline
Joined: 2004-12-15
Points: 0

Glad to know you've got it working. Interesting case though. So the shell you were running the application from did not find the correct implementation class.

jamesjmp
Offline
Joined: 2007-05-30
Points: 0

yes, the classloader in Linux didn't work properly without having set fork=true.
In case this helps anyone here are muy outputs
the output, by means of jaxp.debug=true was this:

JAXP: attempting to use the platform default XML Schema validator
JAXP: createInstance(com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory)
java.lang.ClassNotFoundException: com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory
at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1166)
at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1107)
at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:983)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at javax.xml.validation.SchemaFactoryFinder.createClass(SchemaFactoryFinder.java:278)
at javax.xml.validation.SchemaFactoryFinder.createInstance(SchemaFactoryFinder.java:305)
at javax.xml.validation.SchemaFactoryFinder._newFactory(SchemaFactoryFinder.java:260)
at javax.xml.validation.SchemaFactoryFinder.newFactory(SchemaFactoryFinder.java:145)
at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:205)
at es.rbcdexia.risk.dbimport.xml.SchemaFactoryTest.checkSchema01(SchemaFactoryTest.java:16)
at es.rbcdexia.risk.dbimport.xml.SchemaFactoryTest.main(SchemaFactoryTest.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:202)
at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:134)
at org.apache.tools.ant.taskdefs.Java.run(Java.java:710)
at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:178)
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:84)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:37)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:382)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.Main.start(Main.java:150)
at org.apache.tools.ant.Main.main(Main.java:240)
JAXP: failed to getClass(com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory)
JAXP: unable to find a factory for http://www.w3.org/2001/XMLSchema

On the other hand, with fork=true the output is this:

JAXP: attempting to use the platform default XML Schema validator
JAXP: createInstance(com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory)
JAXP: factory 'com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory' was found for http://www.w3.org/2001/XMLSchema

regards,
Jaime

lethrech
Offline
Joined: 2007-06-06
Points: 0

hi,
but where we have to put fork=true

think s

jamesjmp
Offline
Joined: 2007-05-30
Points: 0

In my case I was using ant and a build.xml. For instance:


classpath="${project.classpath}" fork="true"/>

This way the classloader works in Linux with no problem.
regards

lethrech
Offline
Joined: 2007-06-06
Points: 0

think s for your response
in my case i use jboss4.0.4 to run my validation, and i have the same probleme
some one can help me

think s a lot