解析我有這樣一個XML文件:用的NSXMLParser
<name>Alberto</name>
<subname>Martín</subname>
<age>24</age>
我想的NSXMLParser解析器,並將其保存數據:
NSString *name;
NSString *subname;
NSString *age;
要做到這一點即時通訊使用:
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *)
elementName namespaceURI (NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict {
if([elementName isEqualToString:@"name"])
{
if (!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
elementoEncontrado = YES;
}}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string{
if (elementoEncontrado)
{
[soapResults appendString: string];
}
}
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"name"])
{
name=soapResults;
}
if ([elementName isEqualToString:@"subname"])
{
subname=soapResults;
}
if ([elementName isEqualToString:@"age"])
{
age=soapResults;
}
NSLog(@"%@", name, subname, age);
問題是子名和年齡從不出現...
所有這些線來分析一個非常簡單的XML?爲什麼不使用一個叫做TBXML的簡單庫(http://tbxml.co.uk) – Saurabh 2012-01-16 17:53:53