【问题标题】:Unable to parse XML file using DocumentBuilder无法使用 DocumentBuilder 解析 XML 文件
【发布时间】:2010-09-25 11:54:21
【问题描述】:

我有这个代码:

if (file.exists()) {
  Document doc = builder.parse(file);
  NodeList list = doc.getElementsByTagName("property");
  System.out.println("XML Elements: ");
  for (int ii = 0; ii < list.getLength(); ii++) {

第 2 行给出以下异常

E:\workspace\test\testDomain\src\com\test\ins\nxg\maps\Right.hbm.xml ...***java.net.SocketException:操作超时:连接:可能是由于地址无效 在 java.net.PlainSocketImpl.socketConnect(本机方法) 在 java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:372) 在 java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233) 在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)

【问题讨论】:

    标签: java xml parsing


    【解决方案1】:

    Brabster 的回答对我很有帮助。在我的例子中,我有一个以

    开头的 XML 文档
    <?xml version="1.0"?> <!DOCTYPE GBSet PUBLIC "-//NCBI//NCBI GBSeq/EN" http://www.ncbi.nlm.nih.gov/dtd/NCBI_GBSeq.dtd"> ... more to come
    

    这导致 DocumentBuilder 出现问题。我遇到了超时问题。真正的邪恶在于 URL 的内容:http://www.ncbi.nlm.nih.gov/dtd/NCBI_GBSeq.dtd:

    <!-- ============================================
     ::DATATOOL:: Generated from "gbseq.asn"
     ::DATATOOL:: by application DATATOOL version 1.5.0
     ::DATATOOL:: on 06/06/2006 23:03:48
     ============================================ -->
    
    <!-- NCBI_GBSeq.dtd
    This file is built from a series of basic modules.
    The actual ELEMENT and ENTITY declarations are in the modules.
    This file is used to put them together.
    -->
    
    <!ENTITY % NCBI_Entity_module PUBLIC "-//NCBI//NCBI Entity Module//EN"  
    "NCBI_Entity.mod.dtd">  %NCBI_Entity_module;
    
    <!ENTITY % NCBI_GBSeq_module PUBLIC "-//NCBI//NCBI GBSeq Module//EN" "NCBI_GBSeq.mod.dtd"> %NCBI_GBSeq_module;
    

    删除后

    <!DOCTYPE GBSet PUBLIC "-//NCBI//NCBI GBSeq/EN" "http://www.ncbi.nlm.nih.gov/dtd/NCBI_GBSeq.dtd">
    

    我的程序至少可以前进!

    【讨论】:

      【解决方案2】:

      您是否创建了 DocumentBuilderFactory 的新实例,然后在解析文件之前创建了 newDocumentBuilder?

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(file);
      

      希望link 有所帮助。今天早些时候它肯定对我有帮助。

      【讨论】:

        【解决方案3】:

        尽量简化你的问题。

        你能得到代码吗,你必须手动解析?

        如果是,请尝试解析它。我不认为这是您的 DocumentBuilder 的问题,而是您的网络连接的问题。因此,您必须确保 DocumentBuilder 能够访问 xml 文档的每一位

        如果您手动存储的文档在验证时失败,则会出现不同的错误消息。

        希望对你有帮助。

        【讨论】:

          【解决方案4】:

          也许 DocumentBuilder 没有成功地尝试通过网络套接字为您的 XML 文档访问 DTD?

          如果 XML 文档中有 DTD 引用,请尝试编辑它们以证明原因。

          如果这样可以解决您的问题,我认为您可以使用 EntityResolver 来获得更持久的解决方案,但我自己没有这样做。

          【讨论】:

            猜你喜欢
            • 2014-01-11
            • 1970-01-01
            • 2011-09-30
            • 2013-06-25
            • 1970-01-01
            • 2017-10-15
            • 1970-01-01
            • 2011-05-14
            • 2021-10-06
            相关资源
            最近更新 更多