日期:2014-05-16  浏览次数:20356 次

如何用servlet取代javascript获取某个给定url的feed的xml内容?
我现在可以在浏览器中用javascript,使用xmlhttprequest对象,根据feed的url调用get,获取这个feed的xml文档形式的内容,并把需要的items显示在网页上。

但现在要求浏览器不要直接获得feed的内容,所有内容要统一由服务器端去获得,并定时更新,然后基于浏览器的请求,把内容返回给浏览器。

服务器端是servlet,那servlet的java代码可以像javascript中   一样使用xmlhttprequest对象来获取feed的xml内容么?我怎么看到XMLHttpRequest只是专门用于javascript的阿。

那我究竟应该怎样在servlet的java代码中获取一个已知url的feed的文档内容?(有人说调用什么get?不大懂)具体请指点下,谢谢!

------解决方案--------------------
用jdom来做需要jdom.jar
package com.king.utils;

import java.io.IOException;
import java.io.StringBufferInputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

/**
* @author Kane.D
*
*/

public class XMLParser
{

private DocumentBuilderFactory domfac;

private Document doc;

//protected XMLField[][][] xMLFields;

public XMLParser()
{
domfac = DocumentBuilderFactory.newInstance();
}

public void parse(String xMLString)
{

try
{
DocumentBuilder dombuilder = domfac.newDocumentBuilder();

try
{
doc = dombuilder.parse(new StringBufferInputStream(xMLString));
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

Element root = doc.getDocumentElement();
NodeList tables = root.getElementsByTagName( "table ");
int countx = tables.getLength();
//xMLFields = new XMLField[countx][][];
Element table;
for (int i = 0; i < countx; i++)
{
table = (Element) tables.item(i);
NodeList records = table.getElementsByTagName( "record ");
int county = records.getLength();
//xMLFields[i] = new XMLField[county][];
Element record;
for (int j = 0; j < county; j++)
{
record = (Element) records.item(j);
NodeList fields = record.getElementsByTagName( "field ");
int countz = fields.getLength();
//xMLFields[i][j] = new XMLField[countz];
Element field;
for (int k = 0; k < countz; k++)
{
//xMLFields[i][j][k] = new XMLField();
field = (Element) fields.item(k);
NodeList names = field.getElementsByTagName( "name ");
if (names.getLength() == 1)
{
Element name = (Element) names.item(0);
Text nametxt = (Text) name.getFirstChild();
//xMLFields[i][j][k].setName(nametxt.getNodeValue()); <