Convert XML attributes into Java fields

LacyABV

New Member
I have a chunk of XML that looks like this:\[code\]<transaction name="transaction_A"> <request> <list name="listOfArgs"> <arg name="start" type="time"/> <arg name="stop" type="time"/> <arg name="user" type="user"/> </list> <arg name="are-new" type="boolean"/> <arg name="number-of-references" type="positive small number"/> </request> <response> <arg name="would-cause-conflicts" type="boolean"/> <arg name="number-of-conflicts" type="positive number"/> </response></transaction><transaction name="transaction_B"> <response> <list name="availability"> <arg name="start-hour" type="positive small number"/> <arg name="stop-hour" type="positive small number"/> <arg name="is-override" type="boolean"/> <arg name="is-allocated" type="boolean"/> </list> </response></transaction><transaction name="transaction_C"> <request> <arg name="reference-type"/> <arg name="reference-number"/> </request></transaction>\[/code\]I can convert that to an XSD...\[code\]<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="all"> <xs:complexType> <xs:sequence> <xs:element name="transaction" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="request" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="list" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="arg" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="name" use="optional"/> <xs:attribute type="xs:string" name="type" use="optional"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute type="xs:string" name="name"/> </xs:complexType> </xs:element> <xs:element name="arg" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="name" use="optional"/> <xs:attribute type="xs:string" name="type" use="optional"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="response" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="arg" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="name" use="optional"/> <xs:attribute type="xs:string" name="type" use="optional"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="list" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="arg" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="name" use="optional"/> <xs:attribute type="xs:string" name="type" use="optional"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute type="xs:string" name="name"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute type="xs:string" name="name" use="optional"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>\[/code\]but it's not very useful since the "arg" element is so generic. My ultimate goal is to create Java objects to represent each unique request and response element. I need to be able to differentiate between a request for \[code\]transaction_A\[/code\] vs \[code\]transaction_B\[/code\].My thought was to use XSLT to transform the attributes into child elements, then create a new XSD from that new XML. From that XSD I could create my Java objects with JiBX or some other tool. Something about that, however, doesn't seem like the best solution.Question:What is the best way to convert the XML above into useful Java objects, using the attribute values in the XML as field names (or even a class name in the case of the transaction element).
 
Back
Top