views:

247

answers:

2

Hi. I have the following XML (doing an app for iPhone):

<Row>
<Field name="employee_id_disp">00070431</Field>
<Field name="given_name">John</Field>
<Field name="family_name">Doe</Field>
</Row> ...

How can I retrieve values only for one of the attributes, for example value "John" for attribute name="given_name" ?

Thanks for answers.

+2  A: 

Presumably, you'll be using NSXMLParser to parse this.

That means in your didStartElement: delegate callback, you should check the passed attributes dictionary to see if it has a key "name" with a value "given_name". If it does, you'll want to set some flag that you can start recording the characters found in foundCharacters: callback. Once you reach the didEndElement: callback for the element "Field", you'll have aggregated all the characters into a string, and that string is the name. (Most likely, the characters will all come be reported in one callback to foundCharacters:, but that's not guaranteed).

Dave DeLong
A: 

Easiest way: use an XPath processor and evaluate for the expression:

"/Row/Field[@name='given_name']/text()"

It should return "John" as the result. You can try it in this simulator here: http://www.mizar.dk/XPath/

Here's a simple way to get access to the XPath that comes with libxml2: http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

And a good resource for finding which XML parsers have XPath support: http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

Ramin
@Dave, @Ramin - thank you for your answers.
mitb67