如何根据限定符选择要绑定的特定子类型 [英] How to choose a specific subtype to bind to based on qualifiers

查看:69
本文介绍了如何根据限定符选择要绑定的特定子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的XML(ish)

Here is the XML I have (ish)

<?xml version="1.0" encoding="UTF-8"?>
<edi837>
   <ISA >
      <ISA01>00</ISA01>
      <ISA02>          </ISA02>
      <GS>
         <GS01>HC</GS01>
         <GS07>X</GS07>
         <GS08>005010X223A2</GS08>
         <ST>
            <ST01>837</ST01>
            <ST02>0001</ST02>
            <ST03>005010X223A2</ST03>
            <BHT>
               <BHT01>19</BHT01>
               <BHT02>0</BHT02>
               <BHT03>524</BHT03>
               <BHT04>20111207</BHT04>
               <BHT05>1323</BHT05>
               <BHT06>CH</BHT06>
            </BHT>
            <Loop1000/>
            <Loop1000/>
            <Loop2000/>
            <Loop2000/>
         </ST>
      </GS>
   </ISA>
</edi837>

< - 但是,我有: - >

<-- However, I have: -->

<?xml version="1.0" encoding="UTF-8"?>
<edi835>
   <ISA>
      <ISA01>00</ISA01>
      <ISA02>Authorizat</ISA02>

      <ISA16/>
      <GS>
         <GS06>1</GS06>
         <GS07>X</GS07>
         <GS08>005010X221A1</GS08>
         <ST>
            <ST01>835</ST01>
            <ST02>0001</ST02>
            <BPR/>
            <TRN/>
            <DTM/>
            <Loop1000A/>
            <Loop1000B/>
            <Loop2000/>
            <PLB/>
         </ST>
          <ST>
              <ST01>835</ST01>
              <ST02>0001</ST02>
              <BPR/>
              <TRN/>
              <DTM/>
              <Loop1000A/>
              <Loop1000B/>
              <Loop2000/>
              <PLB/>
          </ST>
      </GS>
   </ISA>
</edi835>

我的基本问题是,我在父元素中有一个字段,告诉我子类型是什么儿童领域

My basic problem is that, I have a field in a parent element that tells me what the subtype is up the child field


< GS08> 005010X223A2< / GS08>

<GS08>005010X223A2</GS08>

这样做的正确方法是什么?

What's the correct way to do this?

我基本上想要一个XmlTypeAdapter,它可以带我从1个绑定类型到另一个绑定类型。喜欢:给我第一个元素,我会告诉你如何处理其余的元素
对象工厂也许可以做到这一点。希望这很清楚

I Basically Want a XmlTypeAdapter that can take me from 1 bound-type to another. Like: "Give me the first elements and I will tell you what to do with the rest of them" An object factory might be able to do this. Hope this is clear

每个< ST>结构可能完全不同,我想做的只是指定直到那个< ST>然后让它从那里反映其余的类型。

Each <ST> structure can be so radically different that really, what I want to do is just specify up until that <ST> and then let it reflect the rest of the types from there.

我已经用ACTUAL XML(浓缩)完全替换了原始的,人为的XML。我解决了它这里,但它很难看

I have completely replaced the original, contrived XML with ACTUAL XML (condensed). I solved it here but it's ugly

推荐答案

不要这样做



Don't Do It


我的基本问题是,我有父元素中的一个字段,
告诉我子字段在子字段(限定符)中是什么。什么是
正确的方法呢?

My basic problem is that, I have a field in a parent element that tells me what the subtype is up the child field (qualifier). What's the correct way to do this?

我不会这样做,因为它不对应在XML中表示继承的标准方法。这意味着你必须跳过箍来处理它,就像需要与这个XML交互的其他人一样。

I wouldn't do it this way since it doesn't correspond to a standard way of representing inheritance in XML. This means that you will have to jump through hoops to process it, as will everyone else that needs to interact with this XML.

删除限定符元素并保持其他所有内容相同。这使得子类型成为限定符的元素名称。这对应于替换组的XML Schema概念,是表示继承的标准方法。它还使你的XML更短,这是一件好事。

Drop the qualifier element and keep everything else the same. This makes the subtypes the element name the qualifier. This corresponds to the XML Schema concept of substitution groups, and is a standard way of representing inheritance. It also makes your XML a bit shorter which is a good thing.

内部

您将在 problemType上使用 @XmlElementRef 注释 内部类的字段/属性。要解组的类型将基于遇到的元素,以及哪个子类型具有匹配的 @XmlRootElement 注释。

You will use the @XmlElementRef annotation on the problemType field/property on the Inner class. The type to be unmarshalled will be based element encountered, and which subtype has a matching @XmlRootElement annotation.

@XmlAccessorType(XmlAccessType.FIELD)
public class Inner {

    @XmlElementRef
    private ProblemType problemType;

}

ProblemType1

@XmlRootElement(name="ProblemType1")
public class ProblemType1 extends ProblemType {
}

ProblemType2

@XmlRootElement(name="ProblemType2")
public class ProblemType2 extends ProblemType {
}

这篇关于如何根据限定符选择要绑定的特定子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆