你能帮我完成这个程序吗?我正在试着用两层解析一个xml文件(我不知道这个术语,所以我这样叫)这是我的xml文件的示例
<?xml version="1.0" encoding="UTF-8"?>
<countries>
<country>
<countryname>Philippines</countryname>
<subsidiaries>
<subsidiary>
<name>Sart Philippines Inc.</name>
<address>Unit 10-A The World Empire Building, 330 Senator Gil Puyat Avenue Philippines, Philippines</address>
<phone>+63.2.929</phone>
<fax>+63.932</fax>
<email>enquiry.philippines@sarts.com</email>
<website>http://www.sartorius-mechatronics.com.ph</website>
</subsidiary>
</subsidiaries>
</country>
<country>
<countryname>Denmark</countryname>
<subsidiaries>
<subsidiary>
<name>Stedim Nordic A|S</name>
<address>Hoerskaetten 6d 2630 Taastrup, Denmark</address>
<phone>+45.7023.4400</phone>
<fax>+45.4630.4030</fax>
<email>ne.customersupport@sartorius.com</email>
<website></website>
</subsidiary>
<subsidiary>
<name>Nordic A|S</name>
<address>Hoerskaetten 6D 2630 Taastrup, Denmark</address>
<phone>+45.7523.8700</phone>
<fax>+45.4130.4020</fax>
<email>ne.customersupport@sartorius.com</email>
<website></website>
</subsidiary>
</subsidiaries>
</country>
</countries>我计划做的是你选择一个国家,然后你选择一个子公司来查看它的信息。我能够解析它,但我无法显示Stedim Nordic A|S子公司。
下面是我的解析器类
@implementation Parser
-(id)initParser
{
if (self == [super init])
{
app = (AppDelegate *) [[UIApplication sharedApplication]delegate];
}
return self;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"countries"])
{
app.listArray = [[NSMutableArray alloc]init];
}
if ([elementName isEqualToString:@"country"])
{
theList = [[List alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!currentElementValue)
{
currentElementValue = [[NSMutableString alloc]initWithString:string];
}
else
{
[currentElementValue appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"country"])
{
[app.listArray addObject:theList];
}
else {
if ([elementName isEqualToString:@"countryname"]) {
theList.countryname = currentElementValue;
}
if ([elementName isEqualToString:@"name"])
{
theList.name = currentElementValue;
}
if ([elementName isEqualToString:@"address"])
{
theList.address = currentElementValue;
}
if ([elementName isEqualToString:@"phone"])
{
theList.phone = currentElementValue;
}
if ([elementName isEqualToString:@"fax"])
{
theList.fax = currentElementValue;
}
if ([elementName isEqualToString:@"email"])
{
theList.email = currentElementValue;
}
if ([elementName isEqualToString:@"website"])
{
theList.website = currentElementValue;
}
currentElementValue = nil;
}
}
@end我不能很好地解释它,但我希望你能帮助我,如果你能给我一个关于这类问题的教程链接,我将非常感激
发布于 2013-01-23 16:30:50
这个问题是因为您的第二个country元素上有两个subsidiary。因此,您需要修改代码,如下所示:
您需要另一个用于保存Country Name的NSString。
NSString *temp = nil;
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"countries"])
{
app.listArray = [[NSMutableArray alloc]init];
}
if ([elementName isEqualToString:@"subsidiary"])
{
theList = [[List alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"subsidiary"])
{
theList.countryname = temp;
[app.listArray addObject:theList];
}
else
{
if ([elementName isEqualToString:@"countryname"])
{
temp = currentElementValue;
}
if ([elementName isEqualToString:@"name"])
{
theList.name = currentElementValue;
}
if ([elementName isEqualToString:@"address"])
{
theList.address = currentElementValue;
}
if ([elementName isEqualToString:@"phone"])
{
theList.phone = currentElementValue;
}
if ([elementName isEqualToString:@"fax"])
{
theList.fax = currentElementValue;
}
if ([elementName isEqualToString:@"email"])
{
theList.email = currentElementValue;
}
if ([elementName isEqualToString:@"website"])
{
theList.website = currentElementValue;
}
currentElementValue = nil;
}
}发布于 2013-01-22 17:17:03
你可以递归地调用解析器。如果你知道你要找的是什么,搜索它是否可用,如果有,就取它的值。我建议您研究一下gdata xml解析器。
https://stackoverflow.com/questions/14454401
复制相似问题