質問

I have been hitting my head into the wall for a few days now trying to figure out how to parse this xml document in cocoa and objective C. I have loaded it into NSXMLDocument, and have been able to get the Nodes.

<MediaContainer noHistory="0" replaceParent="0" size="4" identifier="com.plexapp.system">
<Directory key="aHR0cDovLzE5Mi4xNjguMS45OTozMjQwMC9saWJyYXJ5L3NlY3Rpb25zLzI_" art="2/art?updatedAt=1344035442" unique="1" name="3D Movies" title="3D Movies" type="movie" serverVersion="0.9.6.7.204-266f05d" host="192.168.1.99" serverName="Eddard" path="/library/sections/2" machineIdentifier="2dddaaedb46a722cf74695c19a7d3947bbeebfd2" local="1" port="32400"/>
<Directory key="aHR0cDovLzE5Mi4xNjguMS45OTozMjQwMC9saWJyYXJ5L3NlY3Rpb25zLzE_" art="1/art?updatedAt=1344035443" unique="1" name="Movies" title="Movies" type="movie" serverVersion="0.9.6.7.204-266f05d" host="192.168.1.99" serverName="Eddard" path="/library/sections/1" machineIdentifier="2dddaaedb46a722cf74695c19a7d3947bbeebfd2" local="1" port="32400"/>
<Directory key="aHR0cDovLzE5Mi4xNjguMS45OTozMjQwMC9saWJyYXJ5L3NlY3Rpb25zLzQ_" art="4/art?updatedAt=1344035444" unique="1" name="Super Audio CDs" title="Super Audio CDs" type="movie" serverVersion="0.9.6.7.204-266f05d" host="192.168.1.99" serverName="Eddard" path="/library/sections/4" machineIdentifier="2dddaaedb46a722cf74695c19a7d3947bbeebfd2" local="1" port="32400"/>
<Directory key="aHR0cDovLzE5Mi4xNjguMS45OTozMjQwMC9saWJyYXJ5L3NlY3Rpb25zLzM_" art="3/art?updatedAt=1344037454" unique="1" name="TV Shows" title="TV Shows" type="show" serverVersion="0.9.6.7.204-266f05d" host="192.168.1.99" serverName="Eddard" path="/library/sections/3" machineIdentifier="2dddaaedb46a722cf74695c19a7d3947bbeebfd2" local="1" port="32400"/>
</MediaContainer>

What makes the XML document tricky is that each node has several attributes. So when I go [node XMLString] I get back the full string which is still meaningless. Is their a way to pull a single attribute from the node, for example something similar to the java XML method called getAttribute(String) which would allow me to put in the attribute name for the string argument and return the value bound to it, for example getAttribute("Size") would return a string with value of 4.

Any help would be greatly appreciated.

役に立ちましたか?

解決

If you are using a NSXMLParser you can use the following NSXMLParserDelegate method to get the attributes of a XML element:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
   if ([elementName isEqualToString:@"MediaContainer"]) {
      NSString *size = [attributeDict objectForKey:@"size"];
   }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top