使用 NSXMLParser 解析复杂的 XML 结构 [英] parsing complex XML structure using NSXMLParser

查看:58
本文介绍了使用 NSXMLParser 解析复杂的 XML 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的服务器返回了这个相当复杂的 XML,我需要一些帮助来将它解析为一个我可以访问以供以后使用的对象.

I have this fairly complex XML being returned from my server, I would like some help parsing it into a object I can acess for later use.

这是xml的样子

<Eng>
    <Result         >
        <Series         >
            <Link/>
            <FMF/>
                <AlsoLink/>
                <Plugins/>
            </FMF>
            <Sheet          >
                <Spaces>
                    <Spacing            >
                    <Names/>
                    </Spacing>
                    <Spacing            >
                    <Names/>
                    </Spacing>
                </Spaces>
            </Sheet>
        </Series>
    </Result>
</Eng>

然后我使用 NSXMLParser 尝试将 xml 中每个元素的所有ObjectForKey/s"解析到他们自己的字典中..如果有意义的话.

I am then using NSXMLParser to try and parse all of the "ObjectForKey/s" of each element in the xml into their own dictionarys.. if that makes sense.

这是我的代码目前的样子

This is what my code is currently looking like

#pragma mark - Parsing lifecycle

- (void)startTheParsingProcess:(NSData *)parserData
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
    [parser setDelegate:self];
    [parser parse]; // starts the event-driven parsing operation.
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"Series"]){
        parsedMutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:attributeDict];
        self.parsedDataArrayOfDictionaries = [NSMutableArray arrayWithCapacity:8];

        if ([elementName isEqualToString:@"Link"]) {

        }
        if ([elementName isEqualToString:@"FMF"]) {
            if ([elementName isEqualToString:@"AlsoLink"]) {

            }
            else if ([elementName isEqualToString:@"Plugins"]) {

            }
        }
        else if ([elementName isEqualToString:@"Sheet"]) {

            if ([elementName isEqualToString:@"Spaces"]) {

                if ([elementName isEqualToString:@"Spacing"]) {

                    if ([elementName isEqualToString:@"Names"]) {

                    }
                }
            }

        }
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{    
    if([elementName isEqualToString:@"Series"]){
         [parsedDataArrayOfDictionaries addObject:parsedMutableDictionary];

        if ([elementName isEqualToString:@"Link"]) {

        }
        if ([elementName isEqualToString:@"FMF"]) {
            if ([elementName isEqualToString:@"AlsoLink"]) {

            }
            else if ([elementName isEqualToString:@"Plugins"]) {

            }
        }
        else if ([elementName isEqualToString:@"Sheet"]) {

            if ([elementName isEqualToString:@"Spaces"]) {

                if ([elementName isEqualToString:@"Spacing"]) {

                    if ([elementName isEqualToString:@"Names"]) {

                    }
                }
            }

        }
    }


//     parsedMutableDictionary = nil;
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"Paser Error = %@", parseError);
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A parsing failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [errorAlert show];
}


//TODO: Look into how to use this method over multiple views. i.e.(other requests such as keycode, advanced )
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    // display values
        NSArray *filteredArray = parsedDataArrayOfDictionaries;
        NSLog(@"%@", filteredArray);

}

我遇到的问题是我不明白我应该在他们自己的对象中拥有什么......而且当其中一些值返回时,我可能有多个 Spacing 元素,我只是不知道如何处理......

The problem I am having is that I dont understand what I should have in their own objects... also when some of these values return I could have multiple Spacing elements and I just dont know how to handle that...

如果我没有具体说明什么,请提供任何帮助或提示,因为我的大脑正在努力解决这个问题.

Any help or tips would be hugely appreciated, if I havent specified something just ask as my brain is just struggling to wrap itself around this problem.

谢谢.

推荐答案

假设您的 XML 文件....

Suppose your XML file....

<Series element1="something" element2="something">
    <Spaces>
         <Spacing>
              <Names>
                 Something
              </Names>
         </Spacing>
         <Spacing>
              <Names>Something</Names>
         </Spacing>
    </Spaces>
</Series>

要获取 element1 的值,您必须执行.....

To get the value of element1 you have to do.....

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{   
    if([currentElement isEqualToString:@"Series"]) {
        NSLog(@"%@",[attributeDict objectForKey:@"element1"]);
    }
}

要获得 Spacing 的多个值,您必须....

To get the multiple values of Spacing you have to do....

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"name"]) {
       [currentSpacingName appendString:string];
       [currentSpacingName appendString:@"any character"]
    }
}

在此方法中将值存储到具有期望键的字典中...

After that store the value into a dictionary with desire key in this method...

 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

这只是一个例子,我希望你现在可以解决你的问题.

this is just an example i hope now you could solve your problem.

这篇关于使用 NSXMLParser 解析复杂的 XML 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆