[英]How to parse multilevel xml in xcode
你能用这个程序帮我吗,我试图解析一个具有两个级别的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
我无法很好地解释它,但我希望您能为我提供帮助,如果您可以将有关此类问题的教程链接发送给我,我将不胜感激
好了,您可以递归调用解析器。 如果您知道要查找的内容,则进行搜索,如果存在,则取其值。 我建议您研究一下gdata xml解析器
问题是因为在您的第二个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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.