473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How to: JAXP, Xerces 2.6.2 and XML Schema ?

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>
Jul 20 '05 #1
4 5568
jo**@bluewin.ch (joes) writes:
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.SAXParserFac tory",
"org.apache.xerces.jaxp.SAXParserFactoryImpl") ;

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

Nigel
Jul 20 '05 #2
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
Nigel Whitaker <ni************@deltaxml.com> wrote in message news:<m2************@Nigel-Whitakers-Computer.local>...
jo**@bluewin.ch (joes) writes:
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.SAXParserFac tory",
"org.apache.xerces.jaxp.SAXParserFactoryImpl") ;

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

Nigel

Jul 20 '05 #3
Ok I found the solution by myself...
Please refer to:
http://xml.apache.org/~edwingo/jaxp-...es.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

jo**@bluewin.ch (joes) wrote in message news:<26**************************@posting.google. com>...
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>

Jul 20 '05 #4
"joes" <jo**@bluewin.ch> wrote in message
news:26*************************@posting.google.co m...
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/tu.../JAXPDOM3.html

Section "Validating with XML Schema" is at URL:

http://java.sun.com/j2ee/1.4/docs/tu...8.html#wp76446

/kmc
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bugbear | last post by:
Subject pretty much says it all. I'd like to parse XML (duh!) using Xerces (because its fast, and reliable, and comprehensive, and supports lots of features). I'd like to conform to standards...
0
by: Thomas Scheffler | last post by:
Hi, I use the Xerces SAXParser to read my documents in Java. For that I wrote an EntityResolver so that the parse can use a schema to validate them. Now that is the problem. The parser does...
2
by: Olaf Meyer | last post by:
Apprentently xerces 2.6.0 (Java) does not validate against contraints specified in the schema (e.g. constraints specified via unique element). The validation works with the XML editor I'm using...
0
by: benoit | last post by:
Hi, When it's written in WebLogic Server 8.1 documentation that this software is based on JDK 1.4.1, and is JAXP 1.1 compliant, (http://edocs.bea.com/wls/docs81/xml/xml_intro.html#189256), does...
1
by: Nuno | last post by:
Hello, I'm looking for a way of validating/parsing the xsd file (schema), i only been able of validating the xml file with the corresponding schema, but what i want is only validate the xsd...
8
by: erik_midtskogen | last post by:
Hi Folks, I'm writing a general-purpose HTML screen-scraping framework in Java (scrape new web sites without writing new code, yada yada...), and I want to use the JAXP DOM api along with XPath...
0
by: Undeclared | last post by:
Hello! My goal is to use JAXP for creating SAX parser with my own XMLParserConfiguration. For example, in package org.apache.xerces.parsers there is a constructor: public...
9
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13>...
2
by: Boris Kolpackov | last post by:
Hi, I am pleased to announce the availability of Apache Xerces-C++ 3.0.0. Xerces-C++ is an open-source validating XML parser written in a portable subset of C++. It provides DOM (level 1, 2, and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.