How change Castor mapping to remove “xmlns:xsi” and “xsi:type” attributes from element in XML output?

跟風遠走 提交于 2019-12-22 08:02:26

问题


How do I change the Castor mapping

<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
                         "http://castor.org/mapping.dtd">

<mapping>
    <class name="java.util.ArrayList" auto-complete="true">
        <map-to xml="ArrayList" />
    </class>
    <class name="com.db.spgit.abstrack.ws.response.UserResponse">
        <map-to xml="UserResponse" />
        <field name="id" type="java.lang.String">
            <bind-xml name="id" node="element" />
        </field>
        <field name="deleted" type="boolean">
            <bind-xml name="deleted" node="element" />
        </field>
        <field name="name" type="java.lang.String">
            <bind-xml name="name" node="element" />
        </field>
        <field name="typeId" type="java.lang.Integer">
            <bind-xml name="typeId" node="element" />
        </field>
        <field name="regionId" type="java.lang.Integer">
            <bind-xml name="regionId" node="element" />
        </field>
        <field name="regionName" type="java.lang.String">
            <bind-xml name="regionName" node="element" />
        </field>
    </class>
</mapping>

to suppress the xmlns:xsi and xsi:type attributes in the element of the XML output? For example, instead of the output XML

<?xml version="1.0" encoding="UTF-8"?> 
<ArrayList>
    <UserResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="UserResponse">
        <name>Tester</name>
        <typeId>1</typeId>
        <regionId>2</regionId>
        <regionName>US</regionName>
    </UserResponse>
</ArrayList>

I'd prefer

<?xml version="1.0" encoding="UTF-8"?> 
<ArrayList>
    <UserResponse>
        <name>Tester</name>
        <typeId>1</typeId>
        <regionId>2</regionId>
        <regionName>US</regionName>
    </UserResponse>
</ArrayList>

such that the element name implies the xsi:type.


回答1:


Set the Castor Marshaller property suppressXSIType to false:

Marshaller marshaller = new Marshaller(w);
marshaller.setSuppressXSIType(true);

See Configuring the Marshaller in the Castor 1.3.1 Reference Documentation. (Note that Table 1.10 Marshaller properties lists only property suppressNamespaces, but methods setSuppressNamespaces() and setSuppressXSIType() both exist in class Marshaller.)



来源:https://stackoverflow.com/questions/3007706/how-change-castor-mapping-to-remove-xmlnsxsi-and-xsitype-attributes-from-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!