Search code examples
javaxom

Set timeout when loading xml from a URL?


Is it possible to set a timeout when loading an xml directly from a URL?

Builder parser = new Builder();
Document doc = parser.build("http://somehost");

This may take sometimes minutes, and would be really handy to be able to time this out directly in the library.


Solution

  • You need to use build(InputStream inStream) api instead of build(String systemID).

    URL url = new URL("http://somehost");
    con = url.openConnection();
    con.setConnectTimeout(connectTimeout);
    con.setReadTimeout(readTimeout);
    inStream = con.getInputStream();
    Builder parser = new Builder();
    Document doc = parser.build(inStream);