加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Remove xsi:type after marshalling abstract class with hierar

发布时间:2020-12-16 06:02:41 所属栏目:百科 来源:网络整理
导读:?xml version="1.0" encoding="UTF-8" standalone="yes"?MbfService output1 MbfHeader ServiceCodeLMPCustLevelMgmt/ServiceCode OperationqueryLevelInfo/Operation UId414d5120514d5f6c6f63616c202020203baa474c20012802/UId ServiceResponse StatusCOMPL
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MbfService>
    <output1>
        <MbfHeader>
            <ServiceCode>LMPCustLevelMgmt</ServiceCode>
            <Operation>queryLevelInfo</Operation>
            <UId>414d5120514d5f6c6f63616c202020203baa474c20012802</UId>
            <ServiceResponse>
                <Status>COMPLETE</Status>
            </ServiceResponse>
        </MbfHeader>
        <MbfBody>
            <appHeader>
                <getRecNum>1</getRecNum>
                <beginRecNum>0</beginRecNum>
            </appHeader>
            <body xsi:type="tr01010010Res" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <custNum>6002058692</custNum>
                <levelTypeStructList>
                    <levelTypeStruct>
                        <levelType>A1000020</levelType>
                        <levelNum>B1000010</levelNum>
                    </levelTypeStruct>
                </levelTypeStructList>
            </body>
        </MbfBody>
    </output1>
</MbfService>

上面是使用JAXBContext解析出的报文,想把body节点中的xsi:type xmlns:xsi删除,google找出一篇文章,记录之:


Remove xsi:type after marshalling abstract class with hierarchy

If someone help me,that will be great. So I have a structure:

<span style="font-family:Courier New;font-size:12px;"><letters> 
  <list> 
    <name>Simon</name>  
    <type>2</type>  
    <passengerName>Johny</passengerName>  
    <passengerSurname>Revelator</passengerSurname> 
  </list>  
  <list> 
    <name>someName</name>  
    <type>4</type>  
    <fileURL>someUrl</fileURL>  
    <specialNotes>specialNotes</specialNotes> 
  </list> 
</letters></span>


For this structure I wrote Java Classes and they marshall and unmarshall it: Class A package com.edhex.testing;

import javax.xmlbindannotation.*; @XmlAccessorType(XmlAccessTypePROPERTY)@XmlSeeAlso({B.class, C})@XmlType abstractpublicclass A {int type; String name@XmlElement getType() return}void setType(thistype = getName setNamename }

Class Letters:

@XmlRootElementLettersList<A> list getList setListlist  Class B:

 B extends fileURL specialNotes getFileURL setFileURLfileURL  getSpecialNotes setSpecialNotesspecialNotes  Class C:

 C  passengerName passengerSurname getPassengerName setPassengerNamepassengerName  getPassengerSurname setPassengerSurnamepassengerSurname  Everything looks fine,but after marshalling which I do like this:

static mainString[] args C c new(); csetName("Simon");setType(2setPassengerName"Johny"setPassengerSurname"Revelator" B b  B b4setFileURL"someUrl"setSpecialNotes"specialNotes""someName" list ArrayList>(add(cb letter Letters lettersetListlisttryFile file File"C:file.xml"JAXBContext jaxbContext JAXBContextnewInstanceMarshaller jaxbMarshaller  jaxbContextcreateMarshaller jaxbMarshallersetPropertyMarshallerJAXB_FORMATTED_OUTPUTtruemarshalletter fileSystemoutcatchJAXBException eprintStackTrace I get output like this:

<letters> <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:type"c"<name>Lasha</name><type>2</type><passengerName>Johny</passengerName><passengerSurname>Revelator</passengerSurname></list>"b"someName4<fileURL>someUrl</fileURL><specialNotes>specialNotes</specialNotes></letters>

Question:How can I remove those: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b" in each list tag? I know that JAXB will notify me with what type is it using,because originally mapping is using another type (A.class),but I don't need that information. Have somebody any ideas?


Answers



If you use@XmlElementRefon thelistproperty then the element for the item will be based on the@XmlRootElementof the referenced class and then you won't get thexsi:typeattibute.

@XmlElementRef}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读