简体   繁体   English

使用NSXMLParser的具有属性的iOS XML

[英]iOS XML with Attributes using NSXMLParser

I want to parse an XML file and want to show the information in an TableView. 我想解析一个XML文件,并想在TableView中显示信息。

At first my XML file: 首先,我的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<machines>
<machine id="1" project="100" name="first" status="inactive" />
<machine id="115" project="101" name="second" status="alive" />
<machine id="252" project="456" name="etc" status="alive" />
</machines>

And here my starting Ocj-C code: 这是我的起始Ocj-C代码:

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

if([elementName isEqualToString:@"machine"]){
    machine =[[Machines alloc]init];
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(@"node content = %@",nodecontent);
}

- (void)parser:(NSXMLParser *)parser didEndElement:...
{
if([elementName isEqualToString:@"machine"]){
    machine.name=nodecontent;
    [tableArray addObject:machine];
    [machine release];
    machine = nil;
}
}
  1. The NSLog for the nodecontent has no output. nodecontent的NSLog没有输出。

  2. How can i save the attibutes of an element? 如何保存元素的外观? (got a class "Machines", here are id, project, name and status) (获得了“机器”类,这是ID,项目,名称和状态)

Hope someone can help me :) 希望可以有人帮帮我 :)

The - (void)parser:didStartElement:namespaceURI:qualifiedName:attributes: gives you this information in a dictionary (see the attributes argument). - (void)parser:didStartElement:namespaceURI:qualifiedName:attributes:在字典中为您提供此信息(请参阅attribute参数)。 It is then up to you to save it off and do with it what you please. 然后由您自行保存并随心所欲地进行处理。

For more information, see: Parsing XML data with NSXMLParser and NSXMLParser Class Reference 有关更多信息,请参见: 使用NSXMLParserNSXMLParser类参考 解析XML数据。

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

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