如何在解组时将xml的多次出现捕获到pojo?

时间:2019-01-10 21:15:52

标签: java xml jaxb unmarshalling

只需尝试学习和理解Jaxb以及xml绑定是如何工作的,所以非常感谢!我不知道如何或是否会再次出现块或节?在下面,MM块可以是1或很多,如何吸收到pojo? Jaxb或其他引擎如何绑定多个部分,例如MM部分?谢谢!!!

我有一个需要解组到Java POJO的XML文件。

A   018912111835030108XXXXXXXXXX
<RTR>
<HEAD>
<IDCMS pn="ABF31A6FNCL0004" vers="4.0"/>
<DA>2018/12/11 01:07:37</DA>
<ACD MSN="0149" tail="N503DN" type="A350-900"/>
<TID>C00000011</TID>
</HEAD>
<HEADRTR>
<FROM>KDTW</FROM>
<TO>ZBAA</TO>
<FNBR>DAL189    </FNBR>
</HEADRTR>
<ITD>
 <CB>A</CB>
 <NFDE>
<MC>3166X001</MC>
<MD>INDICATING-OIS CAPT-&quot;OIS NOT AVAIL&quot; displayed instead of the requested video</MD>
 </NFDE>
<MM hdi="y" fo="A" occ="1">
 <MC>4612F11W</MC>
 <MD>VGM-1(107TC1) / AVM-1(103TC1) / OSFC RESET</MD>
 <DA>2018/12/11 00:57:39</DA>
 <ATA>4612</ATA>
 <FPH>8</FPH>
 <FCL>1</FCL>
 <PRIO>high</PRIO>
 <STA>1</STA>
 <SYS name="ASF" bite_id="107" side="0"/>
</MM>
<MM hdi="y" occ="1">
 <MC>3166F8TT</MC>
 <MD>VGM-1(107TC1) / CMV-2(1TE2) / WRG TO CMV-2(1TE2) PIN AEDD</MD>
 <DA>2018/12/11 00:57:36</DA>
  <CDA>2018/12/11 00:57:37</CDA>
 <ATA>3166</ATA>
 <FPH>8</FPH>
 <FCL>1</FCL>
 <PRIO>low</PRIO>
 <STA>2</STA>
   <SYS name="CMV2" bite_id="31" side="2"/>
 </MM>
</ITD>
</RTR>

创建了XML结构的类,减去了如何使用重复发生的部分。我不会全部发布。我认为对某人的帮助将对其他人有所帮助。谢谢!

@XmlRootElement( name = "MM" )
public class Mm {

private String mc;
private String md;
private String da;
private String ata;
private String fph;
private String prio;

Mm(){
    super();
}

Mm( @XmlElement(name="MC") String mc,
    @XmlElement(name="MD") String md,
    @XmlElement(name="DA") String da,
    @XmlElement(name="FPH") String fph, 
    @XmlElement(name="PRIO") String prio) {
    this.mc = mc;
    this.md = md;
    this.da = da;
    this.fph = fph;
    this.prio = prio;
}

public String getMc() {
    return mc;
}
public String getMd() {
    return md;
}
public String getDa() {
    return da;
}
public String getAta() {
    return ata;
}
public String getFph() {
    return fph;
}
public String getPrio() {
    return prio;
}

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder("MC=");
    sb.append(getMc()).append(",");
    sb.append("MD=").append(getMd()).append(",");
    sb.append("DA=").append(getDa()).append(",");
    sb.append("ATA=").append(getAta()).append(",");
    sb.append("FPH=").append(getFph()).append(",");
    sb.append("PRIO=").append(getPrio());

    return sb.toString();
}

}

  @XmlRootElement( name = "FDE" )
public class Fde {

private String mc;
private String md;
private String da;
private String fph;

Fde(){
    super();
}

Fde( @XmlElement(name="MC") String mc,
    @XmlElement(name="MD") String md,
    @XmlElement(name="DA") String da,
    @XmlElement(name="FPH") String fph) {
    this.mc = mc;
    this.md = md;
    this.da = da;
    this.fph = fph;
}

public String getMc() {
    return mc;
}
public String getMd() {
    return md;
}
public String getDa() {
    return da;
}
public String getFph() {
    return fph;
}

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder("MC=");
    sb.append(getMc()).append(",");
    sb.append("MD=").append(getMd()).append(",");
    sb.append("DA=").append(getDa()).append(",");
    sb.append("FPH=").append(getFph());

    return sb.toString();
}
}

@XmlRootElement( name = "HEADRTR" )
public class HeadRtr {

private String from;
private String to;
private String fnbr;

HeadRtr(){
    super();
}

HeadRtr(@XmlElement(name="FROM") String from,
        @XmlElement(name="TO") String to,
        @XmlElement(name="FNBR") String fnbr) {
        this.from = from;
        this.to = to;
        this.fnbr = fnbr;
}   
public String getFrom() {
    return from;
}
public String getTo() {
    return to;
}
public String getFnbr() {
    return fnbr;
}

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder("FROM=");
    sb.append(getFrom()).append(",");
    sb.append("TO=").append(getTo()).append(",");
    sb.append("FNBR=").append(getFnbr());

    return sb.toString();
}

}

我正在考虑这些对象的列表

2 个答案:

答案 0 :(得分:1)

从xml的外观来看。 您希望将ITD,FDE,MM作为对象。 ITD有一个列表,FDE是成员。

答案 1 :(得分:0)

感谢Mavriksc,这是对HTML XML文档进行解码的结果。 这是我们需要解组的示例HTML XML文档。

<RTR>
<HEAD>
<IDCMS pn="ABF31A6FNCL0004" vers="4.0"/>
<DA>2018/12/11 00:15:42</DA>
<ACD MSN="0160" tail="N504DN" type="A350-900"/>
<TID>C00000023</TID>
</HEAD>
<HEADRTR>
<FROM>ZSPD</FROM>
<TO>KLAX</TO>
<FNBR>DAL88     </FNBR>
</HEADRTR>
<ITD>
<CB>A</CB>
<FDE disp="n" dm="y">
<MC>3031H050</MC>
<MD>A-ICE // &gt; A-ICE SIDESLIP PROBE 2 HEATG</MD>
<DA>2018/12/11 00:12:42</DA>
<CDA>2018/12/11 00:14:17</CDA>
<FPH>8</FPH>
<LAB>1</LAB>
</FDE>
<MM fo="A" occ="5">
<MC>3413F7EV</MC>
<MD>PROBE-SSA,2(11FP2)</MD>
<DA>2018/12/11 00:08:43</DA>
<ATA>3413</ATA>
<FPH>8</FPH>
<FCL>1</FCL>
<PRIO>high</PRIO>
<STA>1</STA>
<SYS name="ADR2" bite_id="3" side="2"/>
</MM>
</ITD>
</RTR>

这是Jaxb POJO将XML映射到POJO。

RTR

@XmlRootElement(name = "RTR", namespace="com.ge.ip.infra.fadec.jaxb.decoder")
public class Rtr implements Serializable {

    private static final long serialVersionUID = 1L;

    public Rtr() {
        super();
    }

    private Itd itd;
    private HeadRtr headRtr;
    private Head head;

    public Itd getItd() {
        return itd;
    }
    public void setItd(Itd itd) {
        this.itd = itd;
    }
    public HeadRtr getHeadRtr() {
        return headRtr;
    }
    public void setHeadRtr(HeadRtr headRtr) {
        this.headRtr = headRtr;
    }
    public Head getHead() {
        return head;
    }
    public void setHead(Head head) {
        this.head = head;
    }

    @Override
    public String toString()
    {
        return "RTR ITD = " + itd + ", HEADRTR = " + headRtr + ", HEAD = " + head;
    }
}

@XmlRootElement(name = "HEAD")
public class Head implements Serializable {

    private static final long serialVersionUID = 1L;

    @XmlElement(name = "IDCMS") private Idcms idcms;
    @XmlElement(name = "DA") private String da;
    @XmlElement(name = "ACD") private Acd acd;
    @XmlElement(name = "TID") private String tid;

    public String getDa() {
        return da;
    }
    public String getTid() {
        return tid;
    }
    public void setDa(String da) {
        this.da = da;
    }
    public void setTid(String tid) {
        this.tid = tid;
    }
    public Acd getAcd () {
        return acd;
    }
    public void setAcd (Acd acd) {
        this.acd = acd;
    }
    public Idcms getIdcms () {
        return idcms;
    }
    public void setIdcms (Idcms idcms) {
        this.idcms = idcms;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DA=");
        sb.append(getDa()).append(",");
        sb.append("TID=").append(getTid()).append(",");
        sb.append(acd.toString());
        sb.append(idcms.toString());

        return sb.toString();
    }
//}

public class Idcms implements Serializable {

    private static final long serialVersionUID = 1L;

    @XmlAttribute(name = "pn") private String pn;
    @XmlAttribute(name = "vers") private String vers;

    public String getPn() {
        return pn;
    }
    public String getVers() {
        return vers;
    }
    public void setPn(String pn) {
        this.pn = pn;
    }
    public void setVers(String vers) {
        this.vers = vers;
    }
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("PN=");
        sb.append(getPn()).append(",");
        sb.append("VERS=").append(getVers());

        return sb.toString();
    }
}

public class Acd implements Serializable {

    private static final long serialVersionUID = 1L;

    @XmlAttribute(name = "MSN") private String msn;
    @XmlAttribute(name = "tail") private String tail;
    @XmlAttribute(name = "type") private String type;

    public String getMsn() {
        return msn;
    }
    public String getTail() {
        return tail;
    }
    public String getType() {
        return type;
    }

    public void setMsn(String msn) {
        this.msn = msn;
    }
    public void setTail(String tail) {
        this.tail = tail;
    }
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("MSN=");
        sb.append(getMsn()).append(",");
        sb.append("TAIL=").append(getTail()).append(",");
        sb.append("TYPE=").append(getType());

        return sb.toString();
    }
}

}

HEADRTR

@XmlRootElement( name = "HEADRTR" )
public class HeadRtr implements Serializable {

    private static final long serialVersionUID = 1L;

    private String from;
    private String to;
    private String fnbr;

    HeadRtr(){
        super();
    }

    HeadRtr(@XmlElement(name="FROM") String from,
            @XmlElement(name="TO") String to,
            @XmlElement(name="FNBR") String fnbr) {
            this.from = from;
            this.to = to;
            this.fnbr = fnbr;
    }   
    public String getFrom() {
        return from;
    }
    public String getTo() {
        return to;
    }
    public String getFnbr() {
        return fnbr;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("FROM=");
        sb.append(getFrom()).append(",");
        sb.append("TO=").append(getTo()).append(",");
        sb.append("FNBR=").append(getFnbr());

        return sb.toString();
    }

}

ITD

@XmlRootElement( name = "ITD" )
@XmlAccessorType (XmlAccessType.FIELD)
public class Itd implements Serializable {

    private static final long serialVersionUID = 1L;

    private Mm mm;
    private Fde fde;
    private Nfde nfde;

    public Mm getMm() {
        return mm;
    }
    public void setMm (Mm mm)
    {
        this.mm = mm;
    }
    @XmlElement(name="CB")
    private String cb;

    public String getCb() {
        return cb;
    }
    public void setCb(String cb) {
        this.cb = cb;
    }

    public Fde getFde ()
    {
        return fde;
    }
    public void setFde (Fde fde)
    {
        this.fde = fde;
    }

    public Nfde getNfde() {
        return nfde;
    }
    public void setNfde (Nfde nfde)
    {
        this.nfde = nfde;
    }

    @Override
    public String toString()
    {
        return "ITD MM = " + mm + ", CB = " + cb + ", FDE = " + fde + ", NFDE = " + nfde;
    }
}

NFDE

@XmlRootElement( name = "NFDE" )
@XmlAccessorType (XmlAccessType.FIELD)
public class Nfde implements Serializable {

    private static final long serialVersionUID = 1L;

    private String mc;
    private String md;

    public String getMc() {
        if ( !mc.isEmpty() && null != mc ) {
            return mc;
        } else {
            return "NFDE";
        }
    }
    public String getMd() {
        if ( !md.isEmpty() && null != md ) {
            return md;
        } else {
            return "NFDE";
        }
    }

    @XmlElement(name="MC")
    public void setMc(String mc) {
        this.mc = mc;
    }

    @XmlElement(name="MD")
    public void setMd(String md) {
        this.md = md;
    }

}

FDE

@XmlRootElement( name = "FDE" )
@XmlAccessorType (XmlAccessType.FIELD)
public class Fde implements Serializable {

    private static final long serialVersionUID = 1L;

    private String mc;
    private String md;
    private String da;
    private String fph;

    Fde(){
        super();
    }

    Fde( @XmlElement(name="MC") String mc,
        @XmlElement(name="MD") String md,
        @XmlElement(name="DA") String da,
        @XmlElement(name="FPH") String fph) {
        this.mc = mc;
        this.md = md;
        this.da = da;
        this.fph = fph;
    }

    public String getMc() {
        return mc;
    }
    public String getMd() {
        return md;
    }
    public String getDa() {
        return da;
    }
    public String getFph() {
        return fph;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("MC=");
        sb.append(getMc()).append(",");
        sb.append("MD=").append(getMd()).append(",");
        sb.append("DA=").append(getDa()).append(",");
        sb.append("FPH=").append(getFph());

        return sb.toString();
    }
}

MM

@XmlRootElement( name = "MM" )
@XmlAccessorType (XmlAccessType.FIELD)
public class Mm implements Serializable {

    private static final long serialVersionUID = 1L;

    private String mc;
    private String md;
    private String da;
    private String ata;
    private String fph;
    private String prio;

    Mm(){
        super();
    }

    Mm( @XmlElement(name="MC") String mc,
        @XmlElement(name="MD") String md,
        @XmlElement(name="DA") String da,
        @XmlElement(name="FPH") String fph, 
        @XmlElement(name="PRIO") String prio) {
        this.mc = mc;
        this.md = md;
        this.da = da;
        this.fph = fph;
        this.prio = prio;
    }

    public String getMc() {
        return mc;
    }
    public String getMd() {
        return md;
    }
    public String getDa() {
        return da;
    }
    public String getAta() {
        return ata;
    }
    public String getFph() {
        return fph;
    }
    public String getPrio() {
        return prio;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("MC=");
        sb.append(getMc()).append(",");
        sb.append("MD=").append(getMd()).append(",");
        sb.append("DA=").append(getDa()).append(",");
        sb.append("ATA=").append(getAta()).append(",");
        sb.append("FPH=").append(getFph()).append(",");
        sb.append("PRIO=").append(getPrio());

        return sb.toString();
    }
}

解码器POJO

public class A350Decoder implements Serializable {

    private static final long serialVersionUID = 1L;

    public A350Decoder() {
        super();
    }

    private Rtr rtr;

    public Rtr getRtr() {
        return rtr;
    }
    public void setRtr(Rtr rtr) {
        this.rtr = rtr;
    }

    @Override
    public String toString()
    {
        return "A350Decoder RTR = " + rtr;
    }
}