Sunday 15 May 2011

java - Jaxb Inheritance using Substitution but not to root Element -


I was using replacement for Jacob Inheritance through Blaze's blog.

I want to implement it but not on the root element. I am seeing this type of XML as an output.

  & lt ;? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"? & Gt; & Lt; Configuration & gt; & Lt; Customers & gt; & Lt; Address & gt; & Lt; Road & gt; 1 one street & lt; / Road & gt; & Lt; / Address & gt; & Lt; Address & gt; & Lt; Road & gt; 2b Street & lt; / Road & gt; & Lt; / Address & gt; & Lt; PhoneNumber & gt; & Lt; MobileNo & gt; Xxx-xxx-xxxx & lt; / MobileNo & gt; & Lt; / PhoneNumber & gt; & Lt; / Customer & gt; & Lt; / Configuration & gt;   

The following configuration is. Java

  import javax.xml.bind.annotation.xmlRootElement; @ XmlRootElement public class configuration {Private customer customer; Public customer getCustomer () {return customer; } Public Zero Set Customers (Customer Customers) {this.customer = Customer; } @ Override public string toastring () {return "\ nCustomer [customer =" + customer + "]"; }}   

Customer. Java

  Public class customer {Private list & lt; ContactInfo & gt; contact info; @ XmlElementRef public list & lt; ContactInfo & gt; GetContactInfo () {return contact information; } Public Zero SatCactInfo (list & lt; ContactInfo & gt; contact information) {this.contactInfo = contactInfo; }}   

address. Java

  public category address ContactInfo (private string road; public string getStreet () {return road;} Public Zero Setstreet ( String Street} {this.street = street}}   

PhoneNumber.java

  public class phone number ContactInfo [Private String mobileNo; public string getMobileNo ( ) {Return mobile no;} public zero setmobile no (string mobile no) {this.mobileNo = mobileNo;}}   

demo.java

  import Please java.util.ArrayList; java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class Demo {public static void main (String [] Arges) throws Exception {Configuration configuration = new Configuration ()); Customer customer = new customer (); & Lt; ContactInfo & gt; Contact = New ArrayList & lt; ContactInfo & gt; (); Address address = new address (); Address.setStreet ("1a Street"); Contacts.add (address); Address1 address = new address (); Address1.setStreet ("2b Street"); Contacts.add (address1); Phononumber Phone = New phone number (); Phone.setMobileNo ("408 431 8829"); Contacts.add (phone); Customer.setContactInfo (contact); Configuration.setCustomer (customer); JAXBContext Jesse = JAXBContext.newInstance (Configuration class); Marshall Marshaller = J.C. Caye Marshaller (); Marshaller.setProperty (Marshaler. JAXB_FORMATTED_OUTPUT, true); Marshaller.marshal (Configuration, System.out); }}   

At this time I am following the exception

  exception in the thread "main" com.sun.xml.internal.bind.v2. Runtime.IllegalNnotationsException: 1 Invalid Annotation Number of Exceptions Invalid @ XmlElementRef: Type "class address" or any of its sub-categories are not known in this context  

Problem # 1 - subclass

An implementation can not search the subclasses themselves To refer to the code, click on the ContactInfo class on a @ You can resolve the first exception by using XmlSeeAlso annotation:

  @ XmlSeeAlso ({address) When you create  JAXBContext , you can reference them can give.  
   Code> JAXBContext jc = JAXBContext.newInstance (Configuration Category, Address Square, PhoneNumber.class);   

Issue # 2 - Mapping

When you use @XmlElementRef You have to add it with @ XmlRootElement . If you do not want to go this route, you can use @XmlElements instead. @XmlElements ({@XmlElement (name = "address", type = Address .class), @ XMLElement (name = "phone number", type = PhoneNumber.class)}) Public listing & Lt; ContactInfo & gt; GetContactInfo () {return contact information; }



No comments:

Post a Comment