你可以帮助我解决这个程序吗?我尝试用两个级别解析一个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
我无法解释清楚,但我希望你能帮助我,如果你能给我发一个关于这类问题的教程的链接,我会非常感激
答案 0 :(得分:0)
你可以递归地调用解析器。如果你知道你正在寻找什么搜索,如果它可用,并取其值(如果存在)。我建议你研究一下 gdata xml解析器
答案 1 :(得分:0)
问题是因为在您的第二个country
元素上有两个subsidiary
。
所以你需要修改你的代码,如:
您需要另外NSString
来保留Country Name
。因为您的两个subsidiary
只有一个国家/地区名称元素。
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;
}
}