I have a simple XML and I need to get the first ‘id‘ from puid-list. I found lots of examples but none of them quite do that because of the namespace. How do get the id out as an NSString?
PS: I’m on a Mac.
<genpuid songs="1" xmlns:mip="http://musicip.com/ns/mip-1.0#">
<track file="/htdocs/test.mp3" puid="0c9f2f0e-e72a-c461-9b9a-e18e8964ca20">
<puid-list>
<puid id="0c9f2f0e-e72a-c461-9b9a-e18e8964ca20"/>
</puid-list>
</track>
</genpuid>
You should use
NSXMLParser. Create an instance in your code and tell it to parse:then you need to implement the methods of
NSXMLParserDelegateto get callbacks during parsing:Notes on handling namespaces:
If the namespaces are set,
elementNameis the qualified name, so could have prefix (e.g.mip:puid) If you’re on the Mac,NSXMLNodehas to convenience class methods,localNameForName:andprefixForName:to get the two parts of the string.Also, You might want the
NXXMLParserdelegate methodsparser:didStartMappingPrefix:toURI:andparser:didEndMappingPrefix:.Note that names returned by
NSXMLParserare exactly those from the string (regarding whether they’re prefixed). So it’s rare for the attribute to be namedmip:id, but if you want to be robust, you need to check for this as well.