我一直在试着弄清楚如何在可可和目标C中解析这个xml文档。我已经把它撞到墙上几天了。我已经将它加载到NSXMLDocument中,并且能够获得节点。
<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>
使XML文档变得棘手的是每个节点都有几个属性。所以当我去[node XMLString]时,我得到了完整的字符串,这仍然没有意义。它们是从节点中提取单个属性的方法,例如类似于名为getAttribute(String)的java XML方法,它允许我输入字符串参数的属性名称并返回绑定到它的值,示例getAttribute(“Size”)将返回值为4的字符串。
非常感谢任何帮助。
答案 0 :(得分:0)
如果您使用的是NSXMLParser,则可以使用以下NSXMLParserDelegate方法获取XML元素的属性:
- (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"];
}
}