This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes DTD Validation problem via Sax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DTD Validation problem via Sax" Watch "DTD Validation problem via Sax" New topic
Author

DTD Validation problem via Sax

alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
Hello everybody,

I wrote a http servlet. My servlet getting xml requests over HTTP ,processing xml and sending back to client successfully. But there is a problem about validation via DTD.
I want to validate incoming xmls via DTD.
MY sample DTD : ADD_CLIENT.dtd :
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT ADD_SERVICE (MESSAGE_ID, XXX_SUBSCRIBER_ID,TEL_NUMBER)>
<!ELEMENT MESSAGE_ID (#PCDATA)>
<!ELEMENT XXX_SUBSCRIBER_ID (#PCDATA)>
<!ELEMENT TEL_NUMBER (#PCDATA)>


Sample Incoming xml :
<?xml version="1.0" encoding="UTF-8"?><ADD_SERVICE><MESSAGE_ID>309832903820923803282 308</MESSAGE_ID><XXX_SUBSCRIBER_ID>134523278</XXX_SUBSCRIBER_ID><TEL_NUMBER>03123445566</TEL_NUMBER></ADD_SERVICE>

But i am getting this exception constantly:

Error:
..
Line: 1
Error: Document root element "ADD_SERVICE", must match DOCTYPE root "null".|#]

..
Line: 1
Error: Document is invalid: no grammar found.|#]


I am changing my dtd and xml. But still i am getting same error. After getting this exception servlet continues its work properly.

Sample Code :


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The system isn't finding your DTD. That's because you are providing only a relative path for it and making an incorrect assumption about what the current working directory is when you run that code.
alp carsikarsi
Ranch Hand

Joined: Dec 27, 2005
Posts: 40
I am writing the path before using it. The path seems to be correct. ADD_CLIENT.dtd file is there !

System.out.println("path =" + new File(XML_DTD_PATH).getAbsolutePath());
inputSource.setSystemId(XML_DTD_PATH);


Result in server.log:

path =C:\sailfin\domains\fcsdomain\config\resources\ADD_CLIENT.dtd

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The error messages pretty clearly indicate that your DTD isn't being seen. So now consider the fact that the InputSource.setSystemId method asks for a URL. You aren't passing it a URL. And also, you're supposed to be passing it the system ID of the XML document, not the location of the DTD. I think that would be the file:// URL for the directory containing the DTD.
Robert Monical
Greenhorn

Joined: Mar 27, 2009
Posts: 1
I have a similar problem moving a stable app from Java 5 to Java 6. The DTD used to be resolved relative to the file system location from which the referencing document was opened. Now it is resolved relative to the location from which the application was started.

Edit: April 4. What I ended up doing was creating a EntityResolver and adding it to the DocumentBuilder.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DTD Validation problem via Sax