How to: JAXP, Xerces 2.6.2 and XML Schema ?

joes (archive)

Usenet Archive
Joined
Sep 13, 2003
Messages
18
Reaction score
0
Hello there

I tried for several days to get a simple validation with xml schema & xerces working. Goal for me is tuse JAXP and not specific Xerces classes. I don't get the point what I am doing wrong. Could somebody help me? I didn't find any full example working on the net. Thank you for any hints!

If I run the examples below, the parsers parses the file well, no vlaidation is occuring although the schema and xml file does not match.

I tried with the JAXP standard procedure, it does not work, why ?

File f = new File("dvd.xml");
SAXParserFactory saxFactory = SAXParserFactory.newInstance; saxFactory.setNamespaceAware(true); saxFactory.setValidating(true); SAXParser saxParser = saxFactory.newSAXParser; saxParser.parse(f, new DefaultHandler);

I tried with activating additional SAX Features, does also not work, why ?

File f = new File("dvd.xml");
SAXParserFactory saxFactory = SAXParserFactory.newInstance; saxFactory.setNamespaceAware(true); saxFactory.setValidating(true); saxFactory.setFeature("http://xml.org/sax/features/validation",true); saxFactory.setFeature("http://apache.org/xml/features/validation/schema",true); saxFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true); SAXParser saxParser = saxFactory.newSAXParser; saxParser.parse(f, new DefaultHandler);

Here is my xml:

<?xml version="1.0" encoding="utf-8" ?>
<dvd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dvd.xsd" id="33" available="3">Matrix Revolutions</dvd>

Here is my xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="dvdi" type="xs:string"/> <xs:attribute name="id" type="xs:string"/> <xs:attribute name="available" type="xs:boolean"/> </xs:schema>
 

Nigel Whitaker

Usenet Archive
Joined
Sep 18, 2000
Messages
5
Reaction score
0
Hello there

I tried for several days to get a simple validation with xml schema & xerces working. Goal for me is tuse JAXP and not specific Xerces classes. I don't get the point what I am doing wrong. Could somebody help me? I didn't find any full example working on the net. Thank you for any hints!

Before you call SAXParserFactory.newInstance you need to configure the implementation lookup mechanism to use Xerces (the current JDK
1.4.x default is to use the Crimson parser).

One method (see the javadoc for the SAXParserFactory.newInstance method) would be to set a system property, from the command line using
-D, or in the code:

System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");

You need to do this in addition to having the xerces jar files in your classpath.

Nigel
 

joes (archive)

Usenet Archive
Joined
Sep 13, 2003
Messages
18
Reaction score
0
Thank you Nigel

But this is not the case. I do have already the xerces activated for my application. By the way you have not to configure this by using system properties. You can use the Jar service Provider Service mechanism. Means you have just to include xerces in the class path that's all.

I still not have a solution for my issue above, so has someone a solution how to activate xml schema validation in xerces by using only JAXP and not xerces specific classes ?

thank you
regards
mark
 

joes (archive)

Usenet Archive
Joined
Sep 13, 2003
Messages
18
Reaction score
0
Ok I found the solution by myself...
Please refer to:
http://xml.apache.org/~edwingo/jaxp-ri-1.2.0-fcs/docs/samples.html#Schema

After that I simple tried the following JAXP, SAX & DOM solutions which work. While this is not documented at the Xerces Homepage in detail, I don't expect that this is working in any further releases, so be careful.

For SAX, you have to use especially the underlying xmlReader with the saxparser interface from sun it is not working.

File f = new File("dvd.xml");
SAXParserFactory saxFactory = SAXParserFactory.newInstance; saxFactory.setNamespaceAware(true); saxFactory.setValidating(true); saxFactory.setFeature("http://apache.org/xml/features/validation/schema", true ); SAXParser saxParser = saxFactory.newSAXParser; XMLReader xmlReader = saxParser.getXMLReader; xmlReader.parse(f.getAbsolutePath);

A DOM solution would look and work like this:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance; dbf.setAttribute("http://apache.org/xml/features/validation/schema", Boolean.TRUE); dbf.setNamespaceAware(true); dbf.setValidating(true); DocumentBuilder db = dbf.newDocumentBuilder; db.parse(f);

don't give up...we are just at the beginning
regards
Mark Egloff
 

Keith M. Corbett

Usenet Archive
Joined
Mar 13, 2001
Messages
50
Reaction score
0
joes said:
A DOM solution would look and work like this:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance; dbf.setAttribute("http://apache.org/xml/features/validation/schema", Boolean.TRUE); dbf.setNamespaceAware(true); dbf.setValidating(true); DocumentBuilder db = dbf.newDocumentBuilder; db.parse(f);

Sun's JAXP tutorial includes a more complete example along these lines. The section "Reading XML Data into a DOM" illustrates necessary imports and error handling. See URL:

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPDOM3.html

Section "Validating with XML Schema" is at URL:

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPDOM8.html#wp76446

/kmc
 

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
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top