0

I have this format xml from a web service:

<string xmlns="http://someipaddress">
{ "recordcount": "89", 
  "data": 
          [ 
              { "Code": "1", 
                "Name": "Main Store"
              },
              { "Code": "2", 
                "Name": "Alternate Store"
              }
          ]
} </string>

I need to get it in android and convert it into an array to perform many tasks on it.

Is there an android API that will take that xml directly and just give me an array?

I've already worked on receiving this kind of json:

[
    {"id":"36",
     "username":"Simulator"
    },
    {"id":"36",
     "username":"Simulator"
    }
]

and converting it into a JSONArray by using:

JSONArray jsonArray = new JSONArray(contentAsString);

So I was wondering if there is anything similar for XML on Android?

Thx

marciokoko
  • 4,988
  • 8
  • 51
  • 91
  • you can parse the xml using xmlpullparser and then add items parsed to array or list – Raghunandan Feb 01 '14 at 18:16
  • possible duplicate of [Which is the best library for XML parsing in java](http://stackoverflow.com/questions/5059224/which-is-the-best-library-for-xml-parsing-in-java) – Philipp Gayret Feb 01 '14 at 22:06
  • Or http://stackoverflow.com/questions/373833/best-xml-parser-for-java?rq=1 – Philipp Gayret Feb 01 '14 at 22:06
  • I think your first post is a little outdated. Its from 2011 and doesn't mention XMLParser or XMLPullParser. Or are those 2 different libraries? Thx – marciokoko Feb 01 '14 at 22:49

1 Answers1

1

Is there an android API that will take that xml directly and just give me an array?

No. That's because it is not completely XML and not an array. There is an XML root tag, with one text element inside of it, containing a JSON object representation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So what would I have to do? Use the XML pull parser mentioned by Raghunandan? – marciokoko Feb 01 '14 at 21:31
  • 1
    @marciokoko: Well, ideally, you would slap the Web developer with a trout for returning such a bizarre response format. Assuming that the developer then does not fix things, you will want to use an XML parser to retrieve the text node containing the JSON, then use a JSON parser to parse the JSON. – CommonsWare Feb 01 '14 at 21:45
  • LOL! Couldnt I just parse the XML and make a combo of array/dictionary to handle things? I don't need to go through JSON, right? – marciokoko Feb 01 '14 at 22:46
  • @marciokoko: I would recommend that the response either by all XML or all JSON, not some hybrid. – CommonsWare Feb 01 '14 at 22:48
  • Thx, Ill talk to the WebDevs – marciokoko Feb 01 '14 at 22:49