用xstream解析XML

前端之家收集整理的这篇文章主要介绍了用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;
	}

}

猜你在找的XML相关文章