2011-01-06 7 views
0

.net 웹 서버에서 XML 형식으로 정보를 얻고 있습니다.iphone에서 libxml2를 사용하여 xml을 구문 분석하는 방법

구문 분석에 NSXml 파서를 사용하고 있습니다.

하지만 구문 분석하는 데 시간이 걸립니다.

libxmll2가 NSXml 파서보다 빠르다고 들었습니다.

그러나 사용 방법과 관련하여 명확한 정보를 찾지 못했습니다.

샘플 코드 또는 libxml2를 사용하여 구문 분석하는 방법에 대한 예제를 게시 할 수 있습니다.

미리 감사드립니다.

답변

1

이것을 구현하려면 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 라이브러리 가져 오기.

+0

비누 조치와 XML 문자열을 사용하여 전달하는 방법이 URL에 줄 수있는 방법은 무엇입니까? – MaheshBabu

+0

서버에서 데이터를 구문 분석 할 URL을 생성해야합니다. – Aditya

0

TouchXML을 살펴 보시기 바랍니다.

libxml2를 사용하기 때문에 빠르며 libxml2보다 훨씬 간단하게 사용할 수 있습니다.이 파일은 일반 c로 작성됩니다.

관련 문제