使用XMLParser访问正确的XML节点

时间:2013-01-04 17:57:00

标签: objective-c ios xml xml-parsing nsxmlparser

我无法访问XML的节点:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
      <dict>
        <key>cnt_id</key>
        <string>lovenyc</string>
        <key>user</key>
        <string></string>
        <key>emps_cnpj</key>
        <string>11379404000154</string>
        <key>depto_id</key>
        <string>0</string>
        <key>id_device</key>
        <string>7543AF5F-495D-4739-BB83-56BF08CE7DF0</string>
        <key>data_acesso</key>
        <string>2013-01-04 12:48:21</string>
        <key>id_usuario</key>
        <string></string>
        <key>user_fav</key>
        <string>0</string>
        <key>user_like</key>
        <string>0</string>
        <key>user_lat</key>
        <string></string>
      </dict>
    </array>
 </plist>

目标-c代码:

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)
namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *)attributeDict
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
    _xmlObjects = [[NSMutableArray alloc]init];

    NSLog(@"Attribute %@",[attributeDict allKeys]);
    NSLog(@"Attribute Values %@",[attributeDict allValues]);
}

日志总是给我空值,我无法访问<string>值,例如,任何建议? 感谢

1 个答案:

答案 0 :(得分:2)

使用Foundation对象解析属性列表是一项简单的单行任务:

NSArray *array = [NSArray arrayWithContentsOfFile:@"PropList.plist"];

documentation中的更多信息。

(请注意,您必须使用正确的类 - 如果属性列表的根对象是<dict>,那么您应该使用+ [NSDictionary dictionaryWithContentsOfFile:]代替。)