Convert Java object to XML -


i trying convert java object inside of java library xml file. however, got problem:

a = new a();  // initializing  jaxbcontext jc = jaxbcontext.newinstance("librarya.a");  marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(a, system.out); 

then got exception:

javax.xml.bind.jaxbexception: "librarya.a" doesnt contain objectfactory.class or jaxb.index     @ com.sun.xml.internal.bind.v2.contextfactory.createcontext(contextfactory.java:186)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)     @ java.lang.reflect.method.invoke(method.java:597)     @ javax.xml.bind.contextfinder.newinstance(contextfinder.java:128)     @ javax.xml.bind.contextfinder.find(contextfinder.java:290)     @ javax.xml.bind.jaxbcontext.newinstance(jaxbcontext.java:372)     @ javax.xml.bind.jaxbcontext.newinstance(jaxbcontext.java:337)     @ javax.xml.bind.jaxbcontext.newinstance(jaxbcontext.java:244) 

if change: jaxbcontext jc = jaxbcontext.newinstance("librarya.a");

to:

jaxbcontext jc = jaxbcontext.newinstance(librarya.a.class); 

then have exception:

com.sun.xml.internal.bind.v2.runtime.illegalannotationsexception: 2 counts of illegalannotationexceptions  library.a interface, , jaxb can't handle interfaces.     problem related following location:         @ library.a  library.a not have no-arg default constructor.     problem related   following location:     @ library.a 

could please me?

thanks in advance!

background info (from related question)

from comment made on answer previous question domain model being used jaxb. easiest way have client , server communicate via xml leverage annotated model on both ends.

i have checked source code of client. in process, need convert xml file generated java objects xml file using: javax.xml.bind.jaxbcontext & javax.xml.bind.marshaller. question possible read xml file same java objects? can use java objects further step. in advance!


update

it appears though issue due having domain model defined through interfaces backing implementation classes. below i'll demonstrate how can handle using jaxb implementation (metro, moxy, jaxme, etc).

demo code

import java.io.file;  import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; import javax.xml.bind.unmarshaller;  public class demo {      public static void main(string[] args) throws exception {         jaxbcontext jc = jaxbcontext.newinstance(customerimpl.class);          unmarshaller unmarshaller = jc.createunmarshaller();         file xml = new file("input.xml");         customer customer = (customer) unmarshaller.unmarshal(xml);          address address = customer.getaddress();         system.out.println(address.getstreet());          marshaller marshaller = jc.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.marshal(customer, system.out);     }  } 

interface model

the following interfaces represent our domain model. these interfaces when not leveraged bootstrap jaxbcontext.

customer

public interface customer {      public address getaddress();      public void setaddress(address address);  } 

address

public interface address {      public string getstreet();      public void setstreet(string street);  } 

implementation classes

the implementation classes mapped xml using jaxb.

customerimpl

note in customerimpl class use @xmlelement annotation on address property specify type addressimpl.

import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement;  @xmlrootelement(name="customer") public class customerimpl implements customer {      private address address;      @xmlelement(type=addressimpl.class)     public address getaddress() {         return address;     }      public void setaddress(address address) {         this.address = address;     }  } 

addressimpl

public class addressimpl implements address {      private string street;      public string getstreet() {         return street;     }      public void setstreet(string street) {         this.street = street;     }  } 

input.xml

<?xml version="1.0" encoding="utf-8"?> <customer>     <address>         <street>1 street</street>     </address> </customer> 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -