Hi,
Thanks. This works but it leads to another error.
I have
"Parsing error: cvc-elt.1: Cannot find the declaration of element 'memories'."
Actually, my
java code is
public void validateMemory()
{
try {
String filename = "my/editor/memory1.xsd";
ClassLoader cl = this.getClass().getClassLoader();
URL url = cl.getResource(filename);
if (url == null) {
System.out.println("cannot find memory1.xsd.");
throw new FileNotFoundException(filename);
}
final InputStream in = url.openStream();
DOMParser parser = new DOMParser();
parser.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
// use the default behaviour
return new InputSource(in);
}});
if (in != null){
in.close();
}
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
ErrorChecker errors = new ErrorChecker();
parser.setErrorHandler(errors);
parser.parse( new InputSource(new StringReader(codePanel.getText())));
} catch (Exception e) {
System.out.print("Problem parsing the file." + e.toString());
}
}
my xml is
<?xml version="1.0" encoding="UTF-8"?>
<memories>
<memory tapeid="23412">
<subdate>5/23/2001</subdate>
<donor>John Baker</donor>
<subject>Fishing off Pier 60</subject>
</memory>
</memories>
and schema is
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="memories">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="memory" type="memoryType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="memoryType">
<xsd:sequence>
<xsd:element name="subdate" type="xsd:string"/>
<xsd:element name="donor" type="xsd:string"/>
<xsd:element name="subject" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="tapeid" type="xsd:int"/>
</xsd:complexType>
</xsd:schema>
Is my schema not loaded properly?
I am quite new here but if I set schema in the entityresolver in DomParser, how does DomParser knows that this is the schema to validate against my document? or should I also code? :
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "memory.xsd");
thanks for your help.