[英]how to parse xml using libxml2 in iphone
我正在从.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库。
我建议您看一下TouchXML 。
速度很快,因为它使用libxml2,并且比直接用libc2(用纯c语言编写)直接使用要简单得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.