解析器元素与子元素 [英] Parser Element with child element

查看:90
本文介绍了解析器元素与子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有此文档:

<Category>
 <Name>NameParent</Name>
  <Category>
   <Category>
    <Name>NameChild1</Name>
     <Category>
      <Name>NameChild2</Name>
     </Category>
    </Category>
   </Category>
</Category>

这是我的课程:

class Category: NSObject {
    var attributeID:NSString = ""
    var nameCategory:NSString = "nameNotFound"
    var objCategory:Category?
}

如何查看类别是一个递归类.

how to see category is a recursive class.

这些孩子可能又有其他孩子,并且孩子人数不限,我该如何处理? 如何在解析器中构造它?

the children may have, in turn, other children, and have an unlimited number how can I handle this? how can I structure this in the parser?

目前,解析器无法找到父亲和他们的孩子,但我无法将它们很好地保存在列表中.

at this time, the parser can not find the father and their children, but I can not save them well in the list.

谢谢

推荐答案

我将使用某种堆栈来实现它.在每个打开的<Category>上,将一个新元素推入堆栈,然后在每个</Category>上,弹出堆栈中的最后一个元素.

I would implement this with some kind of stack. On each open <Category> push a new element on the stack and on each </Category> pop the last one from the stack.

一种简单的方法是对堆栈使用数组.

One simple approach is to use an array for the stack.

为类别元素创建一个类.解析时,每当didStartElementelementName == "Category"触发时,都会创建该类的新实例,并进行进一步的处理,直到didEndElement触发,再用elementName == "Category"

Create a class for the category elements. While parsing, whenever didStartElement fires with elementName == "Category" create a new instance of that class and do the further processing until didEndElement fires, again with elementName == "Category"

这篇关于解析器元素与子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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