关闭

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

1785人阅读 评论(0) 收藏 举报
分类:
<?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.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlSeeAlso({B.class, C.class})
@XmlType
abstract public class A {
    int type;
    String name;

    @XmlElement
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Class Letters:

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Letters {
    List<A> list;

    @XmlElement
    public List<A> getList() {
        return list;
    }

    public void setList(List<A> list) {
        this.list = list;
    }
}

Class B:

@XmlRootElement
@XmlType
@XmlAccessorType(XmlAccessType.PROPERTY)
public class B extends A {
    String fileURL;
    String specialNotes;
    @XmlElement
    public String getFileURL() {
        return fileURL;
    }

    public void setFileURL(String fileURL) {
        this.fileURL = fileURL;
    }
    @XmlElement
    public String getSpecialNotes() {
        return specialNotes;
    }

    public void setSpecialNotes(String specialNotes) {
        this.specialNotes = specialNotes;
    }
}

Class C:

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)

public class C extends A {
    String passengerName;
    String passengerSurname;
    @XmlElement
    public String getPassengerName() {
        return passengerName;
    }

    public void setPassengerName(String passengerName) {
        this.passengerName = passengerName;
    }
    @XmlElement
    public String getPassengerSurname() {
        return passengerSurname;
    }

    public void setPassengerSurname(String passengerSurname) {
        this.passengerSurname = passengerSurname;
    }
}

Everything looks fine, but after marshalling which I do like this:

public static void main(String[] args) {
        C c = new C();
        c.setName("Simon");
        c.setType(2);
        c.setPassengerName("Johny");
        c.setPassengerSurname("Revelator");
        B b = new B();
        b.setType(4);
        b.setFileURL("someUrl");
        b.setSpecialNotes("specialNotes");
        b.setName("someName");

        List<A> list = new ArrayList<A>(2);
        list.add(c);
        list.add(b);
        Letters letter = new Letters();
        letter.setList(list);
        try {
            File file = new File("C:\file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Letters.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(letter, file);
            jaxbMarshaller.marshal(letter, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

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>
    <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b">
        <name>someName</name>
        <type>4</type>
        <fileURL>someUrl</fileURL>
        <specialNotes>specialNotes</specialNotes>
    </list>
</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 @XmlElementRef on the list property then the element for the item will be based on the @XmlRootElement of the referenced class and then you won't get the xsi:type attibute.

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Letters {
    List<A> list;

    @XmlElementRef
    public List<A> getList() {
        return list;
    }

    public void setList(List<A> list) {
        this.list = list;
    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:329482次
    • 积分:3676
    • 等级:
    • 排名:第9147名
    • 原创:74篇
    • 转载:67篇
    • 译文:0篇
    • 评论:15条
    最新评论