忽略XOM Parser中的DTD元素以避免没有找到File异常

时间:2013-07-15 14:42:32

标签: java parsing dtd xom

我需要在下面的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 Parser使用下面的Java代码解析XML文件。我相信我不需要这个DTD。我读到entityResolvers和setFeature为false但我无法在下面的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");

1 个答案:

答案 0 :(得分:7)

我导入了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);