java - JaxB to read class hierarchy -
just extending parsing class hierarchy using jaxb question. want read following xml file using jaxb
<import> <table name="user"> <row> <user_id>1</user_id> <row_version>1</row_version> <user_name>navnath</user_name> <login>navnath</login> <login_password>navnath</login_password> </row> <row> <user_id>2</user_id> <row_version>1</row_version> <user_name>kumbhar</user_name> <login>kumbhar</login> <login_password>kumbhar</login_password> </row> </table> <table name="work"> <row> <work_id>1</work_id> <work_name>work1</work_name> <row_version type="n">1</row_version> </row> <row> <work_id>2</work_id> <work_name>work2</work_name> <row_version type="n">1</row_version> </row> </table> <table> ... </table> <table> ... </table> <table> ... </table> </import>
you can see in above xml file column names different in each table. want insert data in database. try create class hierarchy this, don't know how this. row class contains diffrent xml element per table , area not able configure. please suggest.
note: i'm eclipselink jaxb (moxy) lead , member of jaxb (jsr-222) expert group.
you leverage moxy's @xmldescriminatornode
/@xmldescriminatorvalue
extension use case (see: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.html).
import
import java.util.list; import javax.xml.bind.annotation.*; @xmlrootelement(name="import") @xmlaccessortype(xmlaccesstype.field) public class import { @xmlelement(name="table") private list<table> tables; }
table
the @xmldescriminatornode
annotation used specify xml attribute used indicate subclass instantiated. jaxb implementation can't pull in subclasses of class via reflection, use @xmlseealso
annotation reference them.
import javax.xml.bind.annotation.xmlseealso; import org.eclipse.persistence.oxm.annotations.xmldiscriminatornode; @xmldiscriminatornode("@name") @xmlseealso({usertable.class, worktable.class}) public abstract class table { }
usertable
the @xmldescriminatorvalue
annotation used specify value of name
attribute corresponds particular subclass.
import java.util.list; import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.xmldiscriminatorvalue; @xmldiscriminatorvalue("user") @xmlaccessortype(xmlaccesstype.field) public class usertable extends table { @xmlelement(name="row") private list<userrow> rows; }
userrow
import javax.xml.bind.annotation.xmlelement; public class userrow { @xmlelement(name="user_id") private int userid; @xmlelement(name="user_name") private string username; }
worktable
import org.eclipse.persistence.oxm.annotations.xmldiscriminatorvalue; @xmldiscriminatorvalue("work") public class worktable extends table { }
jaxb.properties
to specify moxy jaxb provider need include file called jaxb.properties
in same package domain model following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.jaxbcontextfactory
demo
import java.io.file; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(import.class); unmarshaller unmarshaller = jc.createunmarshaller(); file xml = new file("src/forum15741264/input.xml"); import result = (import) unmarshaller.unmarshal(xml); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(result, system.out); } }
input.xml/output
below input , output running demo code.
<?xml version="1.0" encoding="utf-8"?> <import> <table name="user"> <row> <user_id>1</user_id> <user_name>navnath</user_name> </row> <row> <user_id>2</user_id> <user_name>kumbhar</user_name> </row> </table> <table name="work"/> </import>
alternative solutions
alternatively using standard jaxb apis try following approach using xmladapter
Comments
Post a Comment