Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

XML in a unit test

Thread view: 
Kenneth P. Turvey - 25 Oct 2007 17:04 GMT
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,

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>

ociardhp - 25 Oct 2007 17:55 GMT
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.

Paul

On Oct 25, 5:04 pm, "Kenneth P. Turvey" <kt-use...@squeakydolphin.com>
wrote:
> I'm new to using XML in Java, so please forgive me if this is an easy
> question.
[quoted text clipped - 26 lines]
> --
> Kenneth P. Turvey <kt-use...@squeakydolphin.com>
Kenneth P. Turvey - 25 Oct 2007 18:28 GMT
> 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.

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>

Daniel Pitts - 25 Oct 2007 22:20 GMT
>> Hopefully the following article will help
>> http://www.xml.com/pub/a/2004/03/03/catalogs.html
[quoted text clipped - 6 lines]
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Arne Vajhøj - 27 Oct 2007 18:05 GMT
> 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
[quoted text clipped - 8 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.