5

The system hardware I write software for is physically connect via hardware in a tree structure. The data model in our application is a Tree. For our new rewrite, we're using JAXB to create the data model.

We have three types of Devices, and they all share some properties, so I made an Abtract DeviceType in the XSD schema. My three devices (Pushers, Switchers, Receivers) are all extended from the DeviceType in the 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 switcher child elements, and switchers can have both switcher or receiver children. Receivers are the end of the line (leaf nodes). xjc builds the classes. I got the Unmarshaller to construct the object tree, but I can't figure out how to get a getter method for getDevice(). For tree traversal, I was hoping JAXB would provide something like "getChildren", but I'm not seeing in in the API. If I get a switcher object, I have the methods for getSwitcher() and getReceiver(), but no getDevice() method. But I'm trying to avoid using instanceof when I do a full tree traversal. The Java code that xjc builds, does extend from the Device class, but I just haven't learned how to get a generic 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 suits our system incredible well. Our hardware is literally a tree,we have multiple deployments, and using XML as our site config file to build a state model would be ideal.

Any suggestions for a JAXB novice here?

4

1 に答える 1

2

JAXB ( JSR-222)は、オブジェクトを XML にマッピングするための Java標準です。MetroEclipseLink MOXy (私は技術リーダーです)、Apache JaxMeなど、いくつかの実装があります 。

JAXB は、既存のオブジェクト構造を XML にマップするように設計されています。あなたの例では、XML スキーマからクラス モデルを生成する JAXB の機能を利用しています。これにより、「型付き DOM」のようなものが生成されます。各複合型に一致する (必要な継承関係を持つ) Java クラスと、各属性/要素に一致するプロパティがあります。たとえば、次のクラスは SwitcherType 複合型に対応します。

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;
    }

}

DOM とは異なり、一般的な getChildren() メソッドはありません。オブジェクト モデルを変更することでこれらを自分で実装することもできますが。

JAXB の詳細については、次を参照してください。

于 2011-01-14T17:22:20.033 に答える