Using an abstract base class type to move an entire JAXB object tree

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?

+4
source share
1 answer

JAXB ( JSR-222) is the Java standard for mapping objects to XML. There are several implementations, including: Metro , EclipseLink MOXy (I 'tech Tech), Apache JaxMe , etc.

JAXB is designed to map existing object structures to XML. In your example, you are using JAXB's ability to generate a class model from an XML schema. This creates something like a "typed DOM". There will be a Java class corresponding to each complex type (with the necessary inheritance relationships) and a property corresponding to each attribute / element. For example, the following class corresponds to the complex SwitcherType type:

 import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "SwitcherType", propOrder = { "switcher", "receiver" }) public class SwitcherType extends DeviceType { @XmlElement(required = true) protected SwitcherType switcher; @XmlElement(required = true) protected ReceiverType receiver; public SwitcherType getSwitcher() { return switcher; } public void setSwitcher(SwitcherType value) { this.switcher = value; } public ReceiverType getReceiver() { return receiver; } public void setReceiver(ReceiverType value) { this.receiver = value; } } 

Unlike the DOM, there are no generic getChildren () methods. Although you can implement them yourself by changing the object model.

More on JAXB:

+1
source

Source: https://habr.com/ru/post/1335570/


All Articles