java与Xml的转换(Castor)
Castor用法
http://backhome.iteye.com/blog/582459
http://fyd222.iteye.com/blog/1397761
http://kuwoleft.iteye.com/blog/1242249
依赖包:
castor-1.3.2-xml.jar
castor-1.3.2-core.jar
commons-lang-2.1.0.jar
commons-logging-1.1.jar
javax.xml.stream-1.0.1.jar
JavaBean
Person.java
Java代码
package bean;
import java.io.Serializable;
import java.util.Date;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name = null;
private Date dob = null;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
@Override
public String toString() {
return "Person [dob=" + dob + ", name=" + name + "]";
}
}
package bean;
import java.io.Serializable;
import java.util.Date;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name = null;
private Date dob = null;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
@Override
public String toString() {
return "Person [dob=" + dob + ", name=" + name + "]";
}
}
Staff.java
Java代码
package bean;
import java.io.Serializable;
import java.util.List;
public class Staff implements Serializable{
private static final long serialVersionUID = 1L;
private String deptNO;
private List<Person> persons;
public String getDeptNO() {
return deptNO;
}
public void setDeptNO(String deptNO) {
this.deptNO = deptNO;
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
@Override
public String toString() {
return "Staff [deptNO=" + deptNO + ", persons=" + persons + "]";
}
}
package bean;
import java.io.Serializable;
import java.util.List;
public class Staff implements Serializable{
private static final long serialVersionUID = 1L;
private String deptNO;
private List<Person> persons;
public String getDeptNO() {
return deptNO;
}
public void setDeptNO(String deptNO) {
this.deptNO = deptNO;
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
@Override
public String toString() {
return "Staff [deptNO=" + deptNO + ", persons=" + persons + "]";
}
}
XML映射文件
maping.xml
Xml代码
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<description>Basic mapping example</description>
<class name="bean.Staff">
<map-to xml="Staff" />
<field name="deptNO">
<bind-xml name="deptNO" node="attribute" />
</field>
<field name="persons" type="bean.Person" collection="arraylist">
<bind-xml name="Person" />
</field>
</class>
<class name="bean.Person">
<field name="name">
<bind-xml name="name" node="attribute" />
</field>
<field name="dob">
<bind-xml name="birthday" node="attribute" />
</field>
</class>
</mapping>
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<description>Basic mapping example</description>
<class name="bean.Staff">
<map-to xml="Staff" />
<field name="deptNO">
<bind-xml name="deptNO" node="attribute" />
</field>
<field name="persons" type="bean.Person" collection="arraylist">
<bind-xml name="Person" />
</field>
</class>
<class name="bean.Person">
<field name="name">
<bind-xml name="name" node="attribute" />
</field>
<field name="dob">
<bind-xml name="birthday" node="attribute" />
</field>
</class>
</mapping>
JavaBeanToXml.java
Java代码
package Trans;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.XMLContext;
import bean.Person;
import bean.Staff;
public class JavaBeanToXml {
public static void main(String[] args) {
Staff staff = new Staff();
staff.setDeptNO("MNN1");
Person p1 =new Person();
p1.setName("田中一郎");
p1.setDob(new Date());
Person p2 =new Person();
p2.setName("田中 二郎");
p2.setDob(new Date());
ArrayList<Person> pers =new ArrayList<Person>();
pers.add(p1);
pers.add(p2);
staff.setPersons(pers);
javaBeanToXml("D:\\fsdev\\workspace\\JavaBeanToXML\\src\\Trans\\maping.xml","C:\\Staff.xml",staff);
}
private static void javaBeanToXml(String mapingFile, String destFile, Staff staff) {
Mapping mapping = new Mapping();
try {
mapping.loadMapping(mapingFile);
XMLContext context = new XMLContext();
context.addMapping(mapping);
Marshaller marshaller = context.createMarshaller();
marshaller.setWriter(new OutputStreamWriter(new FileOutputStream(new File(destFile))));
marshaller.marshal(staff);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package Trans;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.XMLContext;
import bean.Person;
import bean.Staff;
public class JavaBeanToXml {
public static void main(String[] args) {
Staff staff = new Staff();
staff.setDeptNO("MNN1");
Person p1 =new Person();
p1.setName("田中一郎");
p1.setDob(new Date());
Person p2 =new Person();
p2.setName("田中 二郎");
p2.setDob(new Date());
ArrayList<Person> pers =new ArrayList<Person>();
pers.add(p1);
pers.add(p2);
staff.setPersons(pers);
javaBeanToXml("D:\\fsdev\\workspace\\JavaBeanToXML\\src\\Trans\\maping.xml","C:\\Staff.xml",staff);
}
private static void javaBeanToXml(String mapingFile, String destFile, Staff staff) {
Mapping mapping = new Mapping();
try {
mapping.loadMapping(mapingFile);
XMLContext context = new XMLContext();
context.addMapping(mapping);
Marshaller marshaller = context.createMarshaller();
marshaller.setWriter(new OutputStreamWriter(new FileOutputStream(new File(destFile))));
marshaller.marshal(staff);
} catch (Exception e) {
e.printStackTrace();
}
}
}
XMLJavaToBean.java
package Trans;
Java代码
import java.io.FileReader;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.XMLContext;
import bean.Staff;
public class XMLJavaToBean {
public static void main(String[] args) {
Staff staff = xmlToJavaBean("D:\\fsdev\\workspace\\JavaBeanToXML\\src\\Trans\\maping.xml","C:\\Staff.xml");
System.out.println(staff);
}
private static Staff xmlToJavaBean(String mapingFile, String xmlFile) {
Mapping mapping = new Mapping();
try {
mapping.loadMapping(mapingFile);
XMLContext context = new XMLContext();
context.addMapping(mapping);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setClass(Staff.class);
FileReader reader = new FileReader(xmlFile);
return (Staff)unmarshaller.unmarshal(reader);
} catch (Exception e) {
e.printStackTrace();
}
return new Staff();
}
}