2011-05-21 75 views
7

我有其中包含了一些数据,我想用一个XML文件:解析XML文件 - 获取值

<?xml version="1.0" encoding="UTF-8" ?> 

<items> 
<item name="product" price="19.95" where="store"> 
This is the first product. 
</item> 

<item name="product2" price="39.95" where="online"> 
This is the second product. 
</item> 

<item name="product3" price="99.95" where="garagesale"> 
This is the third product. 
</item> 

</items> 

如果我做了4个阵列,一个名字,一个是价格,一个用于购买它的地方,一个用于描述,我如何将数据存入数组?

我想通过使用NSXMLParser,但不能得到nameprice,where或描述。

我被困在如何做到这一点。

任何帮助表示赞赏。

+1

你读过[the guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html)吗? – 2011-05-21 12:17:45

+0

[在iPhone上进行XML解析的最佳方法]的可能重复(http://stackoverflow.com/questions/842292/best-approach-for-xml-parsing-on-the-iphone)或[Simple XML Parsing]( http://stackoverflow.com/questions/3616447/parsing-xml-code-on-iphone-sdk(http://stackoverflow.com/questions/3616447/parsing-xml-code-on-iphone-sdk)解决XML的问题(http://stackoverflow.com/questions/3616447/parsing-xml-code-on-iphone-sdk) )或[在可可中解析XML](http://stackoverflow.com/questions/1089737/parsing-xml-in-cocoa)或[NSXMLParser on iPhone如何使用它?](http://stackoverflow.com/问题/ 2964503 /)。拿你的选择。 – 2011-05-21 17:06:31

+0

这是一个很好的教程,但有一件事我没有得到帮助。你能告诉我,我怎样才能读取字符串“这是第一个产品”。 – 2012-04-08 15:53:31

回答

15

首先,你需要创建一个对象,做了解析。它将安装NSXMLParser实例,将其自身设置为解析器的delegate,然后调用解析消息。它也可以负责存储四位结果数组:

NSXMLParser * parser = [[NSXMLParser alloc] initWithData:_data]; 
[parser setDelegate:self]; 
BOOL result = [parser parse]; 

你最感兴趣的是你的委托对象实施的信息是didStartElement。这个人被调用你的XML文件中的每个元素。在这个回调中,你可以添加你的名字,价格&其中属性到它们各自的数组。

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict 
{ 
    // just do this for item elements 
    if(![elementName isEqual:@"item"]) 
     return; 

    // then you just need to grab each of your attributes 
    NSString * name = [attributeDict objectForKey:@"name"]; 

    // ... get the other attributes 

    // when we have our various attributes, you can add them to your arrays 
    [m_NameArray addObject:name]; 

    // ... same for the other arrays 
} 
+0

谢谢,我现在对NSXMLParser有了一些了解。 – 2011-05-21 13:14:25

+0

@ jack-greenhill没问题:) – RedBlueThing 2011-05-22 01:20:53

0

你必须要考虑项目的标签的字典作为数组和三个标签(名称,价格,在哪里)为对象的指数0,1,2

3
在follwing方法

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 

    if([elementName isEqualToString:@"item"]) { 

     NSString *name=[attributeDict objectForKey:@"name"]; 
     NSString *price=[attributeDict objectForKey:@"price"]; 
     NSString *where=[attributeDict objectForKey:@"where"]; 
    } 
} 
3

为了让标签(例如 “这是第一个产品”。)您可以覆盖之间的值 - (空)解析器:(的NSXMLParser *)解析器foundCharacters:(NSString *)字符串

+0

请问您能否详细说明如何做到这一点?我有确切的要求,即获得标签之间的值。 – 2014-06-28 14:15:16