查找XML(SVG)子元素

时间:2014-03-16 18:11:54

标签: ios objective-c xml svg nsxmlparser

我有以下SVG

<g id="background">
    <rect style="fill:#2A6683;" width="768" height="1024"/>
</g>
<g id="wall">
    <rect x="352" y="457" style="fill:#25436B;" width="64" height="64"/>
</g>
<g id="hole">
    <circle style="fill:#25436B;" cx="384" cy="73.06" r="32"/>
</g>
<g id="dot">
    <circle style="fill:#FFFFFF;" cx="384" cy="945.557" r="32"/>
</g>

我想做的是查看g element id所以我知道它是什么类型的对象然后 在child element元素的g处,以了解对象的属性。 我知道如何使用NSXMLParser解析svg文件,问题是我不知道如何参考g元素来访问子元素。

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

    if ([elementName isEqualToString:@"g"]) {
        NSString *object = [attributeDict valueForKey:@"id"];

        if ([object isEqualToString:@"wall"]) {
            // look at the child element for position value etc...
        } else if ([object isEqualToString:@"dot"])  {
           // look at the child element for position value etc...
        }else if ([object isEqualToString:@"hole"]) {
          // look at the child element for position value etc...
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

这应该有效,或者至少对我有用。

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

  if ([elementName isEqualToString:@"g"]) {
    NSString *object = [attributeDict valueForKey:@"id"];

    if ([object isEqualToString:@"wall"]) {
      int width = [attributeDict valueForKey:@"width"];
      int height = [attributeDict valueForKey:@"height"];
    } else if ([object isEqualToString:@"dot"])  {
      int width = [attributeDict valueForKey:@"width"];
      int height = [attributeDict valueForKey:@"height"];
      // look at the child element for position value etc...
    }else if ([object isEqualToString:@"hole"]) {
      int width = [attributeDict valueForKey:@"width"];
      int height = [attributeDict valueForKey:@"height"];
      // look at the child element for position value etc...
    }
  }

}