为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕桂英546537 2022-04-28 16:58:26
只是想学习和理解 Jaxb 以及 xml 绑定是如何工作的,非常感谢!我不明白它如何或是否会重复出现块或部分?在下面,MM 块可以是 1 个或多个,这是如何被 pojo 吸收的?Jaxb 或其他引擎如何绑定多个部分,比如 MM 部分?谢谢你!!!我有一个 XML 文件,我需要将其解组为 Java POJO。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 结构创建类减去如何使用重复出现的部分。我不会全部发布。我认为对一个人的帮助会做其他人。谢谢!
查看完整描述

2 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

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



查看完整回答
反对 回复 2022-04-28
?
慕莱坞森

TA贡献1810条经验 获得超4个赞

这里是解码 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();

    }

}


}

头部


@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();

    }


}

国际贸易发展署


@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();

    }

}

毫米


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

    }

}


查看完整回答
反对 回复 2022-04-28
  • 2 回答
  • 0 关注
  • 102 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信