• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML schema validation program error

 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program to apply xml schema to an xml using the validaition api.

I am using jre 1.6

import are as follows:


The line/statement throwing the error is as follows:

The exception is:
java.lang.NoClassDefFoundError: javax/xml/validation/SchemaFactory

Priety.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

java.lang.NoClassDefFoundError: javax/xml/validation/SchemaFactory


Classpath problem and nedded JAR files are:
http://www.findjar.com/class/javax/xml/validation/SchemaFactory.html
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sagar thanks.

My code is now running.
New problems now.

My xsd is as follows:

My xml is as follows:


I run my code and get the following exception:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'pen'.

How to get around this.
I have mentioned the pen element in the schema.

Also.
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"

Do I need to mention: http://www.w3.org/2001/XMLSchema as the namespace.
Would something else work in there?
I read somewhere that its just a string.
The namespace thing is getting too confusing.
Do I need to associate with a namespace when my xsd is on machine.
What is the namespace saving me from.
What is the use of mentioning a namespace url if my machine is not connected to the network?

Priety.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, UseCodeTags

Priety Sharma wrote:
I run my code and get the following exception:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'pen'.



Clearly, in your schema file you haven't define the element "pen", you can define like this:



The namespace thing is getting too confusing.
Do I need to associate with a namespace when my xsd is on machine.
What is the namespace saving me from.
What is the use of mentioning a namespace url if my machine is not connected to the network?


All this doubt can be answered by simple Googleing and namespace has nothing to do with networks, its just like a "package name" assigned to differentiate the other elements with same name.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,

I am now using code tags.
Still continue to get the following error:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'pen'.

The xml and the schema now look like this:

XML:


XSD/Schema:


Why is it now working now.
Clearly I have defined the pen element now.
Also I have made the XML with a single element pen and the schema also simply specifies the pen element.
What wrong this time?
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, its my guess, but give it a try:


and please go through this tutorial for XML schema concepts.
http://www.w3schools.com/Schema/schema_complex_empty.asp

 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Tried what you said but same result.
Finally took a valid xml and xsd example from W3schools.
Passed it to a validator site.
It showed the result as xml is valid.

Provide the paths to the same xml and xsd to the code.
Same end result.

Got this exception again:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'shiporder'.

The xml is:



Thd xsd is:


Even this is not passed a valid by the code:

So pasting the code for xml validation here:



Is this a problem with the java api.
Why is not passing this xml as valid?

 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set namespace awareness to true on the DocumentBuilderFactory, like
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sagar Thanks!

That line of code made it work.

Its (the xml) now getting parsed by the xsd.

Thanks a million.
I hope am now able to proceed with learning more xsd rules.

 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Honestly speaking, I never parsed XML using the Schema, what it really takes me to conclude the answer was a little Googling and API browsing
I hope next time, before posting the question you should do the above two things I had suggested.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sagar. I will definitely search better from now onwards.
To tell you the truth I had tried to search info on xml schemas and I should have searched for the code part too. I assumed that the code is 100% correct.

Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic