Validating XML using the Xerces DOM parser

H

Helen

I have a web app that reads an XML file uploaded by a user, parses it
and adds the information inside it to a database.

To make sure that the users aren't putting invalid data into the
database, I thought the easiest way to do this would be to write an
XML schema to validate it.

I want all documents uploaded to be validated by this particular
schema. I'm only interested in processing documents that conform to
this schema. I want the parsing to fail for any other file.

I've checked my XML file and my schema by validating them using XML
Spy. XML Spy has no problems applying the schema to the xml file and
picking up any deliberate errors that I put into the file.

I'm using the Xerces DOM parser (version 2.6.2) to read the XML. When
I run the code with valid XML, it works fine, but if I include errors
in the XML file it doesn't pick them up.

DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/namespaces", false);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
"http://localhost:8080/assetDB/schema/assetList.xsd");
Parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
parser.parse(xmlSource);
parser.setErrorHandler(this);
Document assetDoc = parser.getDocument();

What I really want to do is this:
get XML file
Validate xml file
If valid
process file
else
give helpful error messages
end if

Am I even going about this the right way?

I appreciate any help (or even pointers to good java/xml resources)

Helen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top