title: ksoap2: Parsing complex SoapObject- How to prevent this hashmap from being overwritten?
body: <p>I am writing the following method to <em>recursively</em> parse <code>XML</code> data contained a <em>complex</em> <code>SoapObject</code> (<em>that is a <code>SoapObject</code> which has <code>SoapObject</code> as a value of some properties</em>). The SoapObject is the response from a SOAP based web service, which I consumed using <code>ksoap2</code></p>
  
  <p><strong>In this method</strong>, we iterate through all the properties of the passed-in response SoapObject. We check if the property is NOT a <code>SoapObject</code> (which means it has a simple <code>String</code> name and a <code>String</code> value), in which case we simply put the name and value into a <code>HashMap</code> as a key-value pair. But if the property IS a <code>SoapObject</code>, we call the method itself (recursive) and pass it this very <code>SoapObject</code>...</p>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>static protected Map&lt;String, String&gt; parseComplexSoapObject(SoapObject soapObject) {
          Map&lt;String, String&gt; hashMap = new HashMap&lt;String, String&gt;();
  
          //Iterate through the properties starting at top of soapObject
          for (int i=0; i&lt;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 &amp;&amp; !(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 &amp;&amp; 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 &amp;&amp; !(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 &amp;&amp; currentPropertyObject.getClass().equals(SoapObject.class)) {
                      SoapObject currentPropertySoapObject = (SoapObject) currentPropertyObject;
                      parseComplexSoapObject(currentPropertySoapObject);
                  }
              }
          }
  
          return hashMap;
      }
  </code></pre>
  
  <p><strong>PROBLEMS:</strong></p>
  
  <ol>
  <li><p>Now, the problem is that the <code>HashMap</code> the method returns is created inside this method, so each time the method is recursively called, the <code>HashMap</code> is recreated and the previous data is lost. </p></li>
  <li><p>Even if I create the <code>HashMap</code> outside this method and simple write data to it from the method, the problem will be that there will be no distinction as in which name value pairs have come from which level of nesting (e.g. </p>
  
  <pre><code>&lt;result&gt;
    &lt;id&gt;25323205&lt;/id&gt;
    &lt;name&gt;Cric&lt;/name&gt;
  &lt;result&gt;
  &lt;version&gt;1.0&lt;/version&gt;
  &lt;result&gt;
    &lt;id&gt;445&lt;/id&gt;
  &lt;/result&gt;
  </code></pre></li>
  </ol>
  
  <p>will be stored like <code>HashMap = {id=&gt;25323205, name=&gt;Cric, version=&gt;1.0, id=&gt;445}</code>. I would rather like it this way:</p>
  
  <pre><code>HashMap = {result=&gt;{id=&gt;25323205, name=&gt;Cric}, version=&gt;1.0, result=&gt;{id=&gt;445}}
  </code></pre>
  
  <p>So what can I do about this?</p>