如何在 iPhone 中使用 libxml2 解析 xml

发布于 2024-10-11 00:24:15 字数 206 浏览 3 评论 0原文

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

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

但需要时间去解析。

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

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

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

提前谢谢你。

i am getting information in the form of xml from .net web server.

I am using NSXml parser to parse it.

But it takes time to parse.

I hear that libxmll2 is faster than NSXml parser.

But i did n't find clear info regarding how to use it.

can any one pls post sample code or example regarding how to parse using libxml2.

Thank u in advance.

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

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

发布评论

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

评论(2

缱绻入梦 2024-10-18 00:24:15

你需要使用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 库。

You need to use XML parsing to implement this.I suggest use touchXML. Here is a sample code for that:

-(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];
}

Import touchXML library in you header file.

帅冕 2024-10-18 00:24:15

我建议您查看 TouchXML

它很快,因为它使用 libxml2,并且比直接使用 libxml2(用纯 c 编写)要简单得多。

I would suggest you take a look at TouchXML.

It's fast, because it uses libxml2 and it is a lot simpler to use than libxml2 directly, which is written in plain c.

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