如何使用jaxb从java对象创建xml

时间:2014-03-27 10:50:10

标签: java xml jaxb

如何使用jaxb

在java中创建以下类型的xml
<myInfo>
 <firstName>MyfirstName</firstName>
 <lastName>MyLstNme</lastName>
 <contactList>
  <contact>
   <id>001</id>
   <name>name1</name>
  <contact/>
  <contact>
   <id>002</id>
   <name>name2</name>
  <contact/>
  <contact>
   <id>003</id>
   <name>name3</name>
  <contact/>
 </ContactList>
</myInfo>

Bean类是..

@XmlRootElement(name = "myInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyInfo {
    @XmlElement(name = "firstName")
    public String firstName;

    @XmlElement(name = "lastName")
    public String lastName;

    @XmlElement(name = "contactList")
    public ContactList contactList;
   ...getter setter
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "contactList")
public class ContactList {
    @XmlElement(name = "contact", type = Contact.class)
    public List<Contact> list = new ArrayList<Contact>();

    public ContactList() {
    }

    public ContactList(List<Contact> list) {
        this.list = list;
    }

   ...getter setter
}

@XmlRootElement(name = "Contact")
public class Contact {

    @XmlElement(name = "id")
    public String id;

    @XmlElement(name = "name")
    public String name;

   ...getter setter

和例外

objData ToXML 2 counts of IllegalAnnotationExceptions
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
==
Class has two properties of the same name "id"
    this problem is related to the following location:
        at public java.lang.String java8javafx8.Contact.getId()
        at java8javafx8.Contact
        at public java.util.List java8javafx8.ContactList.list
        at java8javafx8.ContactList
        at public java8javafx8.ContactList java8javafx8.MyInfo.contactList
        at java8javafx8.MyInfo
    this problem is related to the following location:
        at public java.lang.String java8javafx8.Contact.id
        at java8javafx8.Contact
        at public java.util.List java8javafx8.ContactList.list
        at java8javafx8.ContactList
        at public java8javafx8.ContactList java8javafx8.MyInfo.contactList
        at java8javafx8.MyInfo
Class has two properties of the same name "name"
    this problem is related to the following location:
        at public java.lang.String java8javafx8.Contact.getName()
        at java8javafx8.Contact
        at public java.util.List java8javafx8.ContactList.list
        at java8javafx8.ContactList
        at public java8javafx8.ContactList java8javafx8.MyInfo.contactList
        at java8javafx8.MyInfo
    this problem is related to the following location:
        at public java.lang.String java8javafx8.Contact.name
        at java8javafx8.Contact
        at public java.util.List java8javafx8.ContactList.list
        at java8javafx8.ContactList
        at public java8javafx8.ContactList java8javafx8.MyInfo.contactList
        at java8javafx8.MyInfo

如何创建Bean类和Bean列表类?

2 个答案:

答案 0 :(得分:0)

好的,听你说:

将一个类调用为MyInfo:

@XmlRootElement(name="myInfo")
public class MyInfo {

private String firstName;
private String lastName;
private List<Contact> contactList = new ArrayList<Contact>(0);


public MyInfo(){}

public MyInfo(String fName, String lName){
    this.firstName = fName;
    this.lastName = lName;
}

@XmlElement(name = "firstName")
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@XmlElement(name="lastName")
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public List<Contact> getContactList() {
    return contactList;
}

@XmlElement(name = "contactList")
public void setContactList(List<Contact> contactList) {
    this.contactList = contactList;
}
 }

写另一个名为Contact as的类:

 public class Contact {

private int id;
private String name;

public Contact(){}

public Contact(int id, String name){
    this.id = id;
    this.name = name;
}

@XmlElement(name = "id")
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

@XmlElement(name = "name")
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

使用JAXB(创建XML)将它作为Marshell:

 public class Test  {

public static void main(String[] args){
    Contact c1 = new Contact(1, "first");
    Contact c2 = new Contact(2, "second");

    MyInfo info = new MyInfo("Shekhar", "Khairnar");
    info.getContactList().add(c1);
    info.getContactList().add(c2);

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(MyInfo.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(info, System.out);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

以上例如。我只是在控制台上打印xml输出,你也可以在文件中写入。 out put将是:

<myInfo>
 <firstName>Shekhar</firstName>
<lastName>Khairnar</lastName>
<contactList>
    <id>1</id>
    <name>first</name>
</contactList>
<contactList>
    <id>2</id>
    <name>second</name>
</contactList>

答案 1 :(得分:0)

您可以执行以下操作:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class MyInfo {

   private String firstName;
   private String lastName;

   @XmlElementWrapper
   @XmlElement(name="contact")
   private List<Contact> contactList;

   // getters & setters

}

注意事项:

  • JAXB是异常配置,因此您只需要注释XML表示与默认值不同的位置。
  • 默认情况下,JAXB将公共字段和属性视为映射。如果您只想将字段视为已映射,则应指定@XmlAccessorType(XmlAccessType.FIELD)
  • 映射的字段可以是私有的。
  • @XmlElementWrapper用于将分组元素添加到集合中。
  • 在集合上使用@XmlElement时,它适用于集合中的每个项目。