Java Xml Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaimemartin
    New Member
    • May 2007
    • 9

    Java Xml Validation

    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:
    Code:
                 SchemaFactory factory =
                        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_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-lnxw07Java-XML-Val
    http://www-128.ibm.com/developerworks/java/library/x-javaxmlvalidapi .html?ca=drs
    http://www.java-tips.org/java-se-tips/javax.xml.valid ation/how-to-create-xml-validator-from-xml-s.html

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

    [java] java.lang.Illeg alArgumentExcep tion: http://www.w3.org/2001/XMLSchema
    [java] at javax.xml.valid ation.SchemaFac tory.newInstanc e(SchemaFactory .java:186)
    [java] at es.rbcdexia.ris k.dbimport.xml. XMLManager.vali darSAX(XMLManag er.java:289)
    [java] at es.rbcdexia.ris k.dbimport.xml. XMLManager.main (XMLManager.jav a:115)
    [java] at sun.reflect.Nat iveMethodAccess orImpl.invoke0( Native Method)
    [java] at sun.reflect.Nat iveMethodAccess orImpl.invoke(N ativeMethodAcce ssorImpl.java:3 9)
    [java] at sun.reflect.Del egatingMethodAc cessorImpl.invo ke(DelegatingMe thodAccessorImp l.java:25)
    [java] at java.lang.refle ct.Method.invok e(Method.java:5 85)
    [java] at org.apache.tool s.ant.taskdefs. ExecuteJava.run (ExecuteJava.ja va:202)
    [java] at org.apache.tool s.ant.taskdefs. ExecuteJava.exe cute(ExecuteJav a.java:134)
    [java] at org.apache.tool s.ant.taskdefs. Java.run(Java.j ava:710)
    [java] at org.apache.tool s.ant.taskdefs. Java.executeJav a(Java.java:178 )
    [java] at org.apache.tool s.ant.taskdefs. Java.execute(Ja va.java:84)
    [java] at org.apache.tool s.ant.UnknownEl ement.execute(U nknownElement.j ava:275)
    [java] at org.apache.tool s.ant.Task.perf orm(Task.java:3 64)
    [java] at org.apache.tool s.ant.Target.ex ecute(Target.ja va:341)
    [java] at org.apache.tool s.ant.Target.pe rformTasks(Targ et.java:369)
    [java] at org.apache.tool s.ant.Project.e xecuteSortedTar gets(Project.ja va:1216)
    [java] at org.apache.tool s.ant.helper.Si ngleCheckExecut or.executeTarge ts(SingleCheckE xecutor.java:37 )
    [java] at org.apache.tool s.ant.Project.e xecuteTargets(P roject.java:106 8)
    [java] at org.apache.tool s.ant.taskdefs. Ant.execute(Ant .java:382)
    [java] at org.apache.tool s.ant.UnknownEl ement.execute(U nknownElement.j ava:275)
    [java] at org.apache.tool s.ant.Task.perf orm(Task.java:3 64)
    [java] at org.apache.tool s.ant.Target.ex ecute(Target.ja va:341)
    [java] at org.apache.tool s.ant.Target.pe rformTasks(Targ et.java:369)
    [java] at org.apache.tool s.ant.Project.e xecuteSortedTar gets(Project.ja va:1216)
    [java] at org.apache.tool s.ant.Project.e xecuteTarget(Pr oject.java:1185 )
    [java] at org.apache.tool s.ant.helper.De faultExecutor.e xecuteTargets(D efaultExecutor. java:40)
    [java] at org.apache.tool s.ant.Project.e xecuteTargets(P roject.java:106 8)
    [java] at org.apache.tool s.ant.Main.runB uild(Main.java: 668)
    [java] at org.apache.tool s.ant.Main.star tAnt(Main.java: 187)
    [java] at org.apache.tool s.ant.Main.star t(Main.java:150 )
    [java] at org.apache.tool s.ant.Main.main (Main.java:240)



    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.ht ml#W3C_XML_SCHE MA_NS_URI

    In the API from SchemaFactory
    http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/SchemaFactory.h tml#newInstance (java.lang.Stri ng)
    it says:

    To find a SchemaFactory object for a given schema language, this method looks the following places in the following order where "the class loader" refers to the context class loader:

    1. If the system property "javax.xml.vali dation.SchemaFa ctory:schemaLan guage" is present (where schemaLanguage is the parameter to this method), then its value is read as a class name. The method will try to create a new instance of this class by using the class loader, and returns it if it is successfully created.
    2. $java.home/lib/jaxp.properties is read and the value associated with the key being the system property above is looked for. If present, the value is processed just like above.
    3.

    The class loader is asked for service provider provider-configuration files matching javax.xml.valid ation.SchemaFac tory in the resource directory META-INF/services. See the JAR File Specification for file format and parsing rules. Each potential service provider is required to implement the method:

    isSchemaLanguag eSupported(Stri ng schemaLanguage)


    The first service provider found in class loader order that supports the specified schema language is returned.
    4. Platform default SchemaFactory is located in a implementation specific way. There must be a platform default SchemaFactory for W3C XML Schema.

    If everything fails, IllegalArgument Exception will be thrown.


    I haven.t done anything of that in the windows platform and it works. do you know what is the best way to make it work in Linux?
    thanks in advance,
    Jaime
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Perhaps this is a problem with version inconsistencies between your Windows and Linux versions of Java. What version are you running on each system?

    Comment

    Working...