aspose file tools*
The moose likes XML and Related Technologies and the fly likes XML validation with XSD in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML validation with XSD in java" Watch "XML validation with XSD in java" New topic
Author

XML validation with XSD in java

aum sharma
Greenhorn

Joined: Oct 11, 2008
Posts: 13
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/
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Oct 11, 2008
Posts: 13
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

Joined: Apr 16, 2008
Posts: 2187
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 11, 2008
Posts: 13
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.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 507
    
    3
>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.
Kashbeen khan
Greenhorn

Joined: Apr 26, 2004
Posts: 1
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.
sri ds
Greenhorn

Joined: Jan 07, 2013
Posts: 1
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: XML validation with XSD in java