import java.io.File;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;
import org.xml.sax.* ;
public class MySchemaProcessor {
static final String VALIDATION_FEATURE_ID =
"http://xml.org/sax/features/validation" ;
static final String SCHEMA_VALIDATION_FEATURE_ID =
"http://apache.org/xml/features/validation/schema" ;
static final String SCHEMA_FULL_CHECKING_FEATURE_ID =
"http://apache.org/xml/features/validation/schema-full-checking" ;
public static void main(String [] args) throws Exception {
DOMParser parser = new DOMParser();
// Turn Validation on
parser.setFeature(VALIDATION_FEATURE_ID, true);
parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
parser.setErrorHandler(new MyErrorHandler()) ;
parser.parse(args [0]) ;
Document document = parser.getDocument() ;
process(document.getDocumentElement()) ;
}
static void process(Node node) {
System.out.println(node.getNodeName()) ;
switch(node.getNodeType()) {
case Node.ELEMENT_NODE:
NamedNodeMap attributes = node.getAttributes() ;
for(int i = 0 ; i < attributes.getLength() ; i++)
process(attributes.item(i)) ;
NodeList children = node.getChildNodes() ;
for(int i = 0 ; i < children.getLength() ; i++)
process(children.item(i)) ;
break ;
case Node.TEXT_NODE:
case Node.ATTRIBUTE_NODE:
System.out.println(node.getNodeValue()) ;
}
}
}
class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void error(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
System.exit(1) ;
}
}