如何从java中的URL读取XML响应? [英] How to read XML response from a URL in java?

查看:393
本文介绍了如何从java中的URL读取XML响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个简单的函数来获取URL并处理XML或JSON的响应,我已经检查了Sun网站 https://swingx-ws.dev.java.net/servlets/ProjectDocumentList ,但是无处可找到HttpRequest对象,是否可以在Java中执行此操作?我正在编写一个富客户端应用程序。

I need to write a simple function that takes a URL and processes the response which is XML or JSON, I have checked the Sun website https://swingx-ws.dev.java.net/servlets/ProjectDocumentList , but the HttpRequest object is to be found nowhere, is it possible to do this in Java? I`m writting a rich client end app.

推荐答案

对于输入流的xml解析,你可以这样做:

For xml parsing of an inputstream you can do:

// the SAX way:
XMLReader myReader = XMLReaderFactory.createXMLReader();
myReader.setContentHandler(handler);
myReader.parse(new InputSource(new URL(url).openStream()));

// or if you prefer DOM:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());

但是要通过http从服务器到客户端进行通信我更喜欢使用粗野图书馆或spring http invoker lib

But to communicate over http from server to client I prefer using hessian library or springs http invoker lib

这篇关于如何从java中的URL读取XML响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆