Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to pass external dtd file path to SAXParser ?

843834Oct 16 2008 — edited Oct 16 2008
Hi,
In my application i am parsing xml files which includes dtd file path in DOCTYPE. I do not want parser to use the dtd file which is reffered in same xml file, instead of that i want to pass dtd file path from java program.
I have done lot of googling and come to know that it could be done by entityResolver. But the problem is that when I am passing path from the application is gives error during validation as
"The markup declarations contained or pointed to by the document type declaration must be well-formed."

The piece of code i am using looks like;

public class TestValidation extends DefaultHandler implements EntityResolver {

//---custom dtd file to set for validation------
public static String customDTD= "../../../demo/dtdFile/test.dtd";

//-----------Here validation xml file --------------
private static void validatePackages(XML xmlFileName) throws IOException, ParserConfigurationException, SAXException {

TestValidation errorHandler = new TestValidation ();

SAXParserFactory factory = SAXParserFactory.newInstance();

factory.setValidating(true);

try {

SAXParser parser = factory.newSAXParser();
if(!xmlFileName.exists()) {
System.err.println("Did not find file ");
}
parser.parse(f, errorHandler);
}

}finally {

errorHandler.setErrors(errorcount);
}

}


//-------------------Checking for dtd file given in xml file if yes then setting it to custom file----------------------------

public InputSource resolveEntity(String publicId, String systemId)
{

if (systemId.contains(".dtd")){
System.out.println("publicId="+publicId+" systemId="+systemId );

return new InputSource(new ByteArrayInputStream(customDTD.getBytes()));
}

else{
System.out.println("Other tag found");
return null;
}
}


public void error(SAXParseException e) throws SAXParseException {

---- Handling errors found;
}

}


I thing the way i am providing dtd file path is wrong.
Do any one know how to solve this problem.
If any one know than plz reply how to specify the dtd file with entity resolver. Does it need to specify with <!DOCTYPE >.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 13 2008
Added on Oct 16 2008
1 comment
468 views