Java: xml转换

时间:2024-06-26 12:35:14

java对于xml的转换有很多种,比较有名的有:DOM, DOM4J, JDOM, SAX。这里要介绍的是javax.xml包的对xml文件的转换。相比于前面几种是最简单的。

直接上代码:

Student.java:

 @XmlRootElement
 public class Student {
     private String name;
     private String width;
     private String height;
     private int age;
     public Student() {
         super();
     }
     public Student(String name, String width, String height, int age) {
         super();
         this.name = name;
         this.width = width;
         this.height = height;
         this.age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public String getWidth() {
         return width;
     }
     public void setWidth(String width) {
         this.width = width;
     }
     public String getHeight() {
         return height;
     }
     public void setHeight(String height) {
         this.height = height;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     @Override
     public String toString() {
         return "Student [name=" + name + ", width=" + width + ", height="
                 + height + ", age=" + age + "]";
     }

 }

BaseTest.java:

 public class BaseTest {
     @Test
     public void test01() throws JAXBException {
         JAXBContext jc = JAXBContext.newInstance(Student.class);
         Marshaller ms = jc.createMarshaller();
         ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
         ms.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
         Student st = new Student("zhang", "w", "h", 11);
         ms.marshal(st, new File("student.xml"));

         StringWriter writer = new StringWriter();
         ms.marshal(st, writer);
         System.out.println(writer.toString());
     }
     @Test
     public void test02() throws JAXBException {
         String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
                 + "<student><age>11</age><height>h</height><name>zhang</name><width>w</width></student>";
         JAXBContext jc = JAXBContext.newInstance(Student.class);
         Unmarshaller unmar = jc.createUnmarshaller();
         File f = new File("student.xml");
         Student st = (Student)unmar.unmarshal(f);
         System.out.println(st);
         Student st1 = (Student)unmar.unmarshal(new StringReader(xml));
         System.out.println(st1);
     }
 }

@XmlRootElement即是对该类进行反射映射为xml。

如果没有@XmlElement,默认对Property进行反射,xml node的名字与该属性名相同,如果不同,需要加上@XmlElement

如果有@XmlElement,注意该注解必须放与get方法之前。

RootClass:

 @XmlRootElement(name="rootclass")
 public class RootClass {

     private EleClassA a;

     private EleClassB b;

     private String root;

     private String rootA;
     private String transientField;

     public RootClass() {
         super();
     }
     public RootClass(String transientField) {
         //super();
         this.transientField = transientField;
     }
     private List<String> items;

     @XmlElement(name="eleClassA")
     public EleClassA getA() {
         return a;
     }
     public void setA(EleClassA a) {
         this.a = a;
     }
     @XmlElement(name="EleClassA")
     public EleClassB getB() {
         return b;
     }
     public void setB(EleClassB b) {
         this.b = b;
     }
     public String getRoot() {
         return root;
     }
     public void setRoot(String root) {
         this.root = root;
     }
     @XmlTransient
     public String getRootA() {
         return rootA;
     }
     public void setRootA(String rootA) {
         this.rootA = rootA;
     }
     @XmlElementWrapper(name="items")
     @XmlElement(name="item")
     public List<String> getItems() {
         return items;
     }
     public void setItems(List<String> items) {
         this.items = items;
     }
 }

EleClassA:

 public class EleClassA {
     private String eleA;
     private String eleB;
     private String attrC;
     @XmlElement
     public String getEleA() {
         return eleA;
     }
     public void setEleA(String eleA) {
         this.eleA = eleA;
     }
     @XmlElement(name="elebnewname")
     public String getEleB() {
         return eleB;
     }
     public void setEleB(String eleB) {
         this.eleB = eleB;
     }
     @XmlAttribute
     public String getAttrC() {
         return attrC;
     }
     public void setAttrC(String attrC) {
         this.attrC = attrC;
     }

 }

EleClassB:

 public class EleClassB {
     private String attrUserName;
     private String attrPassword;
     private String eleCode;
     @XmlAttribute
     public String getAttrUserName() {
         return attrUserName;
     }
     public void setAttrUserName(String attrUserName) {
         this.attrUserName = attrUserName;
     }
     @XmlAttribute(name="password")
     public String getAttrPassword() {
         return attrPassword;
     }
     public void setAttrPassword(String attrPassword) {
         this.attrPassword = attrPassword;
     }
     @XmlElement
     public String getEleCode() {
         return eleCode;
     }
     public void setEleCode(String eleCode) {
         this.eleCode = eleCode;
     }
 }

AttributeTest:

 public class AttributeTest {

     @Test
     public void test01() throws JAXBException {
         RootClass rc = new RootClass("transient");
         //RootClass rc = new RootClass();
         EleClassA a = new EleClassA();
         EleClassB b = new EleClassB();

         a.setAttrC("attrC");
         a.setEleA("eleA");
         a.setEleB("eleB");

         b.setAttrPassword("attrPassword");
         b.setAttrUserName("attrUsrName");
         b.setEleCode("eleCode");

         List<String> l = new ArrayList<String>();
         l.add("yingzhongwen");
         l.add("hetao");
         l.add("xinwang");

         rc.setA(a);
         rc.setB(b);
         rc.setRoot("root");
         rc.setRootA("rootA");
         rc.setItems(l);

         JAXBContext context = JAXBContext.newInstance(RootClass.class);
         Marshaller mar = context.createMarshaller();
         mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
         mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

         StringWriter writer = new StringWriter();
         mar.marshal(rc, writer);
         System.out.println(writer.toString());
     }
 }