XStream解析xml

前端之家收集整理的这篇文章主要介绍了XStream解析xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个工具之前没用过,这次为了解析stock data使用了下,还是比较好用的

这是最后的调用

		XStream xstream = new XStream(new DomDriver());
		xstream.processAnnotations(ControlList.class);
		xstream.autodetectAnnotations(true);
		ControlList list = (ControlList) xstream.fromXML(xml);
下面是List对象
@XStreamAlias("control")
public class ControlList {
	@XStreamImplicit(itemFieldName = "content")
	private List<Content> content;
下面是属性对象,这里要注意,如果不是属性,那就只需要@XStreamAlias就可以了
@XStreamAlias("content")
public class Content {
	@XStreamAsAttribute
	@XStreamAlias("d")
	private String date;
XML数据如下:
<pre name="code" class="html">- <control>
  <content d="2014-01-02" o="11.400" h="11.530" c="11.460" l="11.260" v="229859" bl="" /> 
  <content d="2014-01-03" o="11.340" h="11.380" c="11.190" l="11.110" v="262835" bl="" /> 
  <content d="2014-01-06" o="11.140" h="11.420" c="11.320" l="10.850" v="354200" bl="" /> 
工具类,读取xml数据
<pre name="code" class="html">	private String connReadData(String url) {
		StringBuffer sb = new StringBuffer();
		try {
			URL httpUrl = new URL(url);
			URLConnection connection = httpUrl.openConnection();
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line = null;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			if (br != null) {
				br.close();
				br = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
郑州就维软件 原文链接:https://www.f2er.com/xml/298055.html

猜你在找的XML相关文章