469,636 Members | 1,555 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

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

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_SCH EMA_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);
}
}
}

--
Marcin Cenkier [mailto_marcin.cenkier_at_cognifide_com]
Jan 18 '06 #1
2 7356


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_SCH EMA_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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 18 '06 #2
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?

--
Marcin Cenkier [mailto_marcin.cenkier_at_cognifide_com]
Jan 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Brian | last post: by
reply views Thread by motoman | last post: by
reply views Thread by sk | last post: by
reply views Thread by t | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.