2012-07-30 88 views
0

我想分析如下XML iOS中..我怎樣才能做到這一點解析XML ..NSXMLParsing。與同一節點名稱和不同的屬性值

<RESULTS> 
    <ROW> 
     <COLUMN NAME="NAME">TEST1</COLUMN> 
     <COLUMN NAME="PHONE">4642664</COLUMN> 
     <COLUMN NAME="REMARKS"></COLUMN> 
     <COLUMN NAME="XCOORD_DMS">238558.000038</COLUMN> 
     <COLUMN NAME="YCOORD_DMS">381414.999953</COLUMN> 
     <COLUMN NAME="XCOORD_WGS">51.59917692</COLUMN> 
     <COLUMN NAME="YCOORD_WGS">25.18448963</COLUMN> 
    </ROW> 
    <ROW> 
     <COLUMN NAME="NAME">TEST2</COLUMN> 
     <COLUMN NAME="PHONE">4441303</COLUMN> 
     <COLUMN NAME="REMARKS">تسجيل - ترخيص</COLUMN> 
     <COLUMN NAME="XCOORD_DMS">231930.999982</COLUMN> 
     <COLUMN NAME="YCOORD_DMS">392650.000114</COLUMN> 
     <COLUMN NAME="XCOORD_WGS">51.53369833</COLUMN> 
     <COLUMN NAME="YCOORD_WGS">25.28606306</COLUMN> 
    </ROW> 
</RESULTS> 

感謝

+0

正如你已經猜到,你可以用'NSXMLParser'。你有什麼問題? – user1118321 2012-07-30 18:05:15

+0

你嘗試過的'NSXMLParser'代碼在哪裏?你是否遇到過'تسجيل - ترخيص'的unicode問題?否則,我會投票結束這個問題。 – Joe 2012-07-30 18:10:37

+0

與unicode問題沒有概率..我面臨問題獲取COLUMN標記內的數據。例如,我需要使用SMXMLDocument .. <列NAME =「PHONE」> 4441303 .. im中的電話號碼,他們使用NSXMLPArser – Pras 2012-07-30 18:23:17

回答

0

下面是解析器我寫..

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict { 

    if([elementName isEqualToString:@"RESULTS"]) { 
     //Initialize the array. 
     //appDelegate.maps = [[NSMutableArray alloc] init]; 
    } 
    else if([elementName isEqualToString:@"COLUMN"]) { 

     //Initialize the book. 
     amaps = [[MapDetails alloc] init]; 

     dict = [[NSDictionary alloc] initWithDictionary:attributeDict]; 
     if ([[attributeDict objectForKey:@"NAME"] isEqualToString:@"NAME"]) { 
     } 

    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 

    str123=[NSString stringWithFormat:@"%@",string]; 

} 
// this delegate method is called if an error occurs in locating your current location 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { 

    NSLog(@"STEP 3: Failed!!! Finding location : %@ ", error); 
    if (kCLErrorDenied) { 
    } else { 
     // We handle all non-CoreLocation errors here 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 

    if([elementName isEqualToString:@"RESULTS"]) 
     return; 
    if([elementName isEqualToString:@"COLUMN"]) 
    { 

     amaps.NAME = [NSString stringWithString:str123]; 

     NSLog(@"%@",amaps.NAME); 
     if ([[dict objectForKey:@"NAME"] isEqualToString:@"NAME"]) { 

      [arrName addObject:[NSString stringWithFormat:@"%@",amaps.NAME]]; 

      NSLog(@"arr %@",arrName); 

     } 
     if ([[dict objectForKey:@"NAME"] isEqualToString:@"ANAME"]) { 

      [arrArabName addObject:[NSString stringWithFormat:@"%@",amaps.NAME]]; 

      NSLog(@"arr %@",arrArabName); 

     } 
     if ([[dict objectForKey:@"NAME"] isEqualToString:@"YCOORD_WGS"]) { 
      [arrLatitude addObject:[NSString stringWithFormat:@"%@",amaps.NAME]]; 
      NSLog(@"arr %@",arrLatitude); 
     } 
     if ([[dict objectForKey:@"NAME"] isEqualToString:@"XCOORD_WGS"]) { 
      [arrLongitude addObject:[NSString stringWithFormat:@"%@",amaps.NAME]]; 
      NSLog(@"arr %@",arrLongitude); 
     } 


    } 

}

相關問題