NSXMLParsing ..解析具有相同节点名和不同属性值的xml

时间:2012-07-30 18:03:07

标签: objective-c ios nsxmlparser

我想在iOS中解析下面的xml ..我该怎么做..

<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>

由于

1 个答案:

答案 0 :(得分: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);
        }


    }

}