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



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}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇