I'm retrieving data from this XML:
<appMetrics versionName="1.1.0" startDate="2013-04-16" metric="ActiveUsersByDay" groupBy="dayOfYear" endDate="2013-04-18" country="US" version="1.0" generatedDate="4/19/13 10:10 AM">
<day value="6" date="2013-04-16"/>
<day value="4" date="2013-04-17"/>
<day value="2" date="2013-04-18"/>
</appMetrics>
I have been successful with simpler XMLs like if it was like this instead:
<appMetrics versionName="1.1.0" startDate="2013-04-16" metric="ActiveUsersByDay" groupBy="dayOfYear" endDate="2013-04-18" country="US" version="1.0" generatedDate="4/19/13 10:10 AM">
<day value="6"/>
<day value="4"/>
<day value="2"/>
</appMetrics>
How do I extract both the day value and date under the same single element? What i've done in the past is just search for an element then copy the string, but there are multiple pieces so it doesnt work. Do I have to make it an array instead? Or is there a way to just take the day value and date separately?
You should use NSXMLParser
. In its delegate callback
parser:didStartElement:namespaceURI:qualifiedName:attributes:
you can find all the attributes (like value
, date
) in the attributes dictionary.
Edit
Parsing is really easy. Just create a parser with your data, set self as delegate and start parsing. Maintain some mutable array where you read everything in.
See the NSXMLParser Reference and NSXMLParserDelegate protocol.
If you need more help, go for this question with many links to tutorials.