解析器元素与子元素

问题描述:

例如,我有此文档:

<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"