的iOS:解析使用的NSXMLParser从HTTP一个XML为
它使用的NSXMLParser,不知道第一次,如果你给我从http请求,看起来像解析返回的XML的一些方向:的iOS:解析使用的NSXMLParser从HTTP一个XML为
<?xml version="1.0" ?>
<theresponse>
<status>OK</status>
<pricing currency="USD" symbol="$">
<price class="items">24.00</price>
<price class="shipping">6.00</price>
<price class="tax">1.57</price>
</pricing>
</theresponse>
我知道基本的解析委托方法,我只是想知道代码将在didEndElement/foundCharacters/didStartElement中检索以上项目(货币/项目/ shipping/tax)的代码么?任何帮助不胜感激。
这比一些标准的NSXMLParser
代码稍微棘手;因为基本上当你在寻找“shipping
”时,你想要“6.00
”,但这两部分数据以不同的委托方法返回给你,这是正常的。但通常该元素将被命名为“shipping
”,因此在parser:didEndElement:namespaceURI:qualifiedName:
中,您将自动获得元素名称,因为它已传入方法中。
的解决办法似乎很简单,有一个_currentAttributes
伊娃和parser:didStartElement:namespaceURI:qualifiedName:attributes:
这样做_currentAttributes = attributeDict;
然后在didEndElement:
方法处理这个问题。然而,这种风格很容易破碎,即使在这个适度简单的XML
。
我的处理方法是存储传递到didStartElement:
的属性字典,并将其设置在字典中作为元素名称的键的对象。将这种风格与NSMutableString
作为字符缓冲区的标准用法结合使用,可以将所有逻辑放入didEndElement:
方法中。
附注:我也很喜欢让我的NSXMLParserDelegate
类是NSXMLParser
的子类,就像这个。然而,如果委托方法不是,委托方法将是相同的。
ItemParser.h
#import <Foundation/Foundation.h>
@interface ItemParser : NSXMLParser <NSXMLParserDelegate>
@property (readonly) NSDictionary *itemData;
@end
ItemParser.m
#import "ItemParser.h"
@implementation ItemParser {
NSMutableDictionary *_itemData;
NSMutableDictionary *_attributesByElement;
NSMutableString *_elementString;
}
-(NSDictionary *)itemData{
return [_itemData copy];
}
-(void)parserDidStartDocument:(NSXMLParser *)parser{
_itemData = [[NSMutableDictionary alloc] init];
_attributesByElement = [[NSMutableDictionary alloc] init];
_elementString = [[NSMutableString alloc] init];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
// Save the attributes for later.
if (attributeDict) [_attributesByElement setObject:attributeDict forKey:elementName];
// Make sure the elementString is blank and ready to find characters
[_elementString setString:@""];
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// Save foundCharacters for later
[_elementString appendString:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"status"]){
// Element status only contains a string i.e. "OK"
// Simply set a copy of the element value string in the itemData dictionary
[_itemData setObject:[_elementString copy] forKey:elementName];
} else if ([elementName isEqualToString:@"pricing"]) {
// Pricing has an interesting attributes dictionary
// So copy the entries to the item data
NSDictionary *attributes = [_attributesByElement objectForKey:@"pricing"];
[_itemData addEntriesFromDictionary:attributes];
} else if ([elementName isEqualToString:@"price"]) {
// The element price occurs multiple times.
// The meaningful designation occurs in the "class" attribute.
NSString *class = [[_attributesByElement objectForKey:elementName] objectForKey:@"class"];
if (class) [_itemData setObject:[_elementString copy] forKey:class];
}
[_attributesByElement removeObjectForKey:elementName];
[_elementString setString:@""];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
_attributesByElement = nil;
_elementString = nil;
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"%@ with error %@",NSStringFromSelector(_cmd),parseError.localizedDescription);
}
-(BOOL)parse{
self.delegate = self;
return [super parse];
}
@end
所以测试我存储在XML
你上面贴到一个名为 “ItemXML.xml” 文件。
NSURL *url = [[NSBundle mainBundle] URLForResource:@"ItemXML" withExtension:@"xml"];
ItemParser *parser = [[ItemParser alloc] initWithContentsOfURL:url];
[parser parse];
NSLog(@"%@",parser.itemData);
我得到的结果是:
{
currency = USD;
items = "24.00";
shipping = "6.00";
status = OK;
symbol = "$";
tax = "1.57";
}
谢谢你这么多,使用此代码进行了测试。 – Cam 2012-04-09 19:28:00