JSP如何调用某个地址生成的XML

yuanjj 2014-11-11 03:45:56
如下图,如何用JSP调用XML里的内容
...全文
68 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼上正解,通过远程地址加载获取XML内容;然后通过DOM4J来解析。
 
        String content = "<xml....";
	ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(is);
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
	    is.close();
	}
        if(document == null){
            return null;
        }
        Element root = document.getRootElement();
shixitong 2014-11-11
  • 打赏
  • 举报
回复
如果是获取这个地址生成的xml,可以参考下面
import java.io.*;
import java.net.*;

/**
 * Created by Administrator on 14-11-11.
 */
public class Test1 {
    public static void main(String args[]) throws IOException {
        //URL url = new URL("http://bbs.csdn.net/forums/Java.atom");
        String url = "http://bbs.csdn.net/forums/Java.atom";
        System.out.println(readXMlContent(url));
    }
    public static String readXMlContent(String url) throws IOException {
        StringBuffer temp = new StringBuffer();
        URLConnection uc = new URL(url).openConnection();
        uc.setConnectTimeout(10000);
        uc.setDoOutput(true);
        InputStream in = new BufferedInputStream(uc.getInputStream());
        Reader rd = new InputStreamReader(in,"UTF-8");
        int c = 0;
        while ((c = rd.read()) != -1) {
            temp.append((char) c);
        }
        in.close();
        return temp.toString();
    }
}
yuanjj 2014-11-11
  • 打赏
  • 举报
回复
给个具体代码看看
猎魔人-不纯 2014-11-11
  • 打赏
  • 举报
回复
dom 解析吧,或者找个工具转换为json,好操作些

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧