需要解析其他网页的值。首先,我需要调用其他网页并从中解析XML值

时间:2016-02-28 08:09:44

标签: javascript java angularjs xml

我正在开发一个应该显示货币汇率的项目,所以我打算调用另一个网页来获取该页面的汇率值。我在Angular-js中尝试但是我无法从网页获得响应(在Angular JS中:我们只能调用JSON / Rest url)。我在XMLHttpRequest中尝试但是如果我们调用它,它将不会调用网页(url)来自otherdomain的网页(因为CORS)。

类似地,我尝试使用Java并成功调用了网页并获得了XML但我无法解析该值(获取错误:“未格式化的XML”)。

有人可以指导我,我如何从任何网页获取价值。请告诉我,无论如何我可以使用API​​调用或任何webservice调用来实现。如果我使用API​​或Webservice调用,那么我是否需要与moneyexchange网站的IT供应商进行通信,以使API / webservice消耗特定的值?

请帮助我(我准备在任何技术上实施)

Java代码:     

    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 :(我只是想检查它是否返回任何值,但没有成功。但是当我在不同的域中尝试时,我在XMLHttpRequest(javascript)中遇到了CORS问题)

Angular-JS代码:

<!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 个答案:

答案 0 :(得分:0)

基本上你需要解析HTML文档。为此,请使用JSoup。这将是您的四个用例的理想选择。在java中拥有Document对象后,您可以解析并从中获取所需的值。

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