2016-02-28 80 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