前面一篇文章,很好将xml转换成树,并进行操作,但是忽略了对xml节点上属性的操作,现在让我来修改代码,将属性添加进来。
1、在treenode中加一个类型为NSDictionary的attributeDict用于存放属性。代码如下:
NSDictionary * attributeDict;
2、在中可以在parser:didStartElement:方法中取到属性列表,在其中添加添加下面代码。
leaf.attributeDict = [[NSDictionary alloc] initWithDictionary:attributeDict];
3、修改样例xml。
<?xml version="1.0" encoding="UTF-8"?>
<Login>
<LoginResult id="1">True</LoginResult>
<LoginInfo>OK</LoginInfo>
<LastLogin>2011-05-09 12:20</LastLogin>
<Right>
<A>1</A>
<B>1</B>
<C>0</C>
</Right>
</Login>
4、取属性id的值。
TreeNode * resultTreeNode = [node objectForKey:@"LoginResult"];
NSString *result = [resultTreeNode.attributeDict objectForKey:@"id"];