javaparsingdtdxom

Ignore DTD element in a XOM Parser to avoid no File found exception


I need to ignore this DTD file path in the below XML to avoid file not found exception.

   <?xml version='1.0' encoding="UTF-8"?>
    <!DOCTYPE Document SYSTEM "/usr/home/billadm/release/binaries_39862//CMS/resource.4444/docgenlib/BillingDocument.dtd">
    <Document Sender="Testing Me" Id="130713BA00873650912" BAId="BA0087365091">
    <Summary>
    ...
    </Summary>

I use XOM Parser to parse the XML file using the below Java Code. I am sure I don't need this DTD. I read about entityResolvers and setFeature as false but I could not apply any on the below XOM Parser

public static void main (String [] args) {

        try {

            File folder = new File("D:\\Yahya_sum/");
            File[] listOfFiles = folder.listFiles();

            for (int i = 0; i < listOfFiles.length; i++) {
                  if (listOfFiles[i].isFile()) {
                    System.out.println("File " + listOfFiles[i].getName());
                  } else if (listOfFiles[i].isDirectory()) {
                    System.out.println("Directory " + listOfFiles[i].getName());
                  }

            String filename = "D:\\Yahya_sum\\"+listOfFiles[i].getName();

            File fXmlFile = new File (filename);

            Builder builder = new Builder();



            nu.xom.Document doc = builder.build(fXmlFile);




            String outputFile = i+" - sum.txt";

            PrintWriter writer = new PrintWriter(outputFile, "UTF-8");

            nu.xom.Element summary = doc.getRootElement().getFirstChildElement("Summary");

Solution

  • I imported SAX libraries

    import java.io.File;
    import java.io.PrintWriter;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLReaderFactory;
    

    Created SAX XML Reader

    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    

    Set the feature to false

        xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    

    Created a builder using the above XMLReader

    Builder builder = new Builder(xmlReader);
    

    Parsed it using XOM parser

    nu.xom.Document doc = builder.build(fXmlFile);