Validation of DOM document throws org.xml.sax.SAXParseException:

Marcin Cenkier

Usenet Archive
Joined
Nov 17, 2005
Messages
6
Reaction score
0
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); } } }
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Marcin Cenkier wrote:

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?

I am not sure, as the other stuff works for you with 1.4.2 and you get everything compiled and only a SAXParseException later I don't think it is a problem with classes missing on your class path.

For what its worth, I reduced that example to the DOM stuff

SchemaFactory schemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(SCHEMA))); Validator validator = getValidator(schema);

DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance;
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder;
System.out.println("dom created document:");
Document document = documentBuilder.newDocument;
Element packageElement = document.createElement("package"); document.appendChild(packageElement); System.out.println(" dom validation..."); validator.validate(new DOMSource(document)); System.out.println(" ...passed");

and that works fine here for me on Windows XP (with Java 1.5.0_06 however):

dom created document:
dom validation...
...passed

Sorry, can't help with Java 1.4 and the libraries you used. 1.5 has some version of Xerces incorporated by Sun as far as I know. And the validation API is part of the SDK so no libraries are needed.
 

Marcin Cenkier

Usenet Archive
Joined
Nov 17, 2005
Messages
6
Reaction score
0
Martin Honnen napisał(a):
Sorry, can't help with Java 1.4 and the libraries you used. 1.5 has some version of Xerces incorporated by Sun as far as I know. And the validation API is part of the SDK so no libraries are needed.

Apparently there's a problem with validation of manually created DOM documents in java 1.4.2. Any idea how to solve it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top