| Author |
Problem while validating XML document with schema using SAX Parser.
|
Prince Manchanda
Ranch Hand
Joined: Jun 25, 2001
Posts: 45
|
|
Hi, I am facing problem while using schema to validate my xml document. I am using SAX parser to parse the xml. The application is running on Tomcat 4.1 with jdk1.3. I get the xml as an input stream. The code works fine when I run it as a stand alone appliation with the xml file and xsd file in same directory. Here is the trace of the errors that I am getting: ERROR: org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than xs:appinfo and xs:documentation . Saw 'targetNamespace= "http://localhost:8080/schema"'. ERROR: org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than xs:appinfo and xs:documentation . Saw 'xmlns="http://localhost:8080/schema"'. ERROR: org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than xs:appinfo and xs:documentation . Saw 'elementFormDefault="qualified">'. ERROR: org.xml.sax.SAXParseException: TargetNamespace.1: Expecting namespace 'http://localhost:8080/schema', but the target namespace of the schema document is 'null'. ERROR: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'catalog'. My input xml is <catalog xmlns="http://localhost:8080/schema/"); xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"); xsi:schemaLocation="http://localhost:8080/schema/ catalog.xsd" title="OnJava.com" publisher="O'Reilly"> <journal date="April 2004"> <article> <title>Declarative Programming in Java</title> <author>Narayanan Jayaratchagan</author>" </article> </journal> </catalog> My xsd is <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> targetNamespace="http://localhost:8080/schema/" xmlns="http://localhost:8080/schema" elementFormDefault="qualified"> <xs:element name="catalog"> <xs:complexType> <xs:sequence> <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="title" type="xs:string"/> <xs:attribute name="publisher" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="journal"> <xs:complexType> <xs:sequence> <xs:element ref="article" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="date" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="article"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element ref="author" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="author" type="xs:string"/> </xs:schema> The snippet of code that I am using is: This is my EntityResolver Class public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException { InputStream is = null; try { String uri=;//this contains the path of my xsd is = new FileInputStream(new File(uri)); } catch(Exception e) { System.out.println("ENTITYRESOLVER: "+e); } return new InputSource(is); } My Parsing class is public void parse(InputStream is) throws Exception { try { String PARSER_NAME="org.apache.xerces.parsers.SAXParser" InputSource ins = new InputSource(is); XMLReader parser = (XMLReader) Class.forName(PARSER_NAME).newInstance(); parser.setContentHandler(this); parser.setErrorHandler(this); parser.setEntityResolver(new MyEntityResolver()); parser.setFeature("http://xml.org/sax/features/validation",true); parser.setFeature("http://apache.org/xml/features/validation/schema",true); } catch(Exception e) { System.out.println("EXCEPTION: load(): "+e); throw new Exception(); } }//end of parse Any idea what mistake I am doing?
|
Thanks and Regards
P Manchanda
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Your schema does have the errors that the parser says it has. (No surprise there, really.)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> targetNamespace="http://localhost:8080/schema/" xmlns="http://localhost:8080/schema" elementFormDefault="qualified">
The first line of that is the start tag of an xs:schema element. (Yes, look carefully, it's the whole start tag.) The second line is text, but it should be inside the start tag. Similarly for the third and fourth lines, and the fourth line ends in a ">" which is probably unescaped, making your document not well-formed.
|
 |
Prince Manchanda
Ranch Hand
Joined: Jun 25, 2001
Posts: 45
|
|
Thanks Paul. That was indeed the problem . The line <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> had an extra ">" character. Removing it fixed the problem.
|
 |
 |
|
|
subject: Problem while validating XML document with schema using SAX Parser.
|
|
|