java - ksoap2:解析复杂的SoapObject-如何防止此 HashMap 被覆盖?

标签 java android parsing recursion ksoap2

我正在编写以下方法来递归地解析包含复杂 SoapObjectXML数据(这是一个 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;
    }

问题:

  1. 现在的问题是,该方法返回的 HashMap 是在该方法内部创建的,因此每次递归调用该方法时,都会重新创建 HashMap并且之前的数据会丢失。

  2. 即使我在该方法之外创建 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}}

那么我该怎么办呢?

最佳答案

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

  1. 如果名称的乘法始终位于不同的嵌套级别 - 尝试使用“body”fe 中的全名。 “结果/id”,“结果/某事/id”。

  2. 您可以对类似的标签进行编号。 “id>001”,“id>002”或其他东西(需要第二张 map - 整数)。这种方式允许你存储,但会减慢你的阅读、搜索等速度。如果你以后只是从头到尾遍历数据就足够了。

  3. 如果您需要将代码与 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;
}

关于java - ksoap2:解析复杂的SoapObject-如何防止此 HashMap 被覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27187849/

相关文章:

java - 如何一次性获取数据

java - 基于 Spring Java 的配置和资源问题

没有 EditText 的 Android 键盘 InputType

java - 如何设置PDFBox的字符编码

parsing - 编程语言语法

ios - JSON 解析并收到 : fatal error: unexpectedly found nil while unwrapping an Optional value

java - 等待并通知问题

java - 返回原始开始位置,无需退出应用程序并再次打开它

android - Cordova android 应用程序 - POST 请求返回 "forbidden access"错误

android - 错误 : Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION