Remove xsi:type after marshalling abstract class with hierarchy 删除xml节点中的xsi:type xmlns:xsi

前端之家收集整理的这篇文章主要介绍了Remove xsi:type after marshalling abstract class with hierarchy 删除xml节点中的xsi:type xmlns:xsi前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<?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

@H_576_403@ @H_31_404@


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}
原文链接:https://www.f2er.com/xml/295872.html

猜你在找的XML相关文章