1
votes

I am developing one project that should show currency exchange rate, so I planned to call another webpage to get the exchange rate values from that page. I tried in Angular-js but I couldn't get the response from webpage(in Angular JS: we can call only JSON/Rest url ).I tried in XMLHttpRequest but it won't call the webpage(url) if we call the webpage from otherdomain( Beacuse of CORS ).

Similarly, I tried in Java and I successfully called the webpages and got XML but I couldn't parse the value(getting error:"un-formatted XML").

Can someone please guide me, how i can get the value from any webpage. Please let me know is there anyway that i can implement by using API call or any webservice call. If I go with API or Webservice call then should I need to communicate with IT-vendor of moneyexchange website in order to get the API/webservice to consume particular values ??.

Please help me on the same(I am ready to implement on any technology)

Java code:

    package webXMRead;
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList;
public class webPageXMLRead { public static void main(String args[]) throws URISyntaxException, ClientProtocolException, IOException, MalformedURLException {
//For study and example purpose I took url:http://www.google.com , need to parse this website, I am not using for any profit purpose
String url = "http://www.google.com"; System.out.println("Url is careated****"); URL url2 = new URL(url); HttpGet httpGet = new HttpGet(url); HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
System.out.println("Entity is*****" + entity);
try {
String xmlParseString = EntityUtils.toString(entity);
System.out.println("This Stirng ***" + xmlParseString);

HttpURLConnection connection = (HttpURLConnection) url2
                .openConnection();
InputStream inputStream = connection.getInputStream();

  DocumentBuilderFactory builderFactory = DocumentBuilderFactory
               .newInstance();
  DocumentBuilder documentBuilder = builderFactory
               .newDocumentBuilder();
 Document document = documentBuilder.parse(inputStream);
document.getDocumentElement().normalize();



  NodeList nodeList = document.getElementsByTagName("rss");
  System.out.println("This is firstnode" + nodeList);
   for (int getChild = 0; getChild < nodeList.getLength(); getChild++) {
     Node Listnode = nodeList.item(getChild);
     System.out.println("Into the for loop"
                    + Listnode.getAttributes().getLength());
     Element firstnoderss = (Element) Listnode;
     System.out.println("ListNodes" + Listnode.getAttributes());
     System.out.println("This is node list length"
                + nodeList.getLength());

     Node Subnode = nodeList.item(getChild);
     System.out.println("This is list node" + Subnode);

  }

 } catch (Exception exception) {

        System.out.println("Exception is" + exception);


 }
}

Angular-JS: (I just tried to check whether it return any any value, but no success. But I faced CORS problem in XMLHttpRequest(javascript) when i tried in different domain)

Angular-JS code:

<!DOCTYPE html>
<html>
<head>
    <title>test your webservice</title>
</head>
<body>


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<article ng-app="webpage">
  <section ng-controller="booksCtrl">
  <h2 >{{data}} </h2>
  </section>
</article>
<script type="text/javascript">
var app = angular.module('webpage', []);

app.controller('booksCtrl', function($scope, $http) {
/* $httpProvider.defaults.useXDomain = true;*/
    /*delete $http.defaults.headers.common['X-Requested-With'];*/

/*just for study purpose, not for any profit usage, so for example purpose I used URL:http://www.google.com, */

  $http.get("http://www.google.com")
    .then(function(response) {
        $scope.data=response.data;
        
 
    },

    function(errresponse) {
     alert("err"+errresponse.status);
    });
});

</script>
</body>
</html>
1
Does the other site that you want to get the data from support JSONP? See remysharp.com/2007/10/08/what-is-jsonpSteve Jorgensen
@SteveJorgensen, Thanks for your updates, now I got the solution using jsoupVasant

1 Answers

0
votes

Basically you need to parse a HTML document. For that please use JSoup. This would be an ideal fit four your use case. Once you have the Document object in java, you can parse and get desired value from it.

String html = "<html><head><title>First parse</title></head>"
  + "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);