File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 38
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: 18141
    
    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: 38
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: 18141
    
    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.
 
jQuery in Action, 2nd edition
 
subject: DTD Validation problem via Sax
 
Similar Threads
reading web.xml by XPATH
Count the no of a particular element in xml file
Referring to DTD in XML File
Issue Req Storing Result Set Data in a XML File
Parsing String which comes as xml