8

我正在浏览 Blaise 的博客http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html以了解使用替换的 Jaxb 继承。

我想实现相同但不是根元素。我正在寻找这种类型的 XML 作为输出。

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <configuration>
      <customer>
        <address>
            <street>1 A Street</street>
        </address>
        <address>
            <street>2 B Street</street>
        </address>
        <phoneNumber>
            <mobileNo>xxx-xxx-xxxx</mobileNo>
         </phoneNumber>
     </customer>
 </configuration>

以下是 Configuration.java

    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
     public class Configuration {

    private Customer customer;

    public Customer getCustomer() {
        return customer;
    }

     public void setCustomer(Customer customer) {
        this.customer = customer;
     }

    @Override
    public String toString() {
        return "\n Customer[ customer="+customer+"]";
     }

     }

客户.java

     public class Customer {
private List<ContactInfo> contactInfo;

@XmlElementRef
public List<ContactInfo> getContactInfo() {
    return contactInfo;
}

public void setContactInfo(List<ContactInfo> contactInfo) {
    this.contactInfo = contactInfo;
}

    }

地址.java

  public class Address extends ContactInfo {
private String street;

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

}

电话号码.java

    public class PhoneNumber extends ContactInfo{
private String mobileNo;

public String getMobileNo() {
    return mobileNo;
}

public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo;
}

   }

演示.java

     import java.util.ArrayList;
     import java.util.List;

     import javax.xml.bind.JAXBContext;
     import javax.xml.bind.Marshaller;

      public class Demo {
    public static void main(String[] args) throws Exception {
    Configuration configuration = new Configuration();
    Customer customer = new Customer();
    List<ContactInfo> contacts = new ArrayList<ContactInfo>();

    Address address = new Address();
    address.setStreet("1 A Street");
    contacts.add(address);

    Address address1 = new Address();
    address1.setStreet("2 B Street");
    contacts.add(address1);

    PhoneNumber phone = new PhoneNumber();
    phone.setMobileNo("408 431 8829");
    contacts.add(phone);

    customer.setContactInfo(contacts);

    configuration.setCustomer(customer);

    JAXBContext jc = JAXBContext.newInstance(Configuration.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(configuration, System.out);
    }
 }

目前我正在关注异常

    Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:     1 counts of IllegalAnnotationExceptions
    Invalid @XmlElementRef : Type "class Address" or any of its subclasses are not known to this context.

有人可以帮我解决这个问题吗?

谢谢,夸特拉

4

1 回答 1

14

问题 #1 - 子类

JAXB (JSR-222)实现不能自动发现子类。您可以通过在类上使用@XmlSeeAlso注释ContactInfo来引用子类来解决第一个异常:

@XmlSeeAlso({Address.class, PhoneNumber.class})
public class ContactInfo {
}

或者您可以在创建JAXBContext.

 JAXBContext jc = JAXBContext.newInstance(Configuration.class, Address.class, PhoneNumber.class);

问题 #2 - 映射

使用@XmlElementRef时需要与@XmlRootElement. 如果你不想走这条路,你可以@XmlElements改用。

@XmlElements({
    @XmlElement(name="address", type=Address.class),
    @XmlElement(name="phoneNumber", type=PhoneNumber.class)
})
public List<ContactInfo> getContactInfo() {
    return contactInfo;
}
于 2013-06-21T18:37:25.733 回答