当前位置:编程学习 > JAVA >>

Object转xml字符串的时候的疑问,为什么.....

我用如下方法将Object对象转为xml的字符串:

import java.io.*;
import java.util.Calendar;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
...
public static String Obj2XMLStr(Object obj){
JAXBContext context;
try {
context = JAXBContext.newInstance(obj.getClass());
Marshaller m = context.createMarshaller();
ByteArrayOutputStream os = new ByteArrayOutputStream();
m.marshal(obj,os);
return os.toString();
} catch (JAXBException e1) {
e1.printStackTrace();
return "";
}
}
...

public static void testObj2Xml(){
Address address = new Address("China", "Beijing", "Haidian","北京大学", "100871");
Person p = new Person(Calendar.getInstance(), "zhansan", address,Gender.MALE, "professor");
String str = Obj2XMLStr(p);
System.out.println(str);

}

...

public static void main(String[] args) {
testObj2Xml();
}

//其中Person对象如下:
class Person {
@XmlAttribute
String name;
@XmlElement
Calendar birthDay;
@XmlElement
Address address;
@XmlElement
Gender gender;
@XmlElement
String job;

public Address getAddress() {
return address;
}

public Person() {
}

public Person(Calendar birthDay, String name, Address address,Gender gender, String job) {
this.birthDay = birthDay;
this.name = name;
this.address = address;
this.gender = gender;
this.job = job;
}
}
enum Gender {
MALE("male"),
FEMALE("female") ;
private String name = null;
private Gender(String name){
this.name = name;
}
@Override
public String toString(){
return name;
}

}
class Address {
@XmlAttribute
String country;
@XmlElement
String state;
@XmlElement
String city;
@XmlElement
String street;
String zipcode;
public Address() {
}
public Address(String country, String state, String city, String street,String zipcode) {
this.country = country;
this.state = state;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public String getCountry() {
return country;
}

}

 
    当运行代码,xml成了:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person name="zhansan">
<birthDay>2011-11-11T16:55:54.359+08:00</birthDay>
<address country="China">
<state>Beijing</state>
<city>Haidian</city>
<street>北京大学</street>
</address>
<gender>MALE</gender>
<job>professor</job>
</person>

   而不是我想要的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<name>zhangsan</name>
<birthDay>2011-11-11T15:53:05.250+08:00</birthDay>
<address>
<country>China</country>
<state>Beijing</state>
<city>Haidian</city>
<street>北京大学</street>
</address>
<gender>MALE</gender>
<job>professor</job>
</person>

      请问这两种完全等价吗?

--------------------编程问答-------------------- 多了,应该不是等价的 --------------------编程问答-------------------- 是等价的。 --------------------编程问答-------------------- name 、 country 前的 
    @XmlAttribute
=>  @XmlElement

这样修改后结果应该是后面的 --------------------编程问答-------------------- 不太清楚!
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,