The hardware for which I am writing software is physically connected through hardware in a tree structure. The data model in our application is a tree. For our new rewriting, we use JAXB to create a data model.
We have three types of devices, and all of them have some properties, so I made an Abtract DeviceType in the XSD schema. My three devices (Pushers, Switchers, Receivers) are all expanded from DeviceType to XSD, like this:
<xs:complexType name="DeviceType" abstract="true"> <xs:sequence> <xs:element name="atrr1" type="xs:int"></xs:element> <xs:element name="attr2" type="xs:int"></xs:element> </xs:sequence> </xs:complexType> <xs:complexType name="PusherType"> <xs:complexContent> <xs:extension base="pts:DeviceType"> <xs:sequence> <xs:element name="Switcher" type="pts:SwitcherType" minOccurs="1"></xs:element> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="SwitcherType"> <xs:complexContent> <xs:extension base="pts:DeviceType"> <xs:sequence> <xs:element name="switcher" type="pts:SwitcherType" minOccurs="1"></xs:element> <xs:element name="receiver" type="pts:ReceiverType" minOccurs="1"></xs:element> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
Pushers have only children of the switch, and the switches can have both a switcher and a receiver. Receivers are the end of the line (leaf nodes). xjc creates classes. I got Unmarshaller to build an object tree, but I can't figure out how to get the getter method for getDevice (). To traverse the tree, I was hoping JAXB would provide something like "getChildren", but I don't see it in the API. If I get a switch object, I have methods for getSwitcher () and getReceiver (), but not the getDevice () method. But I try to avoid using instanceof when I do a full tree walk. The Java code that xjc builds doesn't extend to the Device class, but I just didn't find out how to get a common getter method for all devices. I just started with Jaxb two days ago and I have a ton to learn about the Jaxb API.
Yesterday was my first day playing with JAXB, I think this tool is great for our system. Our equipment is literally a tree, we have several deployments, and using XML as a site configuration file to create a state model would be ideal.
Any suggestions for JAXB newbies here?
source share