473,387 Members | 1,423 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,387 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 5567
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.