Привет! Пытаюсь научиться писать схемы для проверки xml на корректность. Код парсер взят из http://www.onjava.com/pub/a/onjava/2004/09/15/schema-validation.html:
| Код | import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler;
public class MyParser { public void validateSchema(String SchemaUrl, String XmlDocumentUrl) { com.sun.org.apache.xerces.internal.parsers.SAXParser parser = new com.sun.org.apache.xerces.internal.parsers.SAXParser();
try { 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 .setProperty( "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", SchemaUrl);
Validator handler = new Validator(); parser.setErrorHandler(handler); parser.parse(XmlDocumentUrl);
if (handler.validationError == true) { System.out.println("XML Document has Error:" + handler.validationError + "" + handler.saxParseException.getMessage()); } else { System.out.println("XML Document is valid"); } } catch (java.io.IOException ioe) { System.out.println("IOException" + ioe.getMessage()); } catch (SAXException e) { System.out.println("SAXException" + e.getMessage()); } }
public static void main(String[] argv) { String SchemaUrl = new String( "/home/elena/workspace/XMLworking/src/with_dtd/redacting.xsl"); String XmlDocumentUrl = new String( "/home/elena/workspace/XMLworking/src/with_dtd/Test.xml"); MyParser validator = new MyParser(); validator.validateSchema(SchemaUrl, XmlDocumentUrl); }
private class Validator extends DefaultHandler { public boolean validationError = false; public SAXParseException saxParseException = null;
public void error(SAXParseException exception) throws SAXException { validationError = true; saxParseException = exception; }
public void fatalError(SAXParseException exception) throws SAXException { validationError = true; saxParseException = exception; }
public void warning(SAXParseException exception) throws SAXException { } } }
|
вот моя схема:
| Код | <?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> <xsd:element name="configuration"> <xsd:complexType> <xsd:element name="config"> <xsd:element name="person"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="old" type="xsd:positiveInteger"/> </xsd:sequence> </xsd:element> </xsd:element> </xsd:complexType> </xsd:element> </schema>
|
вот xml:
| Код | <?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="redacting.xsd">
<config> <person> <name>Jane</name> <old>23</old> </person> <person> <name>Ann</name> <old>25</old> </person> </config> </configuration>
|
почему-то пишет вот что: XML Document has Error:truecvc-elt.1: Cannot find the declaration of element 'configuration'. Помогите! |