XML in a unit test

K

Kenneth P. Turvey

I'm new to using XML in Java, so please forgive me if this is an easy
question.

I'm using NetBeans and I've written a unit test that just reads in an XML
document using the DocumentBuilder class. This is a document I've written
myself and I've written a DTD for it as well. So I would like it to be
validated while being read. So, I've called the setValidating(true)
method on the DocumentBuilderFactory before getting the DocumentBuilder
itself.

The problem is that I don't know what to do with the DTD file so that it
will be found when the document is being parsed. The DocumentBuilder
doesn't know where the xml file came from so the current directory doesn't
seem right. There doesn't seem to be anywhere to load the dtd file so
that the builder will be able to find it.

I'm getting the following error which says to me that this is the
problem, but maybe I'm misreading it.

org.xml.sax.SAXParseException:
Document root element "onlinequiz", must match DOCTYPE root "null".

I'm sure that many of you use this stuff all the time. How would you set
up a unit test to make sure a document is being parsed correctly? What do
you do with the DTD?

Thanks,
 
K

Kenneth P. Turvey

Hopefully the following article will help
http://www.xml.com/pub/a/2004/03/03/catalogs.html
The suggested approach has the benefit of avoiding network latency.

That looks interesting, but it might be overkill for what I'm trying to
do. I don't really need this DTD to be publicly accessible. I just need
my system to be able to find it. I'm not sure where it looks for a SYSTEM
dtd. I'm also not sure how to tell it where to look. If I could do that
then I would be in business. If I could just say, "Look in the classpath
for the dtd" that would be ideal.
 
D

Daniel Pitts

Kenneth said:
That looks interesting, but it might be overkill for what I'm trying to
do. I don't really need this DTD to be publicly accessible. I just need
my system to be able to find it. I'm not sure where it looks for a SYSTEM
dtd. I'm also not sure how to tell it where to look. If I could do that
then I would be in business. If I could just say, "Look in the classpath
for the dtd" that would be ideal.
You might consider having the DTD inline in the XML document. Thats what
I tend to do for small one-off XML files.
 
G

Guest

Kenneth said:
I'm using NetBeans and I've written a unit test that just reads in an XML
document using the DocumentBuilder class. This is a document I've written
myself and I've written a DTD for it as well. So I would like it to be
validated while being read. So, I've called the setValidating(true)
method on the DocumentBuilderFactory before getting the DocumentBuilder
itself.

The problem is that I don't know what to do with the DTD file so that it
will be found when the document is being parsed. The DocumentBuilder
doesn't know where the xml file came from so the current directory doesn't
seem right. There doesn't seem to be anywhere to load the dtd file so
that the builder will be able to find it.

You can either follow Daniels advice and inline the DTD in the XML file.

Or you can put in a resolver:

yourDocumentBuilder.setEntityResolver(new YourResolver());

where the YourResolver class has a method:

public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
if(systemId.endsWith("yourstuff.dtd")) {
return new InputSource("C:\\allyourdtds\\yourstuff.dtd");
} else {
return null;
}
}

Arne
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top