• 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

validating XML file using XML schema

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

I'm having a hard time figuring out why I can't seem to be able to validate my XML file using an XML schema in java, although other tools (e.g. XMLlint) don't seem to have a problem with it. Besides, it even doesn't work with some example files I've found on the internet so I guess it really doesn't have anything to do with the XML file itself but rather with the way I'm attempting to validate it...
I've found several threads that mention similar problems on the net but no answer that could be of use to me :-|

Anyway, here goes. Here's the example XML file I'm using:

Here's the XSD:


And last but not least here's my Java code:


Upon calling this function, an exception is thrown as follows:
Exception in thread "main" org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Artist'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
at Test.testXMLValidation(Test.java:339)
at Test.main(Test.java:345)

I really don't get it. As far as I can tell (although I'm definitely not an expert with these things) the namespaces seem to be correctly set (as I said, the files seem to be valid from the point of view of other tools) and the top-level element should definitely be defined.

Does anyone happen to have an idea of what I'm doing wrong?

Thanks a lot in advance!

Eileen
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DocumentBuilderFactory.newInstance() produces a DocumentBuilder which is not namespace-aware. Call setNamespaceAware(true) before you try to use it with a document including namespaces.
 
Eileen Becker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, it really had to be that easy!! :-)

The correct code looks like this:


Thanks a lot for the help! :-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic