如何在Retrofit XML Request中添加元素数组中的属性?

时间:2018-03-16 06:58:11

标签: java android soap xml-parsing retrofit2

我正在尝试在元素数组中添加属性,但它正在添加根元素。如何在数组元素中添加属性元素。我试图按照以下方式实施,但它无效。

请在下面查看实际和预期输出以及POJO请求类。

实际

<soapenv:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:sug="http://www.sugarcrm.com/sugarcrm"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <soapenv:Body>
        <sug:set_entry arrayType="sug:name_value[]"> // <----ERROR------
            <session>o64reg2perf28joi94a1aoutj6</session>
            <module_name>Cases</module_name>
            <name_value_list length="2">
                <item>
                    <name>status</name>
                    <value>Open</value>
                </item>
                <item>
                    <name>description</name>
                    <value>Testing</value>
                </item>
            </name_value_list>
        </sug:set_entry>
    </soapenv:Body>
</soapenv:Envelope>

预期

<soapenv:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:sug="http://www.sugarcrm.com/sugarcrm"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <soapenv:Body>
        <sug:set_entry>
            <session>xsfsfsffsdfsdfwerdfsf</session>
            <module_name>Cases</module_name>
            <name_value_list length="2" arrayType="sug:name_value[]"> --- Need like this----
                <item>
                    <name>status</name>
                    <value>Open</value>
                </item>
                <item>
                    <name>description</name>
                    <value>Testing</value>
                </item>
            </name_value_list>
        </sug:set_entry>
    </soapenv:Body>
</soapenv:Envelope>

我当前的请求类

@Root(name = "sug:set_entry")
public class CreateRequest
{
 @Element(name = "session")
 private String param1;

 @Element(name = "module_name")
 private String param2;

 @ElementArray(name = "name_value_list")
 private NameValueListItem[] param3;

 @Attribute
 private String arrayType;


 public String getSessionId() {
 return param1;
 }

 public void setSessionId(String sessionId) {
 this.param1 = sessionId;
 }

 public String getModuleName() {
 return param2;
 }

 public void setModuleName(String moduleName) {
 this.param2 = moduleName;
 }

 public NameValueListItem[] getNameValueList() {
 return param3;
 }

 public void setNameValueList(NameValueListItem[] nameValueList) {
 this.param3 = nameValueList;
 }

 public String getArrayType() {
 return arrayType;
 }

 public void setArrayType(String arrayType) {
 this.arrayType = arrayType;
 }
}

更新了Class NameValueListItem

@Root(name = "item")
public class NameValueListItem
{
    @Element(name = "name")
    private String param1;

    @Element(name = "value")
    private String param2;

    @Attribute
    private String arrayType;

    public NameValueListItem(String name, String value,String arrayType) {
        this.param1 = name;
        this.param2 = value;
        this.arrayType =arrayType;
    }

    public String getName() {
        return param1;
    }

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

    public String getValue() {
        return param2;
    }

    public void setValue(String value) {
        this.param2 = value;
    }
}

更新了XML输出

<soapenv:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:sug="http://www.sugarcrm.com/sugarcrm"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <soapenv:Body>
        <sug:set_entry>
            <session>dug17epvfjc2i6sfb5djpaljc1</session>
            <module_name>Cases</module_name>
            <name_value_list length="2">
                <item arrayType="sug:name_value[]"> <-- Added Here This is error
                    <name>status</name>
                    <value>Open</value>
                </item>
                <item arrayType="sug:name_value[]">
                    <name>description</name>
                    <value>Testing</value>
                </item>
            </name_value_list>
        </sug:set_entry>
    </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

 @Attribute
 private String arrayType;

将其移至NameValueListItem课程。