Javascript 需要解析来自其他网页的值。首先,我需要调用其他网页并从中解析XML值
我正在开发一个项目,应该显示货币汇率,所以我计划调用另一个网页,从该网页获取汇率值。我尝试了Angular js,但我无法从网页获得响应(Angular js:我们只能调用JSON/Rest url)。我尝试了XMLHttpRequest,但如果我们从其他域调用网页(因为CORS),它不会调用网页(url) 类似地,我尝试使用Java,成功地调用了网页并获得了XML,但无法解析值(获取错误:“未格式化的XML”) 有人能告诉我,我怎样才能从任何网页上获得价值。请让我知道我是否可以通过使用API调用或任何webservice调用来实现。如果我使用API或Webservice调用,那么我是否需要与moneyexchange网站的IT供应商沟通,以便让API/Webservice使用特定的值 请在这方面帮助我(我准备在任何技术上实现) Java代码: package webXMRead;Javascript 需要解析来自其他网页的值。首先,我需要调用其他网页并从中解析XML值,javascript,java,angularjs,xml,Javascript,Java,Angularjs,Xml,我正在开发一个项目,应该显示货币汇率,所以我计划调用另一个网页,从该网页获取汇率值。我尝试了Angular js,但我无法从网页获得响应(Angular js:我们只能调用JSON/Rest url)。我尝试了XMLHttpRequest,但如果我们从其他域调用网页(因为CORS),它不会调用网页(url) 类似地,我尝试使用Java,成功地调用了网页并获得了XML,但无法解析值(获取错误:“未格式化的XML”) 有人能告诉我,我怎样才能从任何网页上获得价值。请让我知道我是否可以通过使用API调
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();
包webXMReadHttpResponse 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); } }
导入java.io.IOException; 导入java.io.InputStream; 导入java.net.HttpURLConnection; 导入java.net.MalformedURLException; 导入java.net.URISyntaxException; 导入java.net.URL
导入javax.xml.parsers.DocumentBuilder; 导入javax.xml.parsers.DocumentBuilderFactory
导入org.apache.http.HttpEntity; 导入org.apache.http.HttpResponse; 导入org.apache.http.client.ClientProtocolException; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.methods.HttpGet; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.util.EntityUtils; 导入org.w3c.dom.Document; 导入org.w3c.dom.Element; 导入org.w3c.dom.Node; 导入org.w3c.dom.NodeList
公共类网页XmlRead { publicstaticvoidmain(字符串args[])抛出URISyntaxException, ClientProtocolException,IOException,畸形异常{
//出于学习和示例目的,我选择了url:,需要解析此网站,我没有出于任何盈利目的使用它
字符串url=“”; System.out.println(“Url被插入****”; URL url2=新URL(URL); HttpGet HttpGet=新的HttpGet(url); HttpClient HttpClient=新的DefaultHttpClient()Angular JS:(我只是尝试检查它是否返回任何值,但没有成功。但当我在不同的域中尝试时,我在XMLHttpRequest(javascript)中遇到了CORS问题) 角JS代码:HttpResponse-HttpResponse=httpClient.execute(httpGet); HttpEntity entity=httpResponse.getEntity(); System.out.println(“实体为******”+实体); 试一试{ String xmlParseString=EntityUtils.toString(实体); System.out.println(“此字符串***”+xmlParseString); HttpURLConnection连接=(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(“这是第一个节点”+节点列表); 对于(int-getChild=0;getChild
基本上,您需要解析HTML文档。为此,请使用JSoup。这非常适合您的用例。一旦您拥有java中的document对象,您就可以解析并从中获得所需的值测试您的Web服务 {{data}} var app=angular.module('webpage',[]); app.controller('booksCtrl',函数($scope,$http){ /*$httpProvider.defaults.useXDomain=true*/ /*删除$http.defaults.headers.common['X-Requested-With']*/ /*只是为了学习,而不是为了任何利益使用,所以举个例子,我使用了URL:http://www.google.com, */ $http.get(“http://www.google.com") .然后(功能(响应){ $scope.data=response.data; }, 功能(错误响应){ 警报(“错误”+errresponse.status); }); });
基本上,您需要解析HTML文档。为此,请使用JSoup。这非常适合您的用例。一旦您拥有java中的document对象,您就可以解析并从中获得所需的值String html=“第一次解析” +“将HTML解析为文档。”; Document doc=Jsoup.parse(html);
您希望从support JSONP获取数据的其他站点是否存在?请参阅@SteveJorgensen,感谢您的更新,现在我使用获得解决方案您希望从support JSONP获取数据的其他站点是否存在?请参阅@SteveJorgensen,感谢您的更新,现在我使用String html=“第一次解析” +“将HTML解析为文档。”; Document doc=Jsoup.parse(html);
String html = "<html><head><title>First parse</title></head>" + "<body><p>Parsed HTML into a doc.</p></body></html>"; Document doc = Jsoup.parse(html);