Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Xml reading

 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml

From this URL , i have to read the currency value daliy from my application. i dont have idea about how to read the values.
Please help me , how to read this dyanmic content and which java api to use.
Thanks in advance,
Abiramkumar .p
 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

This is urgent requirement,please help me.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be urgent for you, but that doesn't make it urgent for the rest of us.

If it's so urgent, you must have done some research into this - what do you have so far?

Here's an article that demonstrates how to download an XML file, parse it and extract some relevant data.
It's a bit dated, and talks about using J2EE and installing and configuring parsers - you need to do none of that. Everything you need is part of J2SE these days and ready to use.
 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ulf Dittmer,
Finally i got output.

code:

try
{
String urlString="http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml";
URL url = new URL( urlString );
URLConnection URLconnection = url.openConnection ( ) ;
HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
int responseCode =httpConnection.getResponseCode ( ) ;
System.out.println(" responseCode "+ responseCode);
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance ( ) ;
DocumentBuilder db = dbf.newDocumentBuilder ( ) ;
InputStream in = httpConnection.getInputStream ( ) ;
Document document = db.parse( in );
NodeList nodes = document.getElementsByTagName("Cube");
System.out.println("nodes length "+nodes.getLength());
for(int i=1;i<nodes.getLength();i++)
{
Node firstElement = nodes.item(i);
NamedNodeMap nnm =firstElement.getAttributes ( ) ;
int nnmLength = nnm.getLength();
for(int j=0;j<nnmLength;j++)
{

System.out.println("Cube " + nnm.item(j));
}

}

}
catch(Exception e)
{
e.printStackTrace();
}



in my system, running fine.

But my friend machine i run the same code the following exception take place.

java.net.UnknownHostException: www.ecb.europa.eu
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.Socket.connect(Socket.java:520)
at java.net.Socket.connect(Socket.java:470)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:387)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:522)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:231)
at sun.net.www.http.HttpClient.New(HttpClient.java:304)
at sun.net.www.http.HttpClient.New(HttpClient.java:321)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:813)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:765)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:690)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:934)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:367)
at test4.main(test4.java:24)


I checked the urlString. Both are same.Please help me to solve this issue

Thanks in advance,
Abiramkumar.P
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But my friend machine i run the same code the following exception take place.

java.net.UnknownHostException: www.ecb.europa.eu

Is your friend's machine connected to the internet?
Can you open that URL from a browser from his machine?
 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, my friend machine connected with internet.This same URL i can open in browser in his machine
 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried to run using the following command

java -DproxySet=true -DproxyHost=proxyhost test4

but still i am getting same error.

Please help me to solve this issue.

Thanks in advance,
Abiramkumar.P
 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Based on some article , i added Following two lines in my code.

System.setProperty("http.proxyHost", "Proxy ip address");
System.setProperty("http.proxyPort", "8080");

code:
System.setProperty("http.proxyHost", "192.16.1.20");// consider this my proxy ip
System.setProperty("http.proxyPort", "8080");
String urlString="http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml";
URL url = new URL( urlString );
URLConnection URLconnection = url.openConnection ( ) ;
HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
int responseCode =httpConnection.getResponseCode ( ) ;
System.out.println(" responseCode "+ responseCode);
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance ( ) ;
DocumentBuilder db = dbf.newDocumentBuilder ( ) ;
InputStream in = httpConnection.getInputStream ( ) ;
Document document = db.parse( in );
NodeList nodes = document.getElementsByTagName("Cube");
System.out.println("nodes length "+nodes.getLength());
for(int i=1;i<nodes.getLength();i++)
{
Node firstElement = nodes.item(i);
NamedNodeMap nnm =firstElement.getAttributes ( ) ;
int nnmLength = nnm.getLength();
for(int j=0;j<nnmLength;j++)
{

System.out.println("Cube " + nnm.item(j));
}

}

}
catch(Exception e)
{
e.printStackTrace();
}


Following error takes place:
responseCode 407
java.net.ProtocolException: Server redirected too many times (20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1223)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1217)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:906)
at test4.main(test4.java:33)
Caused by: java.net.ProtocolException: Server redirected too many times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1178)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:367)
at test4.main(test4.java:29)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic