1

Possible Duplicate:
Make DocumentBuilder.parse ignore DTD references

Here is my sample XML

<?xml version="1.0" standalone="no"?>
<!DOCTYPE factory-requirements SYSTEM "factory.7.0.dtd">
<data name="product">
  <product name="container">
    <one>20 feet</one>
    <two>50 feet</two>
  </product>
</data>

When I try to pass this file into my Document I get an error .

java.io.FileNotFoundException: C:\Documents and Settings\johnB\workspace\project\factory.7.0.dtd (The system cannot find the file specified)

It seems that it is looking for the DTD also which I don't have. I just need to parse this file as an XML and get products. I will not be validating this XML against a DTD since I don't have a DTD and we only get this XML from our vendors.

Here is my code:

File fstream = new File("C:/Documents and Settings/johnB/workspace/project/factory_product.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fstream);
doc.getDocumentElement().normalize();

What can I do so it ignores <!DOCTYPE factory-requirements SYSTEM "factory.7.0.dtd"> when parsing?

Community
  • 1
  • 1
  • I got the answer I was looking for. Here is what needs to be added right after dBuilder is initialized. `dBuilder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (systemId.contains("factory.7.0.dtd")) { return new InputSource(new StringReader("")); } else { return null; } } });` – user1774507 Oct 25 '12 at 15:37

0 Answers0