File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Reading XML from Web Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Reading XML from Web" Watch "Reading XML from Web" New topic

Reading XML from Web

Amit Dua

Joined: Jan 29, 2002
Posts: 2
Hi All,
I need your help to solve a problem related to parsing XML Stream.
We have an application which reads data in form of XML over HTTP.Earlier , I was using parse method of DocumentBuilder , something like
document = builder.parse(PULL_URL);
But every now and then , it hands at this line i.e. at parse method.
I read good words about HTTPClient from apache on this forum and started using that .present code looks like something :
XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
//SaxSpiceUnmarshaller handler = new SaxSpiceUnmarshaller();
// Parse each file provided on the
// command line.
//new code added to avoid URLConnection glitches
HostConfiguration hc=new HostConfiguration();
client = new HttpClient();
method = new GetMethod("query/smsinxml.asp");
int statusCode = -1;
int attempt = 0;
// We will retry up to 3 times.
while (statusCode == -1 && attempt < 11) {
// execute the method.
statusCode = client.executeMethod(method);
catch (HttpRecoverableException e)
System.err.println("A recoverable exception occurred,retrying. " + e.getMessage());
attempt = attempt +1;
catch (IOException e)
System.err.println("Failed to download file.");
attempt = attempt+1;
// Check that we didn't run out of retries.
if (statusCode == -1) {
System.err.println("Failed to recover from exception.");
//close the database connection and raise NHF Alert

bin=method.getResponseBodyAsStream() ;
// Release the connection.
xr.parse(new InputSource(bin));
I am still facing the same problm.Application hangs every now and then reading from that URL and starts after long duration(sometimes 30mins) . is it not safe reading XML as inputstream ? I have document of size not more than 1 MB and using SAX API to handle the XML nodes one by one, without first loading the whole document in memory.
I have tried all things which i can think of.Please help me out in this.

Thanks in Advance !
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
XML parsers handle InputStreams just fine. The problem must be at the networking level (unless there is a bug report for your particular parser version) so I'm moving this post over to the Sockets and Internet Protocols forum.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I agree. Here's the link:
subject: Reading XML from Web
It's not a secret anymore!