wood burning stoves*
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
Author

Reading XML from Web

Amit Dua
Greenhorn

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();
xr.setContentHandler(this);
xr.setErrorHandler(this);
// Parse each file provided on the
// command line.
//new code added to avoid URLConnection glitches
HostConfiguration hc=new HostConfiguration();
client = new HttpClient();
client.setHostConfiguration(hc);
client.setConnectionTimeout(120000);
method = new GetMethod("query/smsinxml.asp");
int statusCode = -1;
int attempt = 0;
// We will retry up to 3 times.
while (statusCode == -1 && attempt < 11) {
try
{
// 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.");
e.printStackTrace();
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
objweb.closeConnection();
System.exit(-2);
}

bin=method.getResponseBodyAsStream() ;
// Release the connection.
xr.parse(new InputSource(bin));
bin.close();
method.releaseConnection();
****************************************************************
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
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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: http://aspose.com/file-tools
 
subject: Reading XML from Web
 
Similar Threads
[Apache Httpclient] Connection refused
axis2 stops making https connections randomly... HELP!
How to validate Url
Is there java class to parse xml nodes?
apache HttpClient make connection for url1 ,if url1 failed connect url2