2016-02-28 75 views
1

我正在开发一个应显示货币汇率的项目,因此我打算调用另一个网页以获取该页面的汇率值。我尝试了Angular-js,但是我无法获得网页的响应(在Angular JS中:我们只能调用JSON/Rest url)。我在XMLHttpRequest中尝试过,但如果我们调用它,它将不会调用网页(url)来自其他域的网页(CORE的Beacuse)。需要解析其他网页的值。首先,我需要调用其他网页并从中解析XML值

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

有人可以引导我,我怎么能从任何网页获得价值。请让我知道,无论如何,我可以通过使用API​​调用或任何Web服务调用来实现。如果我使用API​​或Web服务调用,那么我是否需要与MoneyExchange网站的IT供应商进行通信,以便使API/Web服务消耗特定的值。

请帮我在同一个(我愿意在任何技术来实现)

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); 


} 
} 

角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>

+0

您想从支持JSONP获取数据的其他网站?请参阅https://remysharp.com/2007/10/08/what-is-jsonp –

+0

@SteveJorgensen,感谢您的更新,现在我使用** [jsoup](http://jsoup.org/)获得了解决方案。 ** – Vasant

回答

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); 
+0

感谢您的更新,现在** [jsoup](http://jsoup.org/)**适用于我:) – Vasant