jboss - mapping structurally differnt xml's to common java object -


i have situation have 2 structurally different xml's , need extract data these (at time xml needs parsed) , submit logic , fill common format.

<xml1>     <a>hello</a>     <b>shreyas</b>     <c>123</c> </xml1> 

and

<xml2>     <d>         <name>             <my>hello</my>         </name>     </d>     <e>shreyas</e>     <f>124</f> </xml2> 

and common object fill

public class gobj {      string fname;     string lname;     string id; } 

i looking kind of run time annotations resolver. tried jaxbintros implemented jboss on top of jaxb , not find xpath support it.

also problem can solved using basic sax parser, wanted method jaxb etc.,. readability of code can easy.

-thanks shreyas

note: i'm eclipselink jaxb (moxy) lead , member of jaxb (jsr-222) expert group.

below how support use case using moxy's external mapping file extension.

metadata xml 1

we use standard jaxb annotations map gobj class first xml representation.

package forum17652921;  import javax.xml.bind.annotation.*;  @xmlrootelement(name="xml1") @xmlaccessortype(xmlaccesstype.field) public class gobj {      @xmlelement(name="a")     string fname;      @xmlelement(name="b")     string lname;      @xmlelement(name="c")     string id;  } 

metadata xml 2

we use moxy's external mapping document map same class second xml representation. default mapping document used augment metadata supplied annotations. if set xml-metadata-complete flag true can replace metadata.

<?xml version="1.0"?> <xml-bindings     xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"     package-name="forum17652921"     xml-accessor-type="field"     xml-mapping-metadata-complete="true">     <java-types>         <java-type name="gobj">             <xml-root-element name="xml2"/>             <java-attributes>                 <xml-element java-attribute="fname" xml-path="d/name/my/text()"/>                 <xml-element java-attribute="lname" name="e"/>                 <xml-element java-attribute="id" name="f"/>             </java-attributes>         </java-type>     </java-types> </xml-bindings> 

demo

in demo code below there 2 instances of jaxbcontext. use first read xml representation 1 instance of gobj. use second jaxbcontext marshal same instance of gobj second xml representation.

package forum17652921;  import java.io.file; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.jaxbcontextproperties;  public class demo {      public static void main(string[] args) throws exception {         jaxbcontext jc1 = jaxbcontext.newinstance(gobj.class);          unmarshaller unmarshaller = jc1.createunmarshaller();         file xml = new file("src/forum17652921/xml1.xml");         gobj gobj = (gobj) unmarshaller.unmarshal(xml);          map<string, object> properties = new hashmap<string, object>(1);         properties.put(jaxbcontextproperties.oxm_metadata_source, "forum17652921/oxm.xml");         jaxbcontext jc2 = jaxbcontext.newinstance(new class[] {gobj.class}, properties);          marshaller marshaller = jc2.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.marshal(gobj, system.out);     }  } 

xml1.xml

<xml1>     <a>hello</a>     <b>shreyas</b>     <c>123</c> </xml1> 

output

<?xml version="1.0" encoding="utf-8"?> <xml2>    <d>       <name>          <my>hello</my>       </name>    </d>    <e>shreyas</e>    <f>123</f> </xml2> 

for more information


Comments