I’m trying to parse a REST response which is in XML. The interesting thing about the response is that multiple nodes have the same name. I’m looking for a simple way to parse this data in iOS. Here’s a sample:
<Report name="BalanceSheet">
<ColDesc><ColTitle>AccountType</ColTitle><ColType>ids_String</ColType></ColDesc>
<ColDesc><ColTitle>Amount</ColTitle><ColType>ids_Amount</ColType></ColDesc>
<Data>
<DataRow>
<ColData>Checking/Savings</ColData>
<ColData>12345345</ColData>
</DataRow>
<DataRow>
<ColData>Accounts Receivable</ColData>
<ColData>674532</ColData>
</DataRow>
<DataRow>
<ColData>Other Current Assets</ColData>
<ColData>423546</ColData>
</DataRow>
<DataRow>
<ColData>Fixed Assets</ColData>
<ColData>63545534</ColData>
</DataRow>
<DataRow>
<ColData>Other Assets</ColData>
<ColData>325465</ColData>
</DataRow>
<DataRow>
<ColData>Accounts Payable</ColData>
<ColData>653653</ColData>
</DataRow>
<DataRow>
<ColData>Other Current Liabilities</ColData>
<ColData>910596.75</ColData>
</DataRow>
<DataRow>
<ColData>Long Term Liabilities</ColData>
<ColData>553797.26</ColData>
</DataRow>
<DataRow>
<ColData>Equity</ColData>
<ColData>45363</ColData>
</DataRow>
</Data>
</Report>
I have tried using the SMXMLDocument parser as well as tried to convert it to a NSDictionary using XMLReader. The way the XML is structured is just confusing.
Any suggestions?
I had already written a simple NSXMLParser example earlier today, so I just took that and made it parse the XML you describe.
Working example project here: https://github.com/erikt/ETParseIntuitSOExample
The NSXMLParser delegate is what you’re asking for. Here’s the header:
And the implementation:
I just printed out the resulting dictionary with NSLog (in the viewDidLoad in the view controller no less … ) and the result is:
I don’t know if you need the column header information from the XML structure, but that should be easy for you to add.