This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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: Document root element "ADD_SERVICE", must match DOCTYPE root "null".|#]
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.
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.
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.