#pragma mark -
#pragma mark user code
- (void) parseXML:(UIButton *) button
{
NSString *XMLPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"userInfo.xml"
];
NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath];
CXMLDocument *document = [[CXMLDocument alloc]initWithData:XMLData
options:0
error:nil
];
//[self parseDire:document];
[self parseRoot:document];
}
//方式一:直接查找
- (void) parseDire:(CXMLDocument *) document
{
NSArray *users = NULL;
users = [document nodesForXPath:@"//user" error:nil];
for (CXMLElement *element in users)
{
if ([element isKindOfClass:[CXMLElement class]])
{
NSMutableDictionary *item = [[NSMutableDictionaryalloc] init];
for (int i = 0; i < [element childCount]; i++)
{
if ([[[element children] objectAtIndex:i]isKindOfClass:[CXMLElement class]])
{
[item setObject:[[element childAtIndex:i]stringValue]
forKey:[[element childAtIndex:i] name]
];
}
}
NSLog(@"%@", item);
}
}
}
//方式二:从根节点查找
- (void) parseRoot:(CXMLDocument *) document
{
CXMLElement *root = [document rootElement];
NSArray *users = [root children];
for (CXMLElement *element in users)
{
if ([element isKindOfClass:[CXMLElement class]])
{
NSMutableDictionary *item = [[NSMutableDictionaryalloc] init];
for (int i = 0; i < [element childCount]; i++)
{
if ([[[element children] objectAtIndex:i]isKindOfClass:[CXMLElement class]])
{
[item setObject:[[element childAtIndex:i]stringValue]
forKey:[[element childAtIndex:i] name]
];
}
}
NSLog(@"%@", item);
}
}
}
|