NSXMLParser - 捕获父子信息?苹果手机 [英] NSXMLParser - Capturing Parent and Child Information ? iPhone

查看:45
本文介绍了NSXMLParser - 捕获父子信息?苹果手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有父子结构的 xml 文件,例如:

I have an xml file with a parent child structure such as :

<geometry id="001-mesh" name="001">
  <mesh>
    <source id="001-mesh-positions">
      <float_array id="001-mesh-positions-array" count="228">

我能够使用 NSXMLParser 成功解析数据.然而问题是我想存储与父级相关的子信息 - 例如,当检测到几何 ID 时,我想存储关联的源 ID 和 float_array id.

I am able to parse the data successfully using NSXMLParser. However the problem is I want to store the child information in relation to the parent - for example when a geometry id is detected, I then want to store the associated source id and float_array id.

谁能建议我这样做的方法?下面的代码将检测在解析过程中何时找到给定元素,但我不确定如何存储这些值.

Can anyone suggest a way I could this ? The below code will detect when a given element is found during parsing, but I'm not sure how to then store the values.

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

// NSLog(@"Parse Started");
//NSLog(@"Element Name is %@", elementName);

if([elementName isEqualToString:@"geometry"]) { 
    NSLog(@"Object Detected");
    NSString *name = [attributeDict objectForKey:@"name"];

NSLog(@"Name is %@",name);
}

if ([elementName isEqualToString:@"float_array"]) {
    NSLog(@"Vertices Detected");
    NSString * vertices = [attributeDict objectForKey:@"count"];

    NSLog(@"Vertices are %@", vertices);
}

}

推荐答案

解析器检测到geometry标签时,source id和float_array id不可用:它们还没有被解析然而.通常的技巧是等待子元素,并在它可用时进行处理.您可以通过创建一个新的 Geometry 对象(或任何与模型中 XML 的 geometry 标记相对应的对象)来实现,并使其可用于 didStartElement:....: 方法,例如将其设置为名为 myGeometry 的 ivar.一旦你检测到 float_array 元素,解析它的 id,然后调用

At the time when the parser detects the geometry tag, the source id and float_array id are not available: they have not been parsed yet. The usual trick is to wait for the child element, and do the processing when it becomes available. You can do it by creating a new Geometry object (or whatever corresponds to XML's geometry tag in your model), and make it available to the didStartElement:...: method, for example by setting it into an ivar called myGeometry. Once you detect float_array element, parse its id, and call

[myGeometry setId:floatArrayId];

这篇关于NSXMLParser - 捕获父子信息?苹果手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆