[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
No Subject
- From: Richard Featherstone <R.Featherstone@uea.ac.uk>
- To: xml-dev@lists.xml.org
- Date: Tue, 10 Jul 2001 14:01:42 +0100 (BST)
Please help. I'm trying to validate against a schema using Xerces and
JAXP. I'm only using a simple schema to get things working, but they
don't. Please look at the following code and let me know what I'm doing
wrong.
Thanks in advance
Richard
HERE'S THE BASIC SCHEMA
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="message" type = "MessageType"/>
<xsd:complexType name = "MessageType">
<xsd:choice>
<xsd:element name = "request" type = "xsd:string"/>
<xsd:element name="response" type = "xsd:string"/>
</xsd:choice>
</xsd:complexType>
</xsd:schema>
AND A VALID DOC
<?xml version="1.0" encoding="UTF-8"?>
<message xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\WINDOWS\Desktop\Inspiration\schema.xsd">
<request>do something</request>
</message>
AN INVALID DOC
<?xml version="1.0" encoding="UTF-8"?>
<message xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\WINDOWS\Desktop\Inspiration\schema.xsd">
<badTag>should fail</badTag>
</message>
AND THE JAVA CODE TO PARSE AND VALIDATE
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
public class Parse extends DefaultHandler {
public void startElement(String uri, String localName, String
qualName, Attributes attrs) throws SAXException {
System.out.println("Start\t" + localName);
}
public void characters (char buffer[], int offset, int length) throws
SAXException {
if(length > 0){
String temp = new String(buffer, offset, length);
if(!temp.trim().equals("")){
System.out.println("Content\t" + temp.trim());
}
}
}
public void error (SAXParseException spe) throws SAXParseException{
throw spe;
}
public void warning (SAXParseException spe) throws SAXParseException{
System.err.println("Warning: " + spe.getMessage());
}
public static void main (String args[]) {
Parse p = new Parse();
try{
XMLReader parser =
(XMLReader)Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
parser.setContentHandler(p);
parser.setErrorHandler(p);
parser.setFeature("http://xml.org/sax/features/validation",
true);
parser.setFeature(
"http://apache.org/xml/features/validation/schema", true);
//parser.setFeature(
"http://apache.org/xml/features/validation/schema-full-checking", true);
parser.parse(new
InputSource("file:///C:/WINDOWS/Desktop/Inspiration/badDoc.xml"));
}
catch (SAXParseException spe){
System.err.println("Parse Error: " + spe.getMessage());
}
catch(SAXException se){
se.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
System.exit(0);
}
}
ERROR MESSAGE
Element type "message" must be declared
- Follow-Ups:
- RE:
- From: Priscilla Walmsley <priscilla@walmsley.com>