如何更改Castor映射以从XML输出中的元素中删除“xmlns:xsi”和“xsi:type”属性?

时间:2010-06-09 16:15:11

标签: java xml mapping castor

如何更改Castor映射

<?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>

要取消XML输出元素中的xmlns:xsixsi:type属性?例如,而不是输出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>

我更喜欢

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

这样元素名称意味着xsi:type

1 个答案:

答案 0 :(得分:6)

将Castor Marshaller属性suppressXSIType设置为false:

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

请参阅Configuring the Marshaller中的Castor 1.3.1 Reference Documentation。 (请注意,表1.10 Marshaller属性仅列出属性suppressNamespaces,但方法setSuppressNamespaces()setSuppressXSIType()都存在于类Marshaller中。)