用xstream解析XML

public class WeBoSquareHandler {
	public static void main(String[] args) {
		parseXML(getXML());
	}

	public static void parseXML(String xmls) {

		XStream xstream = new XStream();
		// 设置别名
		xstream.alias("root",WeboSquare.class);
		xstream.alias("topic",Topic.class);
		xstream.alias("image",Image.class);
		xstream.alias("attach",Attach.class);
		xstream.alias("video",Video.class);
		xstream.alias("music",Music.class);
		xstream.alias("parent",Parent.class);

		// 忽略下列的名字,要用addImplicitCollection()带三个参数的方法,带两个参数的方法会乱了
		xstream.addImplicitCollection(Topic.class,"attachs",Attach.class);
		xstream.addImplicitCollection(Topic.class,"images",Image.class);
		xstream.addImplicitCollection(Topic.class,"videos",Video.class);
		xstream.addImplicitCollection(Topic.class,"musics",Music.class);

		// 设置属性
		xstream.useAttributeFor(WeboSquare.class,"status");
		xstream.useAttributeFor(WeboSquare.class,"code");

		System.out.println("\n将xml转换为Java对象!");
		WeboSquare weboSquare = (WeboSquare) xstream.fromXML(xmls);
		System.out.println(weboSquare.toString());
	}

	public static String getXML() {
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "<root status=\"0\" code=\"200\">" + "<total>1821</total>"
				+ "<max_tid>2043</max_tid>" + "<uid>1</uid>"
				+ "<count>20</count>" + "<topic>" + "<tid>2065</tid>"
				+ "<uid>1</uid>" + "<username>admin</username>"
				+ "<nickname>微博管理员</nickname>"
				+ "<face>http://t2.hq.ctc.com/images/face/b/43/1_s.jpg</face>"
				+ "<content>本微博包含了图片,视频,音乐和附件</content>"
				+ "<validate>0</validate>" + "<imageid>266</imageid>"
				+ "<videoid>7</videoid>" + "<musicid>6</musicid>"
				+ "<attachid>3,4,5</attachid>" + "<image>" + "<id>266</id>"
				+ "<small>{site_url}/images/topic/3/21/266_s.jpg</small>"
				+ "<original>{site_url}/images/topic/3/21/266_o.jpg</original>"
				+ "</image>" + "<attach>" + "<id>3</id>"
				+ "<attach_img>images/filetype/xls.gif</attach_img>"
				+ "<attach_name>jxwb_syds_2013060412 (5).xls</attach_name>"
				+ "<attach_score>0</attach_score>"
				+ "<attach_down>0</attach_down>"
				+ "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
				+ "<attach>" + "<id>4</id>"
				+ "<attach_img>images/filetype/xls.gif</attach_img>"
				+ "<attach_name>jxwb_syds_2013060412 (4).xls</attach_name>"
				+ "<attach_score>0</attach_score>"
				+ "<attach_down>0</attach_down>"
				+ "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
				+ "<attach>" + "<id>5</id>"
				+ "<attach_img>images/filetype/xls.gif</attach_img>"
				+ "<attach_name>jxwb_syds_2013060412 (3).xls</attach_name>"
				+ "<attach_score>0</attach_score>"
				+ "<attach_down>0</attach_down>"
				+ "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
				+ "<video>" + "<video_hosts>sina.com.cn</video_hosts>"
				+ "<video_url>{site_url}/…/142462517543.html</video_url>"
				+ "<video_img>{site_url}/images/211529932731.jpg</video_img>"
				+ "</video>" + "<music>" + "<music_id>2</music_id>"
				+ "<music_url>1769023170</music_url>" + "</music>"
				+ "<replys>0</replys>" + "<forwards>0</forwards>"
				+ "<type>reply</type>" + "<dateline>06月06日 18时28分</dateline>"
				+ "<from_string>来自新浪微博</from_string>"
				+ "<longtextid>11</longtextid>" + "<parent>"
				+ "<tid>2064</tid>" + "<uid>1</uid>"
				+ "<username>admin</username>" + "<nickname>微博管理员</nickname>"
				+ "<face>http://t2.hq.ctc.com/images/face/b/43/1_s.jpg</face>"
				+ "<content>音乐和视频</content>" + "<validate>0</validate>"
				+ "<imageid>265</imageid>" + "<videoid>6</videoid>"
				+ "<musicid>2</musicid>" + "<attachid>2</attachid>"
				+ "<replys>1</replys>" + "<forwards>0</forwards>"
				+ "<type>first</type>" + "<addtime>06月06日 18时01分</addtime>"
				+ "<from_string>来自新浪微博</from_string>" + "<image>"
				+ "<id>265</id>"
				+ "<small>{site_url}/images/topic/e/53/265_s.jpg</small>"
				+ "<original>aaa/images/topic/e/53/265_o.jpg</original>"
				+ "</image>" + "<attach>" + "<id>2</id>"
				+ "<attach_img>images/filetype/xls.gif</attach_img>"
				+ "<attach_name>jxwb_syds_2013060412 (5).xls</attach_name>"
				+ "<attach_score>0</attach_score>"
				+ "<attach_down>0</attach_down>"
				+ "<attach_size>大小:77.5KB</attach_size>" + "</attach>"
				+ "<video>" + "<video_hosts>sina.com.cn</video_hosts>"
				+ "<video_url>{site_url}/…/142462517543.html</video_url>"
				+ "<video_img>{site_url}/images/211529932731.jpg</video_img>"
				+ "</video>" + "<music>" + "<music_id>2</music_id>"
				+ "<music_url>1769023170</music_url>" + "</music>"
				+ "</parent>" + "</topic>" + "</root>";
		return xml;
	}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


xml怎么加入图片路径
rss源错误怎么解决
文件后缀xml是什么意思
xml格式电子发票怎么获取
xml格式是什么意思
rss是什么意思啊
xml格式电子发票怎么打开
rss订阅源是什么意思
rss源是什么
xml注释怎么写
php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类