ios - 如何在 iPhone 中使用 libxml2 解析 xml

标签 ios libxml2 nsxml

我从 .net Web 服务器获取 xml 形式的信息。

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

但是解析需要时间。

我听说 libxmll2 比 NSXml 解析器更快。

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

任何人都可以发布有关如何使用 libxml2 进行解析的示例代码或示例。

提前谢谢你。

最佳答案

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

-(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 库。

关于ios - 如何在 iPhone 中使用 libxml2 解析 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4614731/

相关文章:

ios - 具有相同原型(prototype)单元的两个 TableView,约束行为不同

iOS:滑动手势在同一 Controller 内加载不同的数据

c - libxml2 生成器(从 xsd 到 C 源代码)

xslt - 如何从 xsltproc 输出 iso-8859-1 字符集?

iphone - NSXMLParser问题带有“&”(Ampersand)字符

ios - NSMutableArray 只有最后一个对象的副本

ios - 如何以编程方式 "like"iOS 应用程序中的 Facebook 页面?

c - 如何获取c源目录而不是cmake编译目录

ios - NSXMLparser 在 IOS swift 中具有基本身份验证

ios - 发布时 Azure iOS SDK 错误