[
Lists Home |
Date Index |
Thread Index
]
I'm trying to use org.apache.xerces.parsers.SAXParser from Java to validate
an XML document.
If the document starts:
<?xml version="1.0"?>
<PurchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="PO.xsd"
>
<Header>...</Header>
...
then the code below does the right thing (assuming that PO.xsd is present),
such that if I change <Header></Header> to <xHeader></xHeader>, the document
won't validate.
However, the incoming documents do not have the namespace declarations in
them, so I'm attempting to validate them by specifying the schema from Java.
However, this isn't working properly: the document will be parsed without
errors, even if it contains <xHeader></xHeader>.
What do I have to do to force the document to be validated against a schema
that I specify?
Thanks.
Partial code:
static final String DEFAULT_PARSER_NAME =
"org.apache.xerces.parsers.SAXParser";
static final String VALIDATION_FEATURE =
"http://xml.org/sax/features/validation";
static final String SCHEMA_VALIDATION_FEATURE =
"http://apache.org/xml/features/validation/schema";
static final String DYNAMIC_VALIDATION_FEATURE =
"http://apache.org/xml/features/validation/dynamic";
static final String SCHEMA_NO_NAMESPACE_LOCATION_PROPERTY =
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"
;
static final String SCHEMA_LOCATION_PROPERTY =
"http://apache.org/xml/properties/schema/external-schemaLocation";
static final String SCHEMA_FULL_CHECKING_FEATURE =
"http://apache.org/xml/features/validation/schema-full-checking";
public static void main(String[] args)
{
if(args.length != 1)
{
System.err.println("Usage: java ParseXML <filename>");
System.exit(1);
}
XMLReader parser = null;
try
{
parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
}
catch (Exception e)
{
System.err.println("error: Unable to instantiate parser (" +
DEFAULT_PARSER_NAME + ")");
System.exit(1);
}
try
{
// try various features/properties
parser.setFeature(VALIDATION_FEATURE, true);
parser.setFeature(SCHEMA_VALIDATION_FEATURE, true);
//parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE, true);
parser.setFeature(DYNAMIC_VALIDATION_FEATURE, true);
//parser.setProperty(SCHEMA_NO_NAMESPACE_LOCATION_PROPERTY,
"file:///c:/home/code/eclipsetest/test/telstraPO.xsd");
parser.setProperty(SCHEMA_NO_NAMESPACE_LOCATION_PROPERTY,
"PO.xsd");
//parser.setProperty(SCHEMA_LOCATION_PROPERTY,
"file:///c:/home/code/eclipsetest/test/telstraPO.xsd
file:///c:/home/code/eclipsetest/test/PO.xsd");
//parser.setProperty(SCHEMA_LOCATION_PROPERTY,
"http://www.w3.org/2001/XMLSchema-instance
file:///c:/home/code/eclipsetest/test/PO.xsd");
parser.setErrorHandler(new ErrorHandler()
{
public void fatalError(SAXParseException spe)
throws SAXException
{
printError("Fatal Error", spe);
}
public void error(SAXParseException spe)
throws SAXParseException
{
printError("Error", spe);
System.out.println("error");
}
public void warning(SAXParseException spe)
throws SAXParseException
{
printError("Warning", spe);
}
});
if(false) // not using this at the moment
{
parser.setEntityResolver(new EntityResolver()
{
public InputSource resolveEntity (String publicId,
String systemId)
{
System.out.println("publicId[" + publicId + "]");
System.out.println("systemId[" + systemId + "]");
return new InputSource();
}
});
}
}
catch(SAXNotRecognizedException e)
{
System.err.println("not recognised: " + e);
System.exit(1);
}
catch(SAXNotSupportedException snse)
{
System.err.println("not supported: " + snse);
System.exit(1);
}
try
{
System.out.println("begin parsing");
parser.parse(args[0]);
System.out.println("end parsing");
}
catch(Exception e)
{
System.err.println("Parsing error: " + e);
e.printStackTrace();
}
PJDM
--
Peter Mayne
Technology Consultant
Spherion Technology Solutions
Level 1, 243 Northbourne Avenue, Lyneham, ACT, 2602
T: 61 2 62689727 F: 61 2 62689777
The information contained in this email and any attachments to it:
(a) may be confidential and if you are not the intended recipient, any interference with,
use, disclosure or copying of this material is unauthorised and prohibited; and
(b) may contain personal information of the recipient and/or the sender as defined
under the Privacy Act 1988 (Cth). Consent is hereby given by the recipient(s) to
collect, hold and use such information and any personal information contained in a
response to this email, for any reasonable purpose in the ordinary course of
Spherion's
business, including forwarding this email internally or disclosing it to a third party. All
personal information collected by Spherion will be handled in accordance with
Spherion's Privacy Policy. If you have received this email in error, please notify the
sender and delete it.
(c) you agree not to employ or arrange employment for any candidate(s) supplied in
this email and any attachments without first entering into a contractual agreement with
Spherion. You further agree not to divulge any information contained in this document
to any person(s) or entities without the express permission of Spherion.
|