Google Answers Logo
View Question
 
Q: Parsing XML with Schemas and Java ( No Answer,   0 Comments )
Question  
Subject: Parsing XML with Schemas and Java
Category: Computers > Programming
Asked by: memphisblues-ga
List Price: $10.00
Posted: 12 Dec 2003 07:52 PST
Expires: 12 Dec 2003 12:34 PST
Question ID: 286331
Google Answers -

I'm working on a software application that requires XML validation
with a W3C schema.  The question: what is the easiest way to parse an
XML string (in Java) and validate it against a schema?  Ideally, I'd
like to see something like this (just pseudo-code):

  String xmlData...
  String xmlSchema...
  
  Parser parser = new Parser(xmlData);
  SchemaErrors errors  = parser.validate(xmlSchema);
  // now do something with all of the errors  

The crucial components are: a) commercial-grade libraries; b) in
memory Java parsing (i.e. not command line); and, c) handling multiple
errors within a document (i.e. does not exit at first error). 
Currently, we're using dom4j as the parsing engine, but other
commercial-grade (e.g. Xerces) parsers would be acceptable.

Josh

Request for Question Clarification by endo-ga on 12 Dec 2003 11:21 PST
Hi,

I've done it in the past in the following way:

document = saxBuilder.build(filepath);  //builds doc a first time
document.setDocType(DocType);  //sets the doc type appropriately


byte[] bytes = XMLoutputter.outputString(document).getBytes();  //take
document and fit it into byte array

ByteArrayInputStream bais = new ByteArrayInputStream(bytes);


saxBuilder.setValidation(true);   //sets DTD validation to true
document = saxBuilder.build(bais);  //rebuilds document from byte
stream this time with validation
element = document.getRootElement();


When you use the saxBuilder.build command it will automatically verify
your document against the DTD.

I'm sure though that it won't stop at the first error.


Here is an article though on doing what you want:

Validation with Java and XML Schema
http://www.javaworld.com/javaworld/jw-12-2000/jw-1208-validation4.html

Please let me know what you think.

Thanks.
endo

Clarification of Question by memphisblues-ga on 12 Dec 2003 12:02 PST
endo-ga -

I've read that article from JavaWorld, but it doesn't address my
question.  The schema already has all of the field rules embedded in
it (e.g. phone number is an alphanumeric field that is required) - I
definitely don't want to explicitly rewrite all of the validation
again.  I'd like to find a pre-built validator that will return a list
of encountered errors when validating an XML - something like the
org.xml.sax.ErrorHandler.  What are the options, and what are the
pros/cons associated with them.

Thanks,
Josh

Request for Question Clarification by endo-ga on 12 Dec 2003 12:24 PST
Hi,

Here are a couple of tutorials on using ErrorHandler:

Validation and the SAX ErrorHandler interface
http://www-106.ibm.com/developerworks/xml/library/x-tipeh.html

Using the Validating Parser 
http://java.sun.com/webservices/docs/ea1/tutorial/doc/JAXPSAX11.html#65302

I would say the advantage is that it's easy to implement it that way,
the disadvantage is that it might not be perfectly customised to your
requirements.

If that doesn't suit you, maybe you could consider extending the
ErrorHandler interface.

Please let me know what you think.

Thanks.
endo
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy