首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iphone中使用libxml2解析xml

如何在iphone中使用libxml2解析xml
EN

Stack Overflow用户
提问于 2011-01-06 20:07:01
回答 2查看 1.9K关注 0票数 0

我正在从.net web服务器获取xml格式的信息。

我正在使用NSXml解析器来解析它。

但它需要时间来解析。

我听说libxmll2比NSXml解析器快。

但是我没有找到关于如何使用它的明确信息。

谁可以发布关于如何使用libxml2解析的示例代码或示例。

提前谢谢你。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-06 20:23:47

您需要使用XML解析来实现这一点,我建议使用touchXML。下面是一个示例代码:

代码语言:javascript
复制
-(void)callwebservice{
    NSString *path = @"YOUR URL";
    [self grabRSSFeed:path];
}

-(void) grabRSSFeed:(NSString *)blogAddress {
    // Initialize the blogEntries MutableArray that we declared in the header
    blogEntries = [[NSMutableArray alloc] init];    

    // Convert the supplied URL string into a usable URL object
    NSURL *url = [NSURL URLWithString: blogAddress];

    // Create a new rssParser object based on the TouchXML "CXMLDocument" class, this is the object that actually grabs and processes the RSS data
    CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];

    // Create a new Array object to be used with the looping of the results from the rssParser
    NSArray *resultNodes = NULL;

    // Set the resultNodes Array to contain an object for every instance of an  node in our RSS feed
    resultNodes = [rssParser nodesForXPath:@"//Node you want to parse" error:nil];

    // Loop through the resultNodes to access each items actual data
    for (CXMLElement *resultElement in resultNodes) {
        // Create a temporary MutableDictionary to store the items fields in, which will eventually end up in blogEntries
        NSMutableDictionary *blogItem = [[NSMutableDictionary alloc] init];

        // Create a counter variable as type "int"
        int counter;

        // Loop through the children of the current  node
        for(counter = 0; counter < [resultElement childCount]; counter++) {
            // Add each field to the blogItem Dictionary with the node name as key and node value as the value
            [blogItem setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
            NSLog(@"Data = %@",[[resultElement childAtIndex:counter] stringValue]); 
        }

        // Add the blogItem to the global blogEntries Array so that the view can access it.
        [blogEntries addObject:[blogItem copy]];

    }

    [YourTable reloadData];
}

在头文件中导入touchXML库。

票数 1
EN

Stack Overflow用户

发布于 2011-01-06 20:21:25

我建议你看看TouchXML

它很快,因为它使用libxml2,而且比直接用普通的c编写的libxml2简单得多。

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

https://stackoverflow.com/questions/4614731

复制
相关文章

相似问题

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