如何为ios开发解析xml [英] how to parse xml for ios development

查看:84
本文介绍了如何为ios开发解析xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道如何解析一些XML结构,但我现在正在尝试解析这个与我习惯的有点不同的特定xml结构。

So I know how to parse some XML structures but I am currently trying to parse this specific xml structure thats a bit different to what I am used to.

通常我会解析像

<xml>
  <data>
    <name>Forrest</name>
    <age>25</name>
    <username>forrestgrant</username>
  </data>
</xml>

但现在我正在使用一些像这样的xml ..

But now I'm working with some xml like so..

<xml>
  <data name="Forrest" age="25" username="forrestgrant" />
  <other value="6" />
</xml>

如果这些变量的结构如何,我该如何访问这些变量?
这就是我通常会接近这个任务的方法,它可以搜索标题标签并从每个标签中获取数据。但是我现在正试图弄清楚如何解析这种其他风格的xml。 / p>

how do I access those variables when they are structured like this? This is how I would normally approach this task, which is baised of searching for the title tags and getting the data from each one.. however I am now trying to figure out how to parse this other style of xml.

- (void)startTheParsingProcess:(NSData *)parserData
{
    [myDataArray release]; // clears array for next time it is used.
    myDataArray = [[NSMutableArray alloc] init]; //initalizes the array

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //incoming parserDatapassed to NSXMLParser delegate which starts parsing process 

    [parser setDelegate:self];
    [parser parse]; //Starts the event-driven parsing operation.
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqual:@"item"]) {
        // NSLog(@"Found title!");
        itemString = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [itemString appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqual:@"item"]) {
        //NSLog(@"ended title: %@", itemString);
        [myDataArray addObject:itemString]; //this is where i pass the values over to my array.

        [itemString release];
        itemString = nil;
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    // Passes myDataArray to the method that will sort and display the array into a uitableview.
    [self startSortingTheArray:myDataArray];
}

任何帮助都将不胜感激..

any help would be greatly appreciated..

推荐答案

上面的xml数据提供了数据作为xml元素的属性。

The xml data above provides the data as attributes on the xml element.

这个回调方法给出了您可以作为键值字典(attributeDict)访问属性。

This callback method gives you access to the attributes as a dictionary of key values (attributeDict).

(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
  qualifiedName:(NSString *)qualifiedName 
  attributes:(NSDictionary *)attributeDict

NS在该方法中输出字典以查看值:

NSLog the dictionary out in that method to see the values:

NSLog(@"attributes: %@", attributeDict);

这篇关于如何为ios开发解析xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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