首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在xcode中解析多级xml

如何在xcode中解析多级xml
EN

Stack Overflow用户
提问于 2013-01-22 16:33:42
回答 2查看 1.2K关注 0票数 0

你能帮我完成这个程序吗?我正在试着用两层解析一个xml文件(我不知道这个术语,所以我这样叫)这是我的xml文件的示例

代码语言:javascript
复制
<?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子公司。

下面是我的解析器类

代码语言:javascript
复制
@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

我不能很好地解释它,但我希望你能帮助我,如果你能给我一个关于这类问题的教程链接,我将非常感激

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-23 16:30:50

这个问题是因为您的第二个country元素上有两个subsidiary。因此,您需要修改代码,如下所示:

您需要另一个用于保存Country NameNSString

代码语言:javascript
复制
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;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-01-22 17:17:03

你可以递归地调用解析器。如果你知道你要找的是什么,搜索它是否可用,如果有,就取它的值。我建议您研究一下gdata xml解析器

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14454401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档