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
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