简体   繁体   English

iOS:NSXMLParser-很多属性

[英]iOS: NSXMLParser - A lot of attributes

I know there are a lot of answers here and on google but i am stuck since 3 days figuring out how to handle my problem. 我知道这里和Google上都有很多答案,但是自3天以来我一直在想办法解决我的问题。 I hope someone can give me a good advice. 我希望有人能给我很好的建议。

All the content in my XML are attribute. XML中的所有内容都是属性。 They are parse into attributeDict . 它们被解析为attributeDict Now i am trying to handle the attributes with (void)parser:(NSXMLParser *)parser didStartElement.... and display them later in a TableView. 现在,我尝试使用(void)parser:(NSXMLParser *)parser didStartElement....处理属性,并稍后在TableView中显示它们。

I have put the attributes into NSMutableArray and want to display the device name and his channels and the value of his datapoint. 我已将属性放入NSMutableArray并希望显示设备名称及其通道以及其数据点的值。 Later i would like to change the value of the daterpoint with a button. 后来我想用一个按钮更改daterpoint的值。 But i am not sure if this is the best way for what i am trying. 但是我不确定这是否是我尝试的最佳方法。 I found this post with NSPredicate . 我通过NSPredicate找到了这篇文章。 Could this be the right way? 这是正确的方法吗? Thanks a lot in advance! 在此先多谢!

NSXMLParser delegates Handling Attributes NSXMLParser委托处理属性

This is my code: 这是我的代码:

- (void)parser:(NSXMLParser *)parser didStartElement....

 if(![elementName isEqual:@"device"])
        return;
    Name = [[NSMutableArray alloc] init];
    NSString * name = [attributeDict objectForKey:@"name"];
    [Name addObject:name];

    if(![elementName isEqual:@"device"])
        return;
    Namedatapoint = [[NSMutableArray alloc] init];
    NSString * datapoint = [attributeDict objectForKey:@"ise_id"];
    [Namedatapoint addObject:datapoint];

...

and this a part of my xml: 这是我的xml的一部分:

<stateList>
<device name="Fußbodenheizung" ise_id="1418" unreach="false" sticky_unreach="false" config_pending="false">

<channel name="HM-LC-Sw4-DR MEQ0213526:1" ise_id="1443" visible="true" operate="true">
<datapoint name="BidCos-RF.MEQ0213526:1.STATE" type="STATE" ise_id="1447" value="false" valuetype="2" valueunit="" timestamp="1454260698" operations="7"/>
</channel>
<channel name="HM-LC-Sw4-DR MEQ0213526:2" ise_id="1449" visible="true" operate="true">
<datapoint name="BidCos-RF.MEQ0213526:2.STATE" type="STATE" ise_id="1453" value="false" valuetype="2" valueunit="" timestamp="1454260785" operations="7"/>
</channel>
<channel name="HM-LC-Sw4-DR MEQ0213526:3" ise_id="1455" visible="true" operate="true">
<datapoint name="BidCos-RF.MEQ0213526:3.STATE" type="STATE" ise_id="1459" value="false" valuetype="2" valueunit="" timestamp="1454260786" operations="7"/>
</channel>
<channel name="HM-LC-Sw4-DR MEQ0213526:4" ise_id="1461" visible="true" operate="true">
<datapoint name="BidCos-RF.MEQ0213526:4.STATE" type="STATE" ise_id="1465" value="false" valuetype="2" valueunit="" timestamp="1454260786" operations="7"/>
</channel>
</device>
<device name="HM-CC-RT-DN MEQ0807085" .......

Assuming Name and Namedatapoint are static, you should not alloc init them in parser:didStartElement . 假设NameNamedatapoint是静态的,则不应在parser:didStartElement它们的alloc init parser:didStartElement Doing so will erase previous data each time a new device is found. 这样做会在每次找到新device时删除以前的数据。

If you wan't to keep allocation in parser:didStartElement do it lazily: 如果您不想在parser:didStartElement保留分配,请懒惰地执行此操作:

...
if (!Name) {
  Name = [[NSMutableArray alloc] init];
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM