[size=small][color=red]我现在想实现这样一个功能,在本地传递参数到yahoo网站,该网站根据我的参数返回我需要的数据,返回的是xml格式文件,类似于动态查询天气服务的功能。现在我能够实现对本地的xml文件进行解析处理,但不知如何从yahoo网站上动态获取这个xml文件然后再解析,不知用什么方法可以实现。就是类似于动态查询城市天气的功能,关键问题是怎么从网站上得到这个xml文件然后本地解析通过ext的grid组件显示出来?[/color][/size]
[code="java"] var store = new Ext.data.Store({
// load using HTTP
url: 'bagSearch50.xml', //本地xml文件
// the return will be XML, so lets set up a reader
reader: new Ext.data.XmlReader({
// records will have an "Item" tag
record: 'Product',
id: 'Catalog' //'ASIN',
//totalRecords: '@total'
}, [
// set up the fields mapping into the xml doc
// The first needs mapping, the others are very basic
{name: 'ProductName', mapping: 'Catalog > ProductName'},
'Brand',
{name: 'Summary', mapping: 'Catalog > ProductName'},
'NumOfStores',
'PriceFrom',
'Description',
// Detail URL is not part of the column model of the grid
'Url'
])[/code]
[b]问题补充:[/b]
[size=x-small][color=blue]谢谢关注,有点抽象啊,不明白,可否再详细点,小弟新手,经验不足,还请多指导指导。[/color][/size]
[b]问题补充:[/b]
[code="java"]
var store = new Ext.data.Store({
url: 'Services/ArticleService.asmx/GetArticles', // Web Service 地址
reader: new Ext.data.XmlReader(
{
totalRecords: 'totalRecords', // 数据总行数。对应于 GetArticles 返回的 DataSet 中的 results 表的 totalRecores 列
record: 'record', // 数据。对应于 GetArticles 返回的 DataSet 中的 record 表
id: 'Id' // 主键。对应于 GetArticles 返回的 DataSet 中的 record 表的 Id 列
},
[
{name: 'Id'},
{name: 'Title'},
{name: 'Date'}
]
),
remoteSort: true // 服务端排序
});
store.setDefaultSort('Date', 'desc'); // 默认按 Date 列降序排列
[/code]
[color=blue]在网上找了一些相关的问题,发现url都是本地服务器处理的。我想要的是向yahoo网站发送参数,经过yahoo服务器处理返回的xml数据,但不知道本地怎样接收。在浏览器里输入http://xml.weather.yahoo.com/forecastrss?p=CHXX0008&u=c,会自动返回一个xml格式的数据给浏览器,现在就是不知道怎么接收这个xml,传递给grid[/color]
[size=x-small][color=red]url: 'Services/ArticleService.asmx/GetArticles', // Web Service 地址[/color][/size]
[b]问题补充:[/b]
谢谢你的解答,关键是这个servlet又怎么写呢?
[b]问题补充:[/b]
首先谢谢你的帮助。调试的时候还是遇到了点问题。搞不清楚什么原因。doPost里面的代码调试出错提示[color=red]Connection timed out[/color][color=red],把其中代码注释掉可以打印hello1,hello2,
不知道问题在哪里?
这里是store和grid代码[/color]
[code="java"] var store = new Ext.data.Store({
url:'http://localhost:8080/langsinext/test', //servelet
reader: new Ext.data.XmlReader({
record: 'Product',
id: 'Catalog' //'ASIN',
}, [
{name: 'ProductName', mapping: 'Catalog > ProductName'},
'Brand',
{name: 'Summary', mapping: 'Catalog > ProductName'},
'NumOfStores',
'PriceFrom',
'Description',
// Detail URL is not part of the column model of the grid
'Url'
])
});
var grid = new Ext.grid.GridPanel({
id:'grid-bag',
store: store,
columns: [
{header: "ProductName", width: 220, dataIndex: 'ProductName', sortable: true},
{header: "Brand", width: 80, dataIndex: 'Brand', sortable: true},
{header: "Summary", width: 300, dataIndex: 'Summary', sortable: true},
{header: "NumOfStores", width: 85, dataIndex: 'NumOfStores', sortable: true},
{header: "PriceFrom", width: 70, dataIndex: 'PriceFrom', sortable: true}
],
sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
height:300
});[/code]
[color=blue]这里是servlet类[/color]
[code="java"]
package com.wbr.action;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetArticlesServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
System.out.println("hello1");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
URL url = new URL("http://shopping.yahooapis.com/ShoppingService/V3/productSearch?appid=YahooDemo&category=Bags%20%26%20Wallets");
InputStream is = url.openStream();
ServletOutputStream sos = resp.getOutputStream();
byte[] buf = new byte[4096];
while(is.read(buf) != -1)
{
sos.write(buf,0,buf.length);
}
sos.close();
System.out.println("hello2");
}
}
[/code]
[b]问题补充:[/b]
那是什么原因呢,直接在浏览器里输入可以。把这个 [color=red]url: 'bagSearch50.xml'[/color],直接替换掉也能得到返回数据。
[b]问题补充:[/b]
[code="java"]<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.dom4j.*"%>
<%@ page import="org.dom4j.io.*"%>
<%
// String cityCode = request.getParameter("cityCode"); //获取城市代码
SAXReader reader = new SAXReader(); //创建一个SAXReader
//使用Reader解析远程服务器RSS信息
/*Document doc = reader.read("http://shopping.yahooapis.com/ShoppingService/V3/productSearch?appid=YahooDemo&category=Bags%20%26%20Wallets&result=15");*/
File file = new File("D:/workspace/langsinext/WebRoot/bagSearch50.xml");
Document doc = reader.read(file);
String text = doc.asXML();
// System.out.println(text);
response.setContentType("application/xml;charset=UTF-8");
response.getWriter().write(text);//输出天气信息
%>[/code]
非常感谢,我换了个思路,通过上述方法用dom4j解析处理,可以处理本地的文件,现在应该就是那个网站通过这种方式连不上的问题吧。我再摸索摸索。