如何在JAXB中解组具有相同根的两类子节点?

时间:2018-09-03 11:07:20

标签: java xml kotlin jaxb

我有2个必须与JAXB一起编组的XML。我需要从 m:properties 部分提取数据。
如何重用负责根元素表示的类并设置各种类型的嵌套xml元素?

XML1:

<feedxml:base="https://company.bpmonline.com/0/ServiceModel/EntityDataService.svc/"
xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:georss="http://www.georss.org/georss"
xmlns:gml="http://www.opengis.net/gml">
<id>https://company.bpmonline.com/0/ServiceModel/EntityDataService.svc/ContactCollection</id>
<titletype="text">ContactCollection
</title>
<updated>2018-08-29T06:26:01Z</updated>
<linkrel="self"title="ContactCollection"href="ContactCollection"/>
<entry>
    <id>https://company.bpmonline.com/0/ServiceModel/EntityDataService.svc/ContactCollection(guid'00000000000')</id>
    <categoryterm="Terrasoft.Configuration.Contact"scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <linkrel="edit"title="Contact"href="ContactCollection(guid'00000000')"/>
    <title/>
    <updated>2018-08-29T06:26:01Z</updated>
    <author>
        <name/>
    </author>
    <linkrel="http://schemas.microsoft.com/ado/2007/08/dataservices/edit-media/ContactPhoto"type="application/octet-stream"title="ContactPhoto"href="ContactCollection(guid'000000000000')/ContactPhoto"/>
    <contenttype="application/xml">
        <m:properties>
            <d:Idm:type="Edm.Guid">f00000000000000000000
            </d:Id>
            <d:Email>f.lastname@company.com</d:Email>
            <d:Surname>Lastname</d:Surname>
            <d:GivenName>Firstname</d:GivenName>
        </m:properties>
    </content>
</entry>

XML2:

<feedxml:base="https://company.bpmonline.com/0/ServiceModel/EntityDataService.svc/"
xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:georss="http://www.georss.org/georss"
xmlns:gml="http://www.opengis.net/gml">
<id>https://company.bpmonline.com/0/ServiceModel/EntityDataService.svc/CaseCollection</id>
<titletype="text">CaseCollection
</title>
<updated>2018-08-29T06:26:01Z</updated>
<linkrel="self"title="CaseCollection"href="CaseCollection"/>
<entry>
    <id>https://company.bpmonline.com/0/ServiceModel/EntityDataService.svc/CaseCollection(guid'00000000000')</id>
    <categoryterm="Terrasoft.Configuration.Contact"scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <linkrel="edit"title="Contact"href="CaseCollection(guid'00000000')"/>
    <title/>
    <updated>2018-08-29T06:26:01Z</updated>
    <author>
        <name/>
    </author>
    <linkrel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VwMobileCaseMessageHistoryCollectionByCase"type="application/atom+xml;type=feed"title="VwMobileCaseMessageHistoryCollectionByCase"href="CaseCollection(guid'0000000005')/VwMobileCaseMessageHistoryCollectionByCase"/>
    <contenttype="application/xml">
        <m:properties>
            <d:Idm:type="Edm.Guid">04040404040000400
            </d:Id>
            <d:Number>SR0000000017</d:Number>
            <d:CreatedOnm:type="Edm.DateTime">2018-04-25T17:04:37.0990136
            </d:CreatedOn>
        </m:properties>
    </content>
</entry>

这是我的类 JaxbWrapper ,它表示XML:

@XmlRootElement(name = "feed", namespace = "http://www.w3.org/2005/Atom")
@XmlAccessorType(XmlAccessType.FIELD)
class JaxbWrapper {
    @XmlElement(name = "entry", namespace = "http://www.w3.org/2005/Atom")
    var entry: Entry? = null

    @XmlRootElement(name = "entry", namespace = "http://www.w3.org/2005/Atom")
    @XmlAccessorType(XmlAccessType.FIELD)
    class Entry {
        @XmlElement(name = "content", namespace = "http://www.w3.org/2005/Atom")
        var content: Content? = null

        @XmlRootElement(name = "content", namespace = "http://www.w3.org/2005/Atom")
        @XmlAccessorType(XmlAccessType.FIELD)
        class Content {
            @XmlElement(name = "properties", namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")
            var data: ContactInfo? = null
        }
    }
}

我不想重复自己,并且对两个XML都使用相同的XmlRootElements做类。我想以某种方式分配 data 变量的类型。

ContactInfo 类。

@XmlRootElement(name = "properties", namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")
@XmlAccessorType(XmlAccessType.FIELD)
class ContactInfo {

    @XmlElement(name = "Id", namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")
    var id: String? = null

    @XmlElement(name = "Email", namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")
    var email: String? = null

    @XmlElement(name = "Surname", namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")
    var surname: String? = null

    @XmlElement(name = "GivenName", namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")
    var givenName: String? = null

}

0 个答案:

没有答案