java - Marshalling and Unmarshalling changes xml using moxy -


i trying create this kind(xsd inside) of documents. examples here. because of constant values in root-element , other constant elements generated template eclipse:

<?xml version="1.0" encoding="utf-8"?> <invoice:response xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns="http://www.forum-datenaustausch.ch/invoice" xsi:schemalocation="http://www.forum-datenaustausch.ch/invoice generalinvoiceresponse_440.xsd" language="de">   <invoice:processing>     <invoice:transport from="" to="">       <invoice:via sequence_id="0" via=""/>     </invoice:transport>   </invoice:processing>   <invoice:payload response_timestamp="0">     <invoice:invoice request_date="2001-12-31t12:00:00" request_id="" request_timestamp="0"/>   </invoice:payload> </invoice:response> 

but simple unmarshalling , marshalling changes content:

<?xml version="1.0" encoding="utf-8"?> <response xmlns="http://www.forum-datenaustausch.ch/invoice" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" language="de">    <processing>       <transport from="" to="">          <via via="" sequence_id="0"/>       </transport>    </processing>    <payload response_timestamp="0">       <invoice request_timestamp="0" request_date="2001-12-31t12:00:00.0" request_id=""/>    </payload> </response> 

for reason schema location attribute gone. added manually before marshalling. 2nd problem is, prefixes gone. don't know consumes produced xml (do unmarshal handwritten code? or without validation?). because of want output similar given examples , valid. there either way leave existing elements , attributes untouched , let moxy add namespace prefixes each element?

the following should help. question being handled on eclipselink forum:


for reason schema location attribute gone.

you can specify following property on marshaller output schema location:

marshaller.setproperty(marshaller.jaxb_schema_location, "http://www.forum-datenaustausch.ch/invoice generalinvoiceresponse_440.xsd"); 

2nd problem is, prefixes gone.

the namespace prefixes gone, namespace qualification same (all elements have same local name , namespace uri). in first document invoice prefix assigned http://www.forum-datenaustausch.ch/invoice namespace, , in second document namespace assigned default namespace


controlling namespace prefixes @ design time

you can provide moxy hints @ namespace prefixes should used leveraging @xmlschema annotation (see: http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html).

package-info

@xmlschema(     elementformdefault=xmlnsform.qualified,     namespace="http://www.forum-datenaustausch.ch/invoice",     xmlns={         @xmlns(prefix="invoice", namespaceuri="http://www.forum-datenaustausch.ch/invoice"),         @xmlns(prefix="ds", namespaceuri="http://www.w3.org/2000/09/xmldsig#"),         @xmlns(prefix="xenc", namespaceuri="http://www.w3.org/2001/04/xmlenc#")     } ) package forum16559889;  import javax.xml.bind.annotation.*; 

response

package forum16559889;  import javax.xml.bind.annotation.xmlrootelement;  @xmlrootelement public class response { } 

demo

package forum16559889;  import javax.xml.bind.*;  public class demo {      public static void main(string[] args) throws exception {         jaxbcontext jc = jaxbcontext.newinstance(response.class);          response response = new response();          marshaller marshaller = jc.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.setproperty(marshaller.jaxb_schema_location, "http://www.forum-datenaustausch.ch/invoice generalinvoiceresponse_440.xsd");         marshaller.marshal(response, system.out);     }  } 

output

<?xml version="1.0" encoding="utf-8"?> <invoice:response xsi:schemalocation="http://www.forum-datenaustausch.ch/invoice generalinvoiceresponse_440.xsd" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"/> 

controlling namespace prefixes @ runtime

you cam leverage moxy's namespaceprefixmapper extension control namespace prefixes used @ runtime.

package forum16559889;  import javax.xml.bind.*; import org.eclipse.persistence.jaxb.marshallerproperties; import org.eclipse.persistence.oxm.namespaceprefixmapper;  public class demo {      public static void main(string[] args) throws exception {         jaxbcontext jc = jaxbcontext.newinstance(response.class);          response response = new response();          marshaller marshaller = jc.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.setproperty(marshaller.jaxb_schema_location, "http://www.forum-datenaustausch.ch/invoice generalinvoiceresponse_440.xsd");          marshaller.setproperty(marshallerproperties.namespace_prefix_mapper, new namespaceprefixmapper() {              @override             public string getpreferredprefix(string namespaceuri,                     string suggestion, boolean requireprefix) {                 if("http://www.forum-datenaustausch.ch/invoice".equals(namespaceuri)) {                     return "invoice";                 } else if("http://www.w3.org/2000/09/xmldsig#".equals(namespaceuri)) {                     return "ds";                 } else if("http://www.w3.org/2001/04/xmlenc#".equals(namespaceuri)) {                     return "xenc";                 } else {                     return null;                 }             }          });          marshaller.marshal(response, system.out);     }  } 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -