我正在编写以下方法来递归解析包含复杂XMLSoapObject数据(这是一个SoapObject,其中SoapObject作为某些属性的值)。 SoapObject是来自基于SOAP的Web服务的响应,我使用ksoap2进行了消费

在此方法中,我们遍历传入的响应SoapObject的所有属性。我们检查属性是否不是SoapObject(这意味着它具有简单的String名称和String值),在这种情况下,我们只需将名称和值作为键值放入HashMap对。但是,如果属性是SoapObject,我们将调用方法本身(递归),并将其传递给SoapObject

码:

static protected Map<String, String> parseComplexSoapObject(SoapObject soapObject) {
        Map<String, String> hashMap = new HashMap<String, String>();

        //Iterate through the properties starting at top of soapObject
        for (int i=0; i<soapObject.getPropertyCount(); i++) {

            //Get the current property
            Object currentPropertyObject = soapObject.getProperty(i);

            //If current property is the last property, check if it is a string or a soap object
            if (i==soapObject.getPropertyCount()-1) {
                //If it is a string, add the key value pair of property to the map and break the loop. The only statement after the loop will return the map.
                if(currentPropertyObject!=null && !(currentPropertyObject instanceof SoapObject)) {
                    hashMap.put(currentPropertyObject.toString(), currentPropertyObject.toString());
                    break;
                }
                //else If it is a soap object, cast the currentPropertyObject to SoapObject and make a recursive call to the function itself, passing the current property so that the method gets "inside" this current property soap object and does all this again
                else if(currentPropertyObject!=null && currentPropertyObject.getClass().equals(SoapObject.class)) {
                    SoapObject currentPropertySoapObject = (SoapObject) currentPropertyObject;
                    parseComplexSoapObject(currentPropertySoapObject);
                }
            }
            //else if the current property not the last property, check if it is a soap object or a string
            else {
                //if the current property is a string, add its key value pair to the string  and "continue" the loop, so that the loop continues to read the next properties
                if(currentPropertyObject!=null && !(currentPropertyObject instanceof SoapObject)) {
                    hashMap.put(currentPropertyObject.toString(), currentPropertyObject.toString());
                    continue;
                }
                //else if the current property is a soap object, cast the currentPropertyObject to SoapObject and make a recursive call to the function itself, passing the current property so that the method gets "inside" this current property soap object and does all this again
                else if(currentPropertyObject!=null && currentPropertyObject.getClass().equals(SoapObject.class)) {
                    SoapObject currentPropertySoapObject = (SoapObject) currentPropertyObject;
                    parseComplexSoapObject(currentPropertySoapObject);
                }
            }
        }

        return hashMap;
    }


问题:


现在,问题在于方法返回的HashMap是在该方法内部创建的,因此,每次递归调用该方法时,都会重新创建HashMap并且丢失先前的数据。
即使我在此方法之外创建HashMap并从该方法简单地向其写入数据,问题仍将是,由于哪个名称值对来自哪个嵌套级别(例如

<result>
  <id>25323205</id>
  <name>Cric</name>
<result>
<version>1.0</version>
<result>
  <id>445</id>
</result>



将像HashMap = {id=>25323205, name=>Cric, version=>1.0, id=>445}一样存储。我希望这样:

HashMap = {result=>{id=>25323205, name=>Cric}, version=>1.0, result=>{id=>445}}


那我该怎么办?

最佳答案

关键是,您尝试将多维数据转换为平面结构。仅在某些情况下才有可能。考虑一下它是否真的可以满足您的数据和业务需求。
我有一些想法,您可能会做些什么-任何可能都足够,每个都没用:)


如果名称的乘法始终在不同的嵌套级别上,请尝试使用“ body” fe中的全名。 “结果/ ID”,“结果/某物/ ID”。
您可以编号类似标签fe。 “ id> 001”,“ id> 002”或其他内容(需要第二个地图-整数)。它的方式允许您存储数据,但是却使您在阅读,搜索等方面的速度大大降低。如果您以后只是从头到尾遍历数据,那就足够了。
如果需要将代码与ksoap数据模型隔离,则可以尝试将SoapObject / Primitive结构投影到使用标准Java对象构建的结构中,但是具有相同的复杂性并且不会丢失任何信息。 xml的此结构将是列表列表。


这是示例类Elem,可以容纳Name + Value,其中value可以是Object.Object将是ArrayList或String。以及将Soap ...转换为新结构的简单方法:

class Elem{
    String name;
    Object value;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Object getValue() {
        return value;
    }
    public void setValue(Object value) {
        this.value = value;
    }
}

List<Elem> parseComplexSoapObject(SoapObject soapObject) {
    List<Elem> tmp=new ArrayList<Elem>();
    PropertyInfo info=new PropertyInfo();

    for (int i=0; i<soapObject.getPropertyCount(); i++) {
        Object currentPropertyObject = soapObject.getProperty(i);

        Elem newElem = new Elem();
        if(currentPropertyObject instanceof SoapObject){
            SoapObject currentPropertySoapObject = (SoapObject)currentPropertyObject;
            info.clear();
            soapObject.getPropertyInfo(i, info);
            newElem.setName(info.name);
            newElem.setValue(parseComplexSoapObject(currentPropertySoapObject));
        }else{
            //assume primitive
            SoapPrimitive currentPropertySoapPrimitive = (SoapPrimitive)currentPropertyObject;
            info.clear();
            soapObject.getPropertyInfo(i, info);
            newElem.setName(info.name);
            newElem.setValue(currentPropertySoapPrimitive.getValue().toString());
        }
        tmp.add(newElem);
    }

    return tmp;
}

08-04 23:44