2014-04-09 41 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 
{ 

} 

上面的代码应该为你工作。