• 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 validation with XSD in java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
i am trying to validate XML with a predefined XSD so that if validation is OK i can go ahead with parsing XML,for parsing i am using JAXB 2.x but i am doing validation outside of JAXB.
here is a part of my XML and XSD

and XML is

the code for validation is something like

it is validating the XML against XSD and showing the error also

but The validator reports a single error per wrong element.
It does not report multiple errors because there is only one error report per complex type.
i want that validator should validate complete XML document and come up with all error and warning for any missing tags or invalidTh data type
is there any way to achieve this
Thanks in advance

--
With Regards
Umesh Awasthi
Software Engineers never die… They just go Offline
http://www.travellingrants.com/
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You ARE getting multiple errors. First, there is a missing element. The stateID element. Second, the data type of the content of typeCode element is wrong.
 
aum sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Yes it showing error for missing tag as well invalid data type.but problem is if more than one required tag is missing it will throw error only for the first required tag as found from top-bottom process,no other error for other missing tags
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been a long time since I did any coding, but I seem to recall faintly that there is a setting somewhere to halt on first error or continue processing a document...for something.

Also, in your code you are setting the parser's error handler. So, you may find a way to "code" the behaviour that you want in your ErrorHandler implementation class.

Aside, the document and the XSD are indeed both being "parsed" before it hits your code. For some errors the parser can continue and for some it halts immediately. Ideally, you should not attempt to process XML documents that contain errors and should abort once an error is identified.

Whatever is creating the XML instance documents should be fixed so that it does not produce invalid XML documents. This is where you should focus instead of creating a questionable error handling mechanism in your code.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jimmy Clark wrote:Whatever is creating the XML instance documents should be fixed so that it does not produce invalid XML documents. This is where you should focus instead of creating a questionable error handling mechanism in your code.



+1 for this. The designers of XML didn't mean for it to be used like a compiler for debugging somebody's faulty document. For that we have XML editors like XML Spy and others.
 
aum sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with what you all have suggested and i was looking any possibiliy for the entire XML validtion.
Regarding error handling way,it is a standard way to handle validation error and shoing it in a standard way.i am using this techinique to validate XML file outside Jaxb 2.x since the way JAXB does validation is not a good way.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>validator.setErrorHandler(new MyErrorHandler());
This line suggests you've your error handler implemented. In that case, it would be fairly easy to allow the validation to process and display all the validation errors (which is by itself not fatal whereas ill-formedness is). The question is what is actually coded in MyErrorHander?

If you actually had not put anything there, I can refer you to this article:
http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html
In particular, an implementation in List 3 there would result in showing you all the vaidation errors. You're not obliged to follow letter by letter of that implementation; it shows you the main layout and the functionality sought.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aum sharma wrote:Agree with what you all have suggested and i was looking any possibiliy for the entire XML validtion.
Regarding error handling way,it is a standard way to handle validation error and shoing it in a standard way.i am using this techinique to validate XML file outside Jaxb 2.x since the way JAXB does validation is not a good way.




Hi Anush did you find a solution for your problem. I am also facing same problem... that jaxb validator stops on the first missing element... I need to give errors that those & those elements are missing.
It works if any restriction (max length, max number of elements ) etc occurs.. but it stops if a mandatory element missed.
Did you able to find the solution??
Did you use some other way other than JAXB? please let me know the solution if you achieived.

Thanks.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kashbee & Anush,

I am also having the same issue. JAXB does not report multiple missing elements in <xs:sequence>. Please let me know if you found any solution for this issue.

Thank you,
Sri
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic