Ignore DTD element in XOM Parser to avoid file exclusion

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

<?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 am using XOM Parser to parse an XML file using the Java code below. I am sure that I do not need this DTD. I read about Resolvers and setFeature objects as false, but I could not apply them on the next 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"); 
+4
source share
1 answer

I imported SAX libraries

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

Created by SAX XML Reader

 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 

Set the function to false

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

Created a builder using the XMLReader above

 Builder builder = new Builder(xmlReader); 

Developed it using the XOM analyzer

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

Source: https://habr.com/ru/post/1491454/


All Articles