Android simpleXML 元素“[元素名称]”已被使用

Android simpleXML Element '[element Name]' is already used

我曾尝试使用此网站上提出的其他问题,但其中 none 与我正在做的事情有关。

我正在尝试 'deserialize' soap xml 使用 simpleXML 用于 Android 应用程序。我越来越接近这个了,但我在这里碰壁了。

当我 运行 我的代码时,出现以下错误:

org.simpleframework.xml.core.PersistenceException: Element 'serviceOrderCT' is already used with @org.simpleframework.xml.Element(name=, type=void, data=false, required=true) on field 'serviceOrderCT' private serviceOrderCT GetServiceOrdersResult.serviceOrderCT at line 26

所以,问题似乎出在 'GetServiceOrdersResult.java' class(下面的代码)上,但我无法确定它是什么。

我已经在这里工作了几个小时,但一无所获。

帮助解决这个问题将不胜感激。

谢谢。

[编辑: 这是 XML 文件]

<GetServiceOrdersResponse xmlns="http://tempuri.org/">
     <GetServiceOrdersResult xmlns:a="http://schemas.datacontract.org/2004/07/MobileWebService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:serviceOrderCT>
           <a:_bill_to_Customer_No_>xxxxx</a:_bill_to_Customer_No_>
           <a:_bill_to_Name>xxxxxxx</a:_bill_to_Name>
           <a:_customer_No_ i:nil="true" />
           <a:_description />
           <a:_fix_By_Date>xxxxxxxxxxxxx</a:_fix_By_Date>
           <a:_fix_By_Time />
           <a:_fixed_Date>xxxxxxxxxxxx</a:_fixed_Date>
           <a:_fixed_Time>xxxxxxxxxxxxxx</a:_fixed_Time>
           <a:_name>xxxxxxxxxxxxxxx</a:_name>
           <a:_no_>xxxxxxxxxxxxxxxxxxx</a:_no_>
           <a:_order_Date>xxxxxxxxxxxxxxx</a:_order_Date>
           <a:_order_Time>xxxxxxxxxxxxx</a:_order_Time>
           <a:_responded_Date>xxxxxxxxxxxxxxxxxx</a:_responded_Date>
           <a:_responded_Time />
           <a:_response_Date>xxxxxxxxxxxxxxxxxx</a:_response_Date>
           <a:_response_Time>xxxxxxxxxxxxxxxxxxxx</a:_response_Time>
           <a:_transaction_Status>xxxxxxxxxxxxxxx</a:_transaction_Status>
           <a:_your_Reference />
        </a:serviceOrderCT>
        <a:serviceOrderCT>
           a:_bill_to_Customer_No_>xxxxx</a:_bill_to_Customer_No_>
           <a:_bill_to_Name>xxxxxxx</a:_bill_to_Name>
           <a:_customer_No_ i:nil="true" />
           <a:_description />
           <a:_fix_By_Date>xxxxxxxxxxxxx</a:_fix_By_Date>
           <a:_fix_By_Time />
           <a:_fixed_Date>xxxxxxxxxxxx</a:_fixed_Date>
           <a:_fixed_Time>xxxxxxxxxxxxxx</a:_fixed_Time>
           <a:_name>xxxxxxxxxxxxxxx</a:_name>
           <a:_no_>xxxxxxxxxxxxxxxxxxx</a:_no_>
           <a:_order_Date>xxxxxxxxxxxxxxx</a:_order_Date>
           <a:_order_Time>xxxxxxxxxxxxx</a:_order_Time>
           <a:_responded_Date>xxxxxxxxxxxxxxxxxx</a:_responded_Date>
           <a:_responded_Time />
           <a:_response_Date>xxxxxxxxxxxxxxxxxx</a:_response_Date>
           <a:_response_Time>xxxxxxxxxxxxxxxxxxxx</a:_response_Time>
           <a:_transaction_Status>xxxxxxxxxxxxxxx</a:_transaction_Status>
           <a:_your_Reference />
        </a:serviceOrderCT>
     </GetServiceOrdersResult>
  </GetServiceOrdersResponse>

Envelope.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root
public class Envelope {

    @Element(name = "Body")
    private Body body;


    public Body getBody() {
        return body;
    }

}

Body.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;

import java.util.List;

public class Body {

    @Element(name = "GetServiceOrdersResponse")
    private GetServiceOrdersResponse getServiceOrdersResponse;

    public GetServiceOrdersResponse getServiceOrdersResponse() {
        return getServiceOrdersResponse;
    }
}

GetServiceOrdersResponse.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Path;

public class GetServiceOrdersResponse {

    @Element(name = "GetServiceOrdersResult")
    private GetServiceOrdersResult getServiceOrdersResult;

    public GetServiceOrdersResult getGetServiceOrdersResult() {
        return getServiceOrdersResult;
    }
}

GetServiceOrdersResult.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;

import java.util.List;

public class GetServiceOrdersResult {
    @Element
    private serviceOrderCT serviceOrderCT;

    public serviceOrderCT getServiceOrderCT() {
        return serviceOrderCT;
    }
}

serviceOrderCT

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;

public class serviceOrderCT {

    @Element(name = "_bill_to_Customer_No_", required = false)
    private String billToCustomerNo;
    @Element(name = "_bill_to_Name", required = false)
    private String billToName;
    @Element(name = "_customer_No_", required = false)
    private String customerNo;
    @Element(name = "_description", required = false)
    private String description;
    @Element(name = "_fix_By_Date", required = false)
    private String fixByDate;
    @Element(name = "_fix_By_Time", required = false)
    private String fixByTime;
    @Element(name = "_fixed_Date", required = false)
    private String fixedDate;
    @Element(name = "_fixed_Time", required = false)
    private String fixedTime;
    @Element(name = "_name", required = false)
    private String name;
    @Element(name = "_no_", required = false)
    private String no;
    @Element(name = "_order_Date", required = false)
    private String orderDate;
    @Element(name = "_order_Time", required = false)
    private String orderTime;
    @Element(name = "_responded_Date", required = false)
    private String respondedDate;
    @Element(name = "_responded_Time", required = false)
    private String respondedTime;
    @Element(name = "_response_Date", required = false)
    private String responseDate;
    @Element(name = "_response_Time", required = false)
    private String responseTime;
    @Element(name = "_transaction_Status", required = false)
    private String transactionStatus;
    @Element(name = "_your_Reference", required = false)
    private String yourReference;

    public serviceOrderCT() {
    }

    public String getBillToCustomerNo() {
        return billToCustomerNo;
    }

    public void setBillToCustomerNo(String billToCustomerNo) {
        this.billToCustomerNo = billToCustomerNo;
    }

    public String getBillToName() {
        return billToName;
    }

    public void setBillToName(String billToName) {
        this.billToName = billToName;
    }

    public String getCustomerNo() {
        return customerNo;
    }

    public void setCustomerNo(String customerNo) {
        this.customerNo = customerNo;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getFixByDate() {
        return fixByDate;
    }

    public void setFixByDate(String fixByDate) {
        this.fixByDate = fixByDate;
    }

    public String getFixByTime() {
        return fixByTime;
    }

    public void setFixByTime(String fixByTime) {
        this.fixByTime = fixByTime;
    }

    public String getFixedDate() {
        return fixedDate;
    }

    public void setFixedDate(String fixedDate) {
        this.fixedDate = fixedDate;
    }

    public String getFixedTime() {
        return fixedTime;
    }

    public void setFixedTime(String fixedTime) {
        this.fixedTime = fixedTime;
    }

    public String getName() {
        return name;
    }

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

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(String orderDate) {
        this.orderDate = orderDate;
    }

    public String getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }

    public String getRespondedDate() {
        return respondedDate;
    }

    public void setRespondedDate(String respondedDate) {
        this.respondedDate = respondedDate;
    }

    public String getRespondedTime() {
        return respondedTime;
    }

    public void setRespondedTime(String respondedTime) {
        this.respondedTime = respondedTime;
    }

    public String getResponseDate() {
        return responseDate;
    }

    public void setResponseDate(String responseDate) {
        this.responseDate = responseDate;
    }

    public String getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(String responseTime) {
        this.responseTime = responseTime;
    }

    public String getTransactionStatus() {
        return transactionStatus;
    }

    public void setTransactionStatus(String transactionStatus) {
        this.transactionStatus = transactionStatus;
    }

    public String getYourReference() {
        return yourReference;
    }

    public void setYourReference(String yourReference) {
        this.yourReference = yourReference;
    }

}

Main.java

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import java.io.File;

public class Main {
    public static void main(String[] args) {
        try {
            Serializer serializer = new Persister();
            File result = new File("C:\Users\xxxxxxxx\Documents\file.xml");

            Envelope example = serializer.read(Envelope.class, result);
            System.out.println(example.getBody().getServiceOrdersResponse().getGetServiceOrdersResult().getServiceOrderCT().getBillToCustomerNo());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

所以,在寻求帮助的第二天,我自己解决了我的问题又一次

如果你想获得所有三个 serviceOrderCT,你必须实现一个 ElementList。只捡了一个

如果我删除了另外两个,我会得到一些东西。

建议阅读本文的任何人,坚持使用官方文档。它会对你有很大帮助。 http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#list