2014-04-09 3 views
0

XML 파일이 있습니다. 이것의 일부입니다.iOS에서 XML 구문 분석하기 : 태그

<Placemark> 
    <kml:name xmlns:kml="http://www.opengis.net/kml/2.2">Placename</kml:name> 
    <kml:description xmlns:kml="http://www.opengis.net/kml/2.2"> 
    </kml:description> 
    <kml:Point xmlns:kml="http://www.opengis.net/kml/2.2"> 
     <kml:coordinates>121.142122637505,22.9071362429957,0</kml:coordinates> 
    </kml:Point> 
    <styleUrl>#ylwPng</styleUrl> 
</Placemark> 

는 그리고 이것은 내 코드

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict 
{ 
NSString *ident = [attributeDict objectForKey:@"id"]; 

KMLStyle *style = [_placemark style] ? [_placemark style] : _style; 

// Style and sub-elements 
if (ELTYPE(Style)) { 
    if (_placemark) { 
     [_placemark beginStyleWithIdentifier:ident]; 
    } else if (ident != nil) { 
     _style = [[KMLStyle alloc] initWithIdentifier:ident]; 
    } 
} else if (ELTYPE(PolyStyle)) { 
    [style beginPolyStyle]; 
} else if (ELTYPE(LineStyle)) { 
    [style beginLineStyle]; 
} else if (ELTYPE(color)) { 
    [style beginColor]; 
} else if (ELTYPE(width)) { 
    [style beginWidth]; 
} else if (ELTYPE(fill)) { 
    [style beginFill]; 
} else if (ELTYPE(outline)) { 
    [style beginOutline]; 
} 
// Placemark and sub-elements 
else if (ELTYPE(Placemark)) { 
    _placemark = [[KMLPlacemark alloc] initWithIdentifier:ident]; 
} else if (ELTYPE(Name)) { 
    [_placemark beginName]; 
} else if (ELTYPE(Description)) { 
    [_placemark beginDescription]; 
} else if (ELTYPE(styleUrl)) { 
    [_placemark beginStyleUrl]; 
} else if (ELTYPE(Polygon) || ELTYPE(Point) || ELTYPE(LineString)) { 
    [_placemark beginGeometryOfType:elementName withIdentifier:ident]; 
} 
// Geometry sub-elements 
else if (ELTYPE(coordinates)) { 
    [_placemark.geometry beginCoordinates]; 
} 
// Polygon sub-elements 
else if (ELTYPE(outerBoundaryIs)) { 
    [_placemark.polygon beginOuterBoundary]; 
} else if (ELTYPE(innerBoundaryIs)) { 
    [_placemark.polygon beginInnerBoundary]; 
} else if (ELTYPE(LinearRing)) { 
    [_placemark.polygon beginLinearRing]; 
} 
} 

입니다하지만 내 코드가 작동하지 않습니다. 내 코드는 다음 XML 만 성공적으로 구문 분석 할 수 있습니다.

 <Placemark> 
     <name>Placename/name> 
     <description></description> 
     <styleUrl>#ylwPng</styleUrl> 
     <Point> 
     <coordinates>121.142122637505,22.90713624299571,0</coordinates> 
     </Point> 
    </Placemark> 

XML에 맞게 변경하려고 시도하고 있지만 어떻게해야할지 모르겠다.

답변

1

요소 이름이나 문자열 객체를 기억하려면 하나의 인스턴스 변수를 추가해야합니다. 예를 들어, 요소 이름을 저장하는 NSString *_currentElement;을 추가 할 수 있습니다, 당신은 아래 NSXMLParserDelegate에서 사용 :

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    _currentElement = elementName; 
    if ([elementName isEqualToString:@"kml:name"]) { 
     NSLog(@"url:%@",attributeDict[@"xmlns:kml"]); 
    } 
    else if ([elementName isEqualToString:@"kml:description"]) 
    { 
     NSLog(@"url:%@",attributeDict[@"xmlns:kml"]); 
    } 
    else if ([elementName isEqualToString:@"kml:Point"]) 
    { 
     NSLog(@"url:%@",attributeDict[@"xmlns:kml"]); 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    if ([_currentElement isEqualToString:@"kml:coordinates"]) { 
     NSLog(@"Coordinates:%@",string); 
    } 
    else if ([_currentElement isEqualToString:@"styleUrl"]) 
    { 
     NSLog(@"Style:%@",string); 
    } 
} 

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

} 

위의 조각은 당신을 위해 작동합니다.

관련 문제