2

I have this code:

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++) {

line 2 gives following exception

E:\workspace\test\testDomain\src\com\test\ins\nxg\maps\Right.hbm.xml
...***java.net.SocketException: Operation timed out: connect:could be due to invalid address
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:372)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
unwind
  • 391,730
  • 64
  • 469
  • 606

4 Answers4

9

Parhaps the DocumentBuilder is unsuccessfully trying to access a DTD via a network socket for your XML document?

If there are DTD references in the XML document, try editing them out to prove the cause.

If that fixes your problem, I think you can use an EntityResolver for a more permanent solution, but I've not done it myself.

brabster
  • 42,504
  • 27
  • 146
  • 186
1

The answer by Brabster is very helpful to me. In my case I have an XML document starting with

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

This caused a problem for DocumentBuilder. I got a time out problem. The true evil is in the content of the 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;

After deleting

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

My program can at least move forward!

Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
0

Try to simplify your problem.

Can you get the code, you have to parse, manually?

If yes, try to parse it. I don't think it's the problem of your DocumentBuilder but your network connection. So you have to ensure, that the DocumentBuilder is able to access every bit of the xml document.

If your manually stored document fails when it is validated, there will be a different error message.

Hope it helps.

guerda
  • 23,388
  • 27
  • 97
  • 146
0

Did you create a new instance of a DocumentBuilderFactory and then create a newDocumentBuilder before you parse the file?

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

Hope this link helps. It definitely helped me earlier today.

kevindaub
  • 3,293
  • 6
  • 35
  • 46