Validate XML Schema using MSV or JAXB?
843834Nov 19 2003 — edited Jun 25 2007I am trying to validate my XML file against XML Schema using sun MSV (multi schema validator)and have problems with continuing validation after first missing attribute. I want all validation errors reported irrespective of whether they are missing, unexpected, or invalid. Multi Schema validator seemed the best choice since it supports multiple schemas (RelaxNG, schematron, W3C) but this is a show stopper for me.
Does anyone know how to continue past this problem or would you suggest using JAXB instead?
I am including the code for reference:
// creating verifier object
VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
Verifier verifier = (factory.compileSchema("sample.xsd").newVerifier();
//creating DOM tree
DocumentBuilderFactory domf = DocumentBuilderFactory.newInstance();
domf.setNamespaceAware(true);
DocumentBuilder builder = domf.newDocumentBuilder();
Document dom = builder.parse(new File("sample.xml"));
// validate DOM tree by Node wise...
validateElement(verifier, dom.getDocumentElement());
// methods to validate each node in the DOM
public void validateElement(Verifier verifier, Node node){
verifyElement(verifier, node);
NodeList nL = node.getChildNodes();
int len = nL.getLength();
for(int i=0; i<len; i++){
Node n = nL.item(i);
validateElement(verifier, n);
}
}
public void verifyElement(Verifier verifier, Node node){
try{
verifier.verify(node);
}
catch(ValidityViolation vv){
System.out.println(vv.getMessage() );
}
catch(Exception e){
e.printStackTrace();
}
}
code for sample.xsd
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<xsd:element name="html">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="body"/>
</xsd:sequence>
<xsd:attribute name="lang" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="body">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="img"/>
<xsd:element ref="p" />
</xsd:sequence>
<xsd:attribute name="bgcolor" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="img">
<xsd:complexType>
<xsd:attribute name="src" type="xsd:string" use="required"/>
<xsd:attribute name="alt" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="p" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:schema>
code for sample.xml
--------------------
<html height="40">
<head>
<title>Sample XML file </title>
</head>
<body fgcolor="red" fgColor="white">
<img />
</body>
</html>
output of the program:
----------------------
unexpected attribute "height" in the tag "html"
tag name "head" is not allowed.
tag name "title" is not allowed.
unexpected attribute "fgColor" in the tag "body"
element "img" is missing "alt" attribute
Results expecting from the validator
----------------------------------------
1) should continue looking for "lang" attribute in <html> tag after encountering unknown attribute "height"
2) shoud continue looking for "src" attribute in <img> tag after reporting the missing "alt" attribute.
3) anticipating to report the missing element <p> which has minOccurs=1.
I would appreciate some quick responses as I am absolutely against a brick wall here!
Thanks in advance...
Venkat Paruchuri