ios开发如何解析xml

发布于 2024-12-09 08:52:23 字数 2108 浏览 0 评论 0原文

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

通常我会解析类似的内容

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

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

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

当这些变量的结构如下时,我如何访问它们? 这就是我通常处理此任务的方式,它是基于搜索标题标签并从每个标签获取数据......但是我现在试图弄清楚如何解析这种其他样式的 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];
}

任何帮助将不胜感激..

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.

normally I would parse something like

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

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

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

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-12-16 08:52:23

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

此回调方法使您能够以键值字典 (attributeDict) 的形式访问属性。

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

在该方法中 NSLog 字典以查看值:

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

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

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

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

NSLog(@"attributes: %@", attributeDict);
奈何桥上唱咆哮 2024-12-16 08:52:23

didStartItem: 方法中,attributes 字典将包含所有 XML 属性的值。

In your didStartItem: method, the attributes dictionary will contain values for all the XML attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文