简体   繁体   English

JaxB:如何从嵌套元素中检索文本属性?

[英]JaxB: How Do I Retrieve Text Attribute from Nested Element?

I want the Country class to store the "ISO_3166-1_Alpha-2_Code" code. 我希望Country类存储“ ISO_3166-1_Alpha-2_Code”代码。 The code currently gets back the "ISO_3166-1_Numeric-3_Code" code. 该代码当前取回“ ISO_3166-1_Numeric-3_Code”代码。 Can't figure out how to tweak the Country class to get the specific attribute I want. 无法弄清楚如何调整Country类以获得我想要的特定属性。

XML: XML:

<?xml version='1.0' encoding='UTF-8'?>
<wd:Message_Event_Configuration">
    <wd:Message_Event_Configuration_Data>
        <wd:Country_Reference wd:Descriptor="Saint Martin">
            <wd:ID wd:type="WID">66b7082a21e510000961bb6d82b5002a</wd:ID>
            <wd:ID wd:type="ISO_3166-1_Alpha-2_Code">MF</wd:ID>
            <wd:ID wd:type="ISO_3166-1_Alpha-3_Code">MAF</wd:ID>
            <wd:ID wd:type="ISO_3166-1_Numeric-3_Code">663</wd:ID>
        </wd:Country_Reference>
        <wd:Country_Reference wd:Descriptor="Saint Barthelemy">
            <wd:ID wd:type="WID">881527f6cec910000ba81e8dccf61127</wd:ID>
            <wd:ID wd:type="ISO_3166-1_Alpha-2_Code">BL</wd:ID>
            <wd:ID wd:type="ISO_3166-1_Alpha-3_Code">BLM</wd:ID>
            <wd:ID wd:type="ISO_3166-1_Numeric-3_Code">652</wd:ID>
        </wd:Country_Reference>
    </wd:Message_Event_Configuration_Data>
</wd:Message_Event_Configuration>

Country List: 国家列表:

@XmlRootElement(name = "Message_Event_Configuration")
@XmlAccessorType(XmlAccessType.FIELD)
public class Countries {

    @XmlElementWrapper(name = "Message_Event_Configuration_Data")
    @XmlElement(name = "Country_Reference")

    private List<Country> countries = new ArrayList<Country>();

    public List<Country> getCountries() {
        return countries;
    }

    public void setCountries(List<Country> countries) {
        this.countries = countries;
    }

}

Country: 国家:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Country_Reference")
public class Country {

    @XmlElement(name = "ID")
    private String isoCode;

    public Country() {
    }

    public Country(String isoCode) {
        this.isoCode = isoCode;
    }

    @XmlAttribute(name = "ISO_3166-1_Alpha-2_Code")
    public String getISOCode() {
        return isoCode;
    }

    public void setISOCode(String isoCode) {
        this.isoCode = isoCode;
    }
}

The <Country_Reference> XML element contains the ISO codes in a rather sophisticated way within several <wd:ID> XML elements. <Country_Reference> XML元素以相当复杂的方式在多个<wd:ID> XML元素内包含ISO代码。 It is therefore much too simple to model them as a Java String property. 因此,将它们建模为Java String属性太简单了。

Instead, you need to model the Java-structure with more similarity to the XML-structure. 相反,您需要对Java结构进行建模,使其与XML结构更加相似。

The sequence of XML elements <wd:ID> can be modeled by a property List<ID> idList which needs to be annotated by @XmlElement(name="ID") . XML元素<wd:ID>的序列可以由属性List<ID> idList建模,该属性需要由@XmlElement(name="ID")进行注释。

The XML attribute wd:Descriptor="...." can be modeled by a property String descriptor which needs to be annotated by @XmlAttribute(name="Descriptor") . XML属性wd:Descriptor="...."可以由属性String descriptor建模,该属性需要由@XmlAttribute(name="Descriptor")进行注释。

For your convenience you can add an all-arguments-constructor and some methods for getting the WID and ISO codes from the List<ID> . 为了您的方便,您可以添加一个all-arguments-constructor以及一些从List<ID>获取WID和ISO代码的方法。

@XmlAccessorType(XmlAccessType.FIELD)
public class Country {

    @XmlAttribute(name = "Descriptor")
    private String descriptor;

    @XmlElement(name = "ID")
    private List<ID> idList;

    public Country() {
    }

    public Country(String descriptor, String wid, String isoAlpha2Code, String isoAlpha3Code, String isoNumeric3Code) {
        this.descriptor = descriptor;
        idList = new ArrayList<>();
        idList.add(new ID("WID", wid));
        idList.add(new ID("ISO_3166-1_Alpha-2_Code", isoAlpha2Code));
        idList.add(new ID("ISO_3166-1_Alpha-3_Code", isoAlpha3Code));
        idList.add(new ID("ISO_3166-1_Numeric-3_Code", isoNumeric3Code));
    }

    public String getWid() {
        return getIdByType("WID");
    }

    public String getIsoAlpha2Code() {
        return getIdByType("ISO_3166-1_Alpha-2_Code");
    }

    public String getIsoAlpha3Code() {
        return getIdByType("ISO_3166-1_Alpha-3_Code");
    }

    public String getIsoNumeric3Code() {
        return getIdByType("ISO_3166-1_Numeric-3_Code");
    }

    private String getIdByType(String idType) {
        for (ID id : idList) {
        if (id.getType().equals(idType))
            return id.getValue();
        }
        return null;
    }
}

The XML elements <wd:ID> are quite complex. XML元素<wd:ID>非常复杂。 Therefore we need a separate POJO class for modeling them. 因此,我们需要一个单独的POJO类来对其进行建模。 Let's call the class ID . 让我们称为类ID

The XML text between <wd:ID ..> and </wd:ID> is modeled by the property String value which needs to be annotated by @XmlValue . <wd:ID ..></wd:ID>之间的XML文本由属性String value建模,该属性需要由@XmlValue注释。

The XML attribute wd:type="..." is modeled by the property String type which needs to be annotated by @XmlAttribute . XML属性wd:type="..."由属性String type建模,需要使用@XmlAttribute进行注释。

For convenient use by the class Country above, an all-arguments-constructor is added. 为了方便上面的Country类使用,添加了一个all-arguments-constructor。

@XmlAccessorType(XmlAccessType.FIELD)
public class ID {

    @XmlAttribute
    private String type;

    @XmlValue
    private String value;

    public ID() {
    }

    public ID(String type, String value) {
        this.type = type;
        this.value = value;
    }

    // public getters and setters (omitted here fro brevity)
}

The screenshot below (taken from within the debugger) visualizes the Java structure and confirms that the unmarshalling of your XML example works correctly: 下面的屏幕截图(从调试器内部获取)可视化了Java结构并确认XML示例的解组工作正常:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM