我需要在下面的XML中忽略此DTD文件路径,以避免找不到文件异常。

   <?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>

我使用XOM分析器通过以下Java代码分析XML文件。我确定我不需要此DTD。我读到了有关EntityResolvers和setFeature为false的信息,但无法在下面的XOM分析器中应用任何内容
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");

最佳答案

我导入了SAX库

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

创建了SAX XML Reader
XMLReader xmlReader = XMLReaderFactory.createXMLReader();

将该功能设置为false
    xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

使用上述XMLReader创建了一个构建器
Builder builder = new Builder(xmlReader);

使用XOM解析器对其进行解析
nu.xom.Document doc = builder.build(fXmlFile);

10-04 21:47