首页>代码>java XML文件解析,超简单DEMO>/XstramTest/src/com/test/PersonTest.java
/**
 *@author: chenyoulong 
 *@Title:PersonTest.java
 *@date 2012-9-28 下午3:25:09 
 *@Description:TODO
 */
package com.test;

import java.util.ArrayList;
import java.util.List;

import com.entity.FireCompanyInfoBean;
import com.entity.FireCompanyInfoBean.FireCompanyInfo;
import com.entity.FireCompanyInfoBean.Syscode;
import com.entity.PersonBean;
import com.entity.PersonBean.Animal;
import com.entity.PersonBean.Friends;
import com.entity.PersonBean.Pets;
import com.entity.PersonBean.PhoneNumber;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 *@ClassName:PersonTest
 *@author: chenyoulong  Email: chen.youlong@payeco.com
 *@date :2012-9-28 下午3:25:09
 *@Description:TODO 
 */
public class PersonTest {

	/** 
	 * @Title: main 
	 * @Description: TODO 
	 * @param args 
	 * @return void  
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//toXml
		//		String xmlStr=new PersonTest().toXml();

		//toBean
		//		PersonBean per=new PersonTest().toBean();
		//		String xmlStr = "<person>" + "<firstName>chen</firstName>" + "<lastName>youlong</lastName>"
		//				+ "<telphone>" + "<code>137280</code>" + "<number>137280968</number>"
		//				+ "</telphone>" + "<faxphone>" + "<code>20</code>" + "<number>020221327</number>"
		//				+ "</faxphone>" + "<friends>" + "<name>A1</name>" + "<name>A2</name>"
		//				+ "<name>A3</name>" + "</friends>" + "<pets>" + "<pet>" + "<name>doly</name>"
		//				+ "<age>2</age>" + "</pet>" + "<pet>" + "<name>Ketty</name>" + "<age>2</age>"
		//				+ "</pet>" + "</pets>" + "</person>";
		//		PersonBean person = XmlUtil.toBean(xmlStr, PersonBean.class);
		//		System.out.println("person=firstname==" + person.getFirstName());
		//		System.out.println("person==Friends==name1==" + person.getFriend().getName().get(0));
		//		System.out.println("person==Pets==name2=="
		//				+ person.getPet().getAnimalList().get(1).getName());

		String biz_content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<BizContent>"
				+ "<srcID>123456</srcID>" + "<TIMESTAMP>20111101134513</TIMESTAMP>"
				+ "<operateMode>1</operateMode>" + "<FireCompanyInfoList>" + "<FireCompanyInfo>"
				+ "<orgID>123,312</orgID>" + "<companyNo>3123123123</companyNo>"
				+ "<companyName>TEST</companyName>" + "<syscodeList>" + "<syscode>"
				+ "<codetype>302</codetype>" + "<code>1001</code>" + "</syscode>" + "<syscode>"
				+ "<codetype>309</codetype>" + "<code>101</code>" + "</syscode>" + "</syscodeList>"
				+ "<fireCompanyTypeList>" + "<fireCompanyType>" + "<code>1001101</code>"
				+ "</fireCompanyType>" + "<fireCompanyType>" + "<code>1001202</code>"
				+ "</fireCompanyType>" + "</fireCompanyTypeList>" + "</FireCompanyInfo>"
				+ "</FireCompanyInfoList>" + "</BizContent>";
		FireCompanyInfoBean fireCompanyInfo = XmlUtil
				.toBean(biz_content, FireCompanyInfoBean.class);
		System.out.println("srcID==" + fireCompanyInfo.getSrcID());
		FireCompanyInfo fci = fireCompanyInfo.getFireCompanyInfoList().getFireCompanyInfo().get(0);
		Syscode syscode = fci.getSyscodeList().getSyscode().get(0);
		System.out.println(syscode.getCode());
	}

	/**
	 * 注解生产xml
	 * @Title: toXml 
	 * @Description: TODO 
	 * @return String
	 */
	public String toXml() {
		PersonBean per = new PersonBean();
		per.setFirstName("chen");
		per.setLastName("youlong");

		PhoneNumber tel = new PhoneNumber();
		tel.setCode(137280);
		tel.setNumber("137280968");

		PhoneNumber fax = new PhoneNumber();
		fax.setCode(20);
		fax.setNumber("020221327");
		per.setTel(tel);
		per.setFax(fax);

		//测试一个标签下有多个同名标签
		List<String> friendList = new ArrayList<String>();
		friendList.add("A1");
		friendList.add("A2");
		friendList.add("A3");
		Friends friend1 = new Friends();
		friend1.setName(friendList);
		per.setFriend(friend1);

		//测试一个标签下循环对象
		Animal dog = new Animal("doly", 2);
		Animal cat = new Animal("Ketty", 2);
		List<Animal> petList = new ArrayList<Animal>();
		petList.add(dog);
		petList.add(cat);
		Pets pet = new Pets();
		pet.setAnimalList(petList);
		per.setPet(pet);

		XStream xstream = new XStream(new DomDriver("utf-8")); //指定编码解析器
		xstream.processAnnotations(PersonBean.class);//如果没有这句,xml中的根元素会是<包.类名>;或者说:注解根本就没生效,所以的元素名就是类的属性
		xstream.aliasSystemAttribute(null, "class");
		String xmlString = xstream.toXML(per);
		System.out.println("xml===" + xmlString);
		return xmlString;
	}

	/**
	 * toBean
	 * @Title: toBean 
	 * @Description: TODO 
	 * @return 
	 * @return PersonBean
	 */
	public PersonBean toBean() {
		String xmlStr = "<person>" + "<firstName>chen</firstName>" + "<lastName>youlong</lastName>"
				+ "<telphone>" + "<code>137280</code>" + "<number>137280968</number>"
				+ "</telphone>" + "<faxphone>" + "<code>20</code>" + "<number>020221327</number>"
				+ "</faxphone>" + "<friends>" + "<name>A1</name>" + "<name>A2</name>"
				+ "<name>A3</name>" + "</friends>" + "<pets>" + "<pet>" + "<name>doly</name>"
				+ "<age>2</age>" + "</pet>" + "<pet>" + "<name>Ketty</name>" + "<age>2</age>"
				+ "</pet>" + "</pets>" + "</person>";

		XStream xstream = new XStream(new DomDriver()); //注意:不是new Xstream(); 否则报错:java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
		xstream.processAnnotations(PersonBean.class);
		PersonBean person = (PersonBean) xstream.fromXML(xmlStr);
		System.out.println("person=firstname==" + person.getFirstName());
		System.out.println("person==Friends==name1==" + person.getFriend().getName().get(0));
		System.out.println("person==Pets==name=="
				+ person.getPet().getAnimalList().get(1).getName());
		return person;
	}

	/**
	 * 输出xml和解析xml的工具类
	 *@ClassName:XmlUtil
	 *@author: chenyoulong  Email: chen.youlong@payeco.com
	 *@date :2012-9-29 上午9:51:28
	 *@Description:TODO
	 */
	public static class XmlUtil {
		/**
		 * java 转换成xml
		 * @Title: toXml 
		 * @Description: TODO 
		 * @param obj 对象实例
		 * @return String xml字符串
		 */
		public static String toXml(Object obj) {
			XStream xstream = new XStream();
			//			XStream xstream=new XStream(new DomDriver()); //直接用jaxp dom来解释
			//			XStream xstream=new XStream(new DomDriver("utf-8")); //指定编码解析器,直接用jaxp dom来解释

			////如果没有这句,xml中的根元素会是<包.类名>;或者说:注解根本就没生效,所以的元素名就是类的属性
			xstream.processAnnotations(obj.getClass()); //通过注解方式的,一定要有这句话
			return xstream.toXML(obj);
		}

		/**
		 *  将传入xml文本转换成Java对象
		 * @Title: toBean 
		 * @Description: TODO 
		 * @param xmlStr
		 * @param cls  xml对应的class类
		 * @return T   xml对应的class类的实例对象
		 * 
		 * 调用的方法实例:PersonBean person=XmlUtil.toBean(xmlStr, PersonBean.class);
		 */
		public static <T> T toBean(String xmlStr, Class<T> cls) {
			//注意:不是new Xstream(); 否则报错:java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(cls);
			T obj = (T) xstream.fromXML(xmlStr);
			return obj;
		}

	}

}
最近下载更多
李晓珂  LV2 2021年12月14日
长胜  LV1 2021年5月26日
503382513  LV10 2021年4月1日
jachyn  LV6 2020年9月9日
jiang12316  LV1 2020年7月19日
raccoonxx  LV2 2020年6月4日
kuafuzhuri  LV1 2020年4月24日
luohaipeng  LV23 2019年12月3日
lixq377587158  LV8 2019年11月8日
christ168  LV1 2019年10月29日
最近浏览更多
youneverknow  LV2 2月4日
wbbhappy  LV13 1月11日
林间听风  LV10 2022年8月24日
开发哈哈 2022年1月11日
暂无贡献等级
shiopaaa  LV13 2021年8月23日
长胜  LV1 2021年5月26日
zhoujunyu  LV14 2021年4月13日
503382513  LV10 2021年4月1日
tansuo阿郎  LV8 2021年2月27日
liuguojun920  LV6 2020年9月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友