|
[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] No Subject
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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|
|||||||||

Cart








