本文介绍了NSXMLParser 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的 XML
I have an XML like this
<IS>
<Value>
<Signature>-804</Signature>
<Amount>139</Amount>
</Value>
<Value>
<Signature>-845</Signature>
<Amount>639466</Amount>
</Value>
<Value>
<Signature>-811</Signature>
<Amount>16438344</Amount>
</Value>
<Value>
<Signature>-1115</Signature>
<Amount>-159733</Amount>
</Value>
</IS>
现在我只想从中解析特定的值.例如,如何获取具有对应签名的节点的值 -804
Now I want to parse only specific values from this. For example, how do I get the value for the node having corresponding signature as -804
请帮帮我..
我知道 NSXMLParser 的基础知识,但不知道如何实现条件解析.
I know the basics of NSXMLParser, but do not know how to acheive conditional parsing.
谢谢.
推荐答案
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentKey = nil;
[currentStringValue release];
currentStringValue = nil;
if([elementName isEqualToString:@"Value"]){
//alloc some object to parse value into
} else if([elementName isEqualToString:@"Signature"]){
currentKey = @"Signature";
return;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(currentKey){
if(!currentStringValue){
currentStringValue = [[NSMutableString alloc] initWithCapacity:200];
}
[currentStringValue appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"Signature"] && [currentStringValue intValue] == 804){
ivar.signature = [currentStringValue intValue];
return;
}
}
类似的东西.注意我还没有在编译器上真正测试过这段代码,所以这里会有语法错误 &那里.
Something like this.Note I havent really tested this code on compiler so there will be syntax errors here & there.
这篇关于NSXMLParser 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!