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

Need to parse a value from other webpage. First I need to call other webpage and parse the XML value from it

本文关键字:网页 其他 XML 首先 调用      更新时间:2023-09-26

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

同样,我尝试使用 Java 并成功调用网页并获取 XML,但我无法解析值(收到错误:"未格式化的 XML")。

有人可以指导我,我如何从任何网页中获得价值。请让我知道我是否可以通过使用 API 调用或任何 Web 服务调用来实现。如果我使用 API 或 Web 服务调用,那么我是否需要与货币兑换网站的 IT 供应商进行通信,以便让 API/Web 服务使用特定值??.

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

爪哇代码:

   包 webXMRead;
import java.io.IOException; 导入java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformURLException; import java.net.URISyntaxException; 导入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;
公共类网页XMLRead { public static void main(String args[]) 抛出 URISyntaxException, ClientProtocolException, IOException, MalformURLException {
出于研究和示例目的,我选择了网址:http://www.google.com,需要解析本网站,我不用于任何盈利目的
字符串网址 ="http://www.google.com"; System.out.println("Url is careated****"); 网址 url2 = 新网址(网址); 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问题)

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

基本上你需要解析一个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);