Java code
package bean
import org.dom4j.Document
import org.dom4j.DocumentHelper
import org.dom4j.Element
public class A {
private String id
private String name
private Integer age
public A(String id, String name, Integer age) {
this.id = id
this.name = name
this.age = age
}
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 Integer getAge() {
return age
}
public void setAge(Integer age) {
this.age = age
}
//输出javabean的xml格式字符串
public String getXMLString(){
Document d=DocumentHelper.createDocument()
Element root=d.addElement(this.getClass().getSimpleName())
root.addElement("id").addText(getId())
root.addElement("name").addText(getName())
root.addElement("age").addText(String.valueOf(getAge()))
d.setXMLEncoding("utf-8")
return d.asXML()
}
public static void main(String[] args) {
A a=new A("1"培闭,"张三",23)
System.out.println(a.getXMLString())
}
}
输出结果:
<?xml version="1.0" encoding="utf-8"?>
<A><id>1</id><配晌裂name>张三</name><age>23</age></A>谨纤
用Dom4j和SAX解析配置文件成java Bean对象分类: java 2011-08-05 11:13 138人阅读 评论(0) 收藏 举报
[java] view plaincopy
用Dom4j和SAX两种方式将如下配置文件解析成java Bean对象。配置文件(config.xml)如下:<pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?>
<Beans>
<Bean className="Test" classComment="测试类">
<Field name="name" type="String" comment="名字" />
<Field name="sex" type="Integer" comment="性别" />
</Bean>
<Bean className="Test1" classComment="测试类1">
<Field name="name1" type="String" comment="名字1" />
<Field name="sex1" type="Integer" comment="性别1" />
</Bean>
</Beans>租局
[java] view plaincopy
首先定义java的三个类Beans、Bean、Field 来弊山让承载配置文件的节点,类定义如下:
[java] view plaincopy
/**
* 总对象
* @author
*/
public class Beans {
private ArrayList<Bean>listBean = new ArrayList<Bean>()
public ArrayList<Bean>getListBean() {
return listBean
}
public void setListBean(ArrayList<Bean>listBean) {
this.listBean = listBean
}
}
/**
* 一个Class Bean
* @author
*
*/
public class Bean {
private String className//类名
private String classComment//类注释
private ArrayList<Field>listField = new ArrayList<Field>()
public String getClassName() {
return className
}
public void setClassName(String className) {
this.className = className
}
public String getClassComment() {
return classComment
}
public void setClassComment(String classComment) {
this.classComment = classComment
}
public ArrayList<Field>getListField() {
return listField
}
public void setListField(ArrayList<Field>listField) {
this.listField = listField
}
}
/**
* Bean 中的唯含属性对象
* @author
*
*/
public class Field {
//类型
private String type
//字段名字
private String name
//字段注释
private String comment
public String getType() {
return type
}
public void setType(String type) {
this.type = type
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public String getComment() {
return comment
}
public void setComment(String comment) {
this.comment = comment
}
}
第一种方式:通过Dom4j的方式解析xml文件:
[java] view plaincopy
[java] view plaincopy
/**
* 将xml格式的字符串转成Beans对象
* @param xml
* @return
*/
public static Beans parseXml(String xml)
{
Beans returnObj = new Beans()
Document doc = null
try{
// doc = DocumentHelper.parseText(xml) 原版转的是用的这种方式获取document对象,但是测试出错。自己换成了下面方式获取document对象就对了。
SAXReader reader = new SAXReader()
doc = reader.read(new File(xml))
Element root = doc.getRootElement()
//obj list
List list_el = root.elements()
if(list_el !=null &&list_el.size()>0)
{
for(int i =0i<list_el.size()i++)
{
Element obj_el = (Element) list_el.get(i)
//解析obj 节点
Bean obj = parseObj(obj_el)
returnObj.getListBean().add(obj)
}
}
}catch(Exception e){
e.printStackTrace()
}
return returnObj
}
/**
* 解析Bean节点
* @param obj_el
* @return
*/
private static Bean parseObj(Element obj_el)
{
Bean obj = new Bean()
obj.setClassName(obj_el.attributeValue("className"))
obj.setClassComment(obj_el.attributeValue("classComment"))
//获取field 列表
List list_field = obj_el.elements()
if(list_field != null &&list_field.size() >0)
{
for(int i = 0i<list_field.size()i++)
{
Element field_el = (Element)list_field.get(i)
//解析field节点
Field field = parseField(field_el)
obj.getListField().add(field)
}
}
return obj
}
/**
* 解析Field属性
* @param field_el
* @return
*/
private static Field parseField(Element field_el)
{
Field field = new Field()
List list_attr = field_el.attributes()
if(list_attr != null &&list_attr.size() >0)
{
for(int i=0i<list_attr.size()i++)
{
Attribute field_attr = (Attribute)list_attr.get(i)
String key = field_attr.getName()
String value = field_attr.getText()
if(key.equals("name"))
{
field.setName(value)
}
else if(key.equals("type"))
{
field.setType(value)
}
else if(key.equals("comment"))
{
field.setComment(value)
}
}
}
return field
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)