472,789 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

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 7503


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Zandy Marantal | last post by:
Hello everyone, I'm having trouble using Xerces2(2.4, 2.5) when validating against an XML schema if a general entity reference is defined within the XML file. The error I'm getting is this:...
2
by: Olaf Meyer | last post by:
Apprentently xerces 2.6.0 (Java) does not validate against contraints specified in the schema (e.g. constraints specified via unique element). The validation works with the XML editor I'm using...
0
by: Brian | last post by:
I am having alot of trouble getting a XML document validated with a schema. I got a sample document and schema off of w3schools.com, which passed an online xml validator:...
0
by: motoman | last post by:
I have a problem with the code sample below, basically when i run it i get the error classnotfoundexception org.apache.xerces.parsers.SAXParser now i know the simple answer is declare xerces.jar...
0
by: motoman | last post by:
i have the following code (see below), i run my validation and get unexpected results. Public ID: null System ID: file:///C:/DATA/Sandpit/Eclipse/workspace/dtdValidation/memory.xml Line number:...
3
by: ilockett | last post by:
The background: I have a web app with a simple master page that contains just one content placeholder. I have created a web form that then uses this master page. Within the content...
3
by: PBR | last post by:
How can I capture the Element name, and generate meaningful error messages, when a validation error occurrs.I am using java APIs to validate (javax.xml.validation)
0
by: sk | last post by:
I am trying to make validation program but getting the following error. org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'tag1'. I attached the xml document and...
0
by: t | last post by:
If validation fails at several places of the document, we can retrieve the line numbers of those locations using SAXParseexception. getLineNumber(), but i wnat the Xpath of that element . can that...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.