xml与javabean的相互转换

引用开源包:dom4j

jar包            :见附件

 

功能   :主要用于开发中常见的xml与javabean之间的数据互转,减少重复代码的编写,支持xml对应字段中的集合数据类型(只能为list)。但不支持 bean转换成xml时不指定字段所对应xml元素结点名称的情况,所有的字段必需指定生成xml时所对应的结点名称(即使想字段名就是xml结点属性名)。

 

 

example:

 

 要转换成bean对象的xml文件

 

 

<?xml version='1.0' encoding='UTF-8'?>
<result resultCode="0" ses="5">
	<dd>1212</dd>
	<getDiskResult>
		<nodeCount>43</nodeCount>
		<contentList length="35">
			<contentInfo>
				<contentID>xb11RJTmc01109020111028015344009</contentID>
				<contentName>1-1稻城风光.docx</contentName>
				<contentList length="35">
					<contentInfo>
						<contentID>
							xb11RJTmc01109020111028015344009
						</contentID>
						<contentName>1-2-11稻城风光.docx</contentName>
					</contentInfo>
					<contentInfo>
						<contentID>
							xb11RJTmc01109020111028015344010
						</contentID>
						<contentName>1-2-12稻城风光.docx</contentName>
					</contentInfo>
				</contentList>
			</contentInfo>
			<contentInfo>
				<contentID>xb11RJTmc01109020111028015344009</contentID>
				<contentName>2-1稻城风光.docx</contentName>
				<contentList length="35">
					<contentInfo>
						<contentID>
							xb11RJTmc01109020111028015344009
						</contentID>
						<contentName>2-2-11稻城风光.docx</contentName>
					</contentInfo>
					<contentInfo>
						<contentID>
							xb11RJTmc01109020111028015344010
						</contentID>
						<contentName>2-2-12稻城风光.docx</contentName>
					</contentInfo>
				</contentList>
			</contentInfo>
		</contentList>
	</getDiskResult>
</result>

 

java实体bean对象(一定要提供get set方法)

   Content.java

 

 

public class Content {

	@AttributeAnnotation(attribute = "resultCode")
	@ElementAnnotation(xmlElement = "result")
	private String resultCode;

	@ElementAnnotation(xmlElement = "nodeCount")
	private String nodeCount;

	@ElementAnnotation(xmlElement = "contentList")
	@CollectionAnnotation(beanClass = ContentInfo.class, childElement = "contentInfo")
	private List<ContentInfo> contentList;

	@AttributeAnnotation(attribute = "length")
	@ElementAnnotation(xmlElement = "contentList")
	private String length;

	@ElementAnnotation(xmlElement = "dd")
	private String dd;

	@AttributeAnnotation(attribute = "ses")
	@ElementAnnotation(xmlElement = "result")
	private String other;

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public String getNodeCount() {
		return nodeCount;
	}

	public void setNodeCount(String nodeCount) {
		this.nodeCount = nodeCount;
	}

	public List<ContentInfo> getContentList() {
		return contentList;
	}

	public void setContentList(List<ContentInfo> contentList) {
		this.contentList = contentList;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public String getDd() {
		return dd;
	}

	public void setDd(String dd) {
		this.dd = dd;
	}

	public String getOther() {
		return other;
	}

	public void setOther(String other) {
		this.other = other;
	}

        
	@Override
	public String toString() {
		return "dd:" + this.dd + " other:" + this.other + " nodeCount:"
				+ this.nodeCount + " resultCode:" + this.resultCode
				+ " length:" + this.length + " contentList:" + this.contentList;
	}
 }
 

  ContentInfo.java

 

public class ContentInfo {

	@ElementAnnotation(xmlElement = "contentID")
	private String id;

	@ElementAnnotation(xmlElement = "contentName")
	private String name;

	@ElementAnnotation(xmlElement = "contentList")
	@CollectionAnnotation(beanClass = ContentInfoChild.class, childElement = "contentInfo")
	private List<ContentInfo> contentList;

	@AttributeAnnotation(attribute = "length")
	@ElementAnnotation(xmlElement = "contentList")
	private String length;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<ContentInfo> getContentList() {
		return contentList;
	}

	public void setContentList(List<ContentInfo> contentList) {
		this.contentList = contentList;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "id:" + this.id + " name:" + this.name + " length:"
				+ this.length + " contentList:" + this.contentList;
	}
 }
 

ContentInfoChild.java

 

public class ContentInfoChild {

	@ElementAnnotation(xmlElement = "contentID")
	private String id;

	@ElementAnnotation(xmlElement = "contentName")
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "id:" + this.id + " name:" + this.name;
	}

}

 

测试MyTest.java类

 

public class MyTest {
	
public static void main(String[] args){
		InputStream in = MyAnalyMain.class.getClassLoader().getResourceAsStream("ce.xml");
		Content content = null;
		Long begin = System.currentTimeMillis();
		XmlToBeanAnaly analy = new XmlToBeanAnaly(); //xml转换成实现bean的
		analy.setClass(Content.class);               //设置要转换成xml的bean的class信息
		analy.initBeanFields();                      //初使化字段数据  
		
		//bean转换成xml文件
		BeanToXmlAnaly toXml = new BeanToXmlAnaly(); 
		toXml.setEncodeing("utf-8", "root");
		try {
		  toXml.setSchema("your scheama path", "public url");
		  //Content content = analy.xmlToBean(in,Content.class); //当没有调用 setClass()与initBeanFields()方法时可调用此方法
		  content = analy.xmlToBean(in);                         //将xml转换成bean
		  System.out.println(content);
		  Long end = System.currentTimeMillis();
		  System.out.println("执行耗时:"+(end - begin));
		  
		  //bean转换成xml文件
		  String xml = toXml.beanToXmlString(content);
		  System.out.println(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 

输出:

 

  System.out.println(content);
  System.out.println("执行耗时:"+(end - begin));

dd:1212 other:5 nodeCount:43 resultCode:0 length:35 contentList:[id:xb11RJTmc01109020111028015344009 name:1-1稻城风光.docx length:35 contentList:[id:xb11RJTmc01109020111028015344009 name:1-2-11稻城风光.docx, id:xb11RJTmc01109020111028015344010 name:1-2-12稻城风光.docx], id:xb11RJTmc01109020111028015344009 name:2-1稻城风光.docx length:35 contentList:[id:xb11RJTmc01109020111028015344009 name:2-2-11稻城风光.docx, id:xb11RJTmc01109020111028015344010 name:2-2-12稻城风光.docx]]
执行耗时:101

 

System.out.println(xml);
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root PUBLIC "public url " "your scheama path">
<root>
	<dd>1212</dd>
	<contentList length="35">
		<contentInfo>
			<contentList length="35">
				<contentInfo>
					<contentName>1-2-11稻城风光.docx</contentName>
					<contentID>
						xb11RJTmc01109020111028015344009
					</contentID>
				</contentInfo>
				<contentInfo>
					<contentName>1-2-12稻城风光.docx</contentName>
					<contentID>
						xb11RJTmc01109020111028015344010
					</contentID>
				</contentInfo>
			</contentList>
			<contentName>1-1稻城风光.docx</contentName>
			<contentID>xb11RJTmc01109020111028015344009</contentID>
		</contentInfo>
		<contentInfo>
			<contentList length="35">
				<contentInfo>
					<contentName>2-2-11稻城风光.docx</contentName>
					<contentID>
						xb11RJTmc01109020111028015344009
					</contentID>
				</contentInfo>
				<contentInfo>
					<contentName>2-2-12稻城风光.docx</contentName>
					<contentID>
						xb11RJTmc01109020111028015344010
					</contentID>
				</contentInfo>
			</contentList>
			<contentName>2-1稻城风光.docx</contentName>
			<contentID>xb11RJTmc01109020111028015344009</contentID>
		</contentInfo>
	</contentList>
	<nodeCount>43</nodeCount>
</root>
 

ps:附件己重新更新,用法和文章介绍差不多。只是注解里的属性名有所更改,同时修正了几处bug。同时添加了根据bean中的字段类型自动转换数据(现只支持String ,Boolean,Double,Float等常用类型),当字段与xml结点或属性名相同时默认不用指定名称。

 

源码为src

你可能感兴趣的:(xml,javabean)