M
Marcin Cenkier
Hi,
I want to validate a DOM document, and if I build DOM from a stream
using documentBuilder.parse() validation using
validator.validate(DOMSource) works, but if I create the same document
manually then validation throws an exception:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of
element ....
I'm using java 1.4.2 and xalan-2.7.0.jar, xercesImpl-2.7.1.jar,
xml-apis-1.3.02.jar
Is it possible that the problem occurs because javax.xml.validation.*
package is not included in standard java distribution?
Here's a sample application showing the problem:
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import com.oyster.mom.contentserver.export.Xml;
public class DOMSchemaValidation {
private final static String SCHEMA = "<xs:schema
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"><xs:element
name=\"package\" type=\"xs:anyType\" /></xs:schema>";
private final static String XML = "<package/>";
private static Validator getValidator(Schema schema) {
Validator validator = schema.newValidator();
validator.setErrorHandler(new DefaultHandler() {
public void error(SAXParseException e) throws SAXException {
System.out.println("error: " + e.getMessage());
throw e;
}
});
return validator;
}
public static void main(String[] args) {
try {
SchemaFactory schemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new StreamSource(new
StringReader(SCHEMA)));
Validator validator = getValidator(schema);
System.out.println("dom parsed document:");
System.out.println(" stream validation...");
validator.validate(new StreamSource(new StringReader(XML)));
System.out.println(" ...passed");
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(new
StringReader(XML)));
System.out.println(" dom validation...");
validator.validate(new DOMSource(document));
System.out.println(" ...passed");
{
documentBuilder = documentBuilderFactory.newDocumentBuilder();
System.out.println("dom created document:");
document = documentBuilder.newDocument();
Element packageElement = document.createElement(Xml.Package.TAG_PACKAGE);
document.appendChild(packageElement);
System.out.println(" dom validation...");
validator.validate(new DOMSource(document));
System.out.println(" ...passed");
}
}
catch (Exception e) {
System.out.println("... failed");
e.printStackTrace(System.out);
}
}
}
I want to validate a DOM document, and if I build DOM from a stream
using documentBuilder.parse() validation using
validator.validate(DOMSource) works, but if I create the same document
manually then validation throws an exception:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of
element ....
I'm using java 1.4.2 and xalan-2.7.0.jar, xercesImpl-2.7.1.jar,
xml-apis-1.3.02.jar
Is it possible that the problem occurs because javax.xml.validation.*
package is not included in standard java distribution?
Here's a sample application showing the problem:
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import com.oyster.mom.contentserver.export.Xml;
public class DOMSchemaValidation {
private final static String SCHEMA = "<xs:schema
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"><xs:element
name=\"package\" type=\"xs:anyType\" /></xs:schema>";
private final static String XML = "<package/>";
private static Validator getValidator(Schema schema) {
Validator validator = schema.newValidator();
validator.setErrorHandler(new DefaultHandler() {
public void error(SAXParseException e) throws SAXException {
System.out.println("error: " + e.getMessage());
throw e;
}
});
return validator;
}
public static void main(String[] args) {
try {
SchemaFactory schemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new StreamSource(new
StringReader(SCHEMA)));
Validator validator = getValidator(schema);
System.out.println("dom parsed document:");
System.out.println(" stream validation...");
validator.validate(new StreamSource(new StringReader(XML)));
System.out.println(" ...passed");
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(new
StringReader(XML)));
System.out.println(" dom validation...");
validator.validate(new DOMSource(document));
System.out.println(" ...passed");
{
documentBuilder = documentBuilderFactory.newDocumentBuilder();
System.out.println("dom created document:");
document = documentBuilder.newDocument();
Element packageElement = document.createElement(Xml.Package.TAG_PACKAGE);
document.appendChild(packageElement);
System.out.println(" dom validation...");
validator.validate(new DOMSource(document));
System.out.println(" ...passed");
}
}
catch (Exception e) {
System.out.println("... failed");
e.printStackTrace(System.out);
}
}
}