xml schema validation (JAXP 1.3 JDK 1.4.2)

T

tremalnaik

Hello,
I'm trying to validate the xml string which root follows:

<ScenarioResponse
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ScenarioResponse.xsd">
...............
</ScenarioResponse>

with the schema file ScenarioResponse.xsd:

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"elementFormDefault="qualified">
........
</xs:schema>

My code:

private void validate(String schemaPath, String xml) throws
SAXException,
ParserConfigurationException, IOException
{
// parse document
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new
StringReader(xml)));
// create schema
SchemaFactory sf =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
sf.setErrorHandler(new myErrorHandler());
InputStream iStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaPath);
StreamSource ss = new StreamSource(iStream);
Schema schema = sf.newSchema(ss);
// validate
Validator validator = schema.newValidator();
validator.setErrorHandler(new myErrorHandler());
validator.validate(source);
}

executing the last instruction I get

org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute
'xsi:noNamespaceSchemaLocation' is not allowed to appear in element
'ScenarioResponse'.

I try to use another instance of the SchemaFactory so I try the
following:

SchemaFactory sf =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

the result is:

2005-10-27 16:17:17,932 [http-0.0.0.0-8080-1] ERROR [ROM] [THREAD: 1]
java.lang.IllegalArgumentException:
http://www.w3.org/2001/XMLSchema-instance
at
javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:181)

so I suppose an instance for this schema is not implemented.

Can someone give me a direction, please?

TREMALNAIK
 
T

tremalnaik

Roedy said:

thanks Roedy, but now I have the proof that my code is correct
(probabily).

First, I upgraded to sdk 1.5.0 and the problem was still there.

But the same snipplet of code, if run from a raw console, behaves
properly. When I use it from inside a web application (deployed into
jboss) I get the exception.

So I think I have a classpath problem, with old classes hiding the new
shipped with the latest JAXP/JDK

I have to figure out this.

Thanks

TREMALNAIK
 

KaR

Joined
Oct 8, 2008
Messages
5
Reaction score
0
Hello,

I try to validate a DOM Document using:

SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile=new StreamSource("myschema.xsd");
schema schema=factory.newSchema(schemaFile);
Validator validator=schema.newValidator();
validator.validate(new DOMSource(document));


I don't validate using the parse(File) method, because I already have a DOM Tree in memory.

BUT now I receive the following error:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'catalogue'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.XMLSchemaValidator.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)

catalogue is my rootElement.

I'm using jdk 1.4.

Does somebody know how to solve this?

Thank you!
Katja
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top