how to parse xml using libxml2 in iphone

喜夏-厌秋 提交于 2019-12-25 06:37:08

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4614731/how-to-parse-xml-using-libxml2-in-iphone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!