用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;
	}

}


發佈了37 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章