如何解析响应 xml 字符串?
Posted
技术标签:
【中文标题】如何解析响应 xml 字符串?【英文标题】:How can I parse the response xml string? 【发布时间】:2013-01-07 17:43:18 【问题描述】:<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<VideoListResponse xmlns="http://tempuri.org/">
<VideoListResult>
"List":["GUID":"127381638172638173","PageNumber":[2,1]]
</VideoListResult>
</VideoListResponse>
</soap:Body>
</soap:Envelope>
这是我的响应 xml 字符串。如何从这个 xml 字符串中解析 PageNumber?
【问题讨论】:
【参考方案1】:这是一个简单的代码片段,您可以使用它来扩展
ViewController.h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <NSXMLParserDelegate>
@property(retain,nonatomic)NSString *videoListResult;
@property(retain,nonatomic)NSString *currentElement;
@end
ViewController.m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
-(void)viewDidAppear:(BOOL)animated
[self parseXML];
-(void)parseXML
NSString *xmlString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\
<soap:Body>\
<VideoListResponse xmlns=\"http://tempuri.org/\">\
<VideoListResult>\"List\":[\"GUID\":\"127381638172638173\",\"PageNumber\":[2,1]]\
</VideoListResult>\
</VideoListResponse>\
</soap:Body>\
</soap:Envelope>";
NSData *xmlData = [xmlString dataUsingEncoding:NSASCIIStringEncoding];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
[xmlParser setDelegate:self];
[xmlParser parse];
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
NSLog(@"Element started %@",elementName);
self.currentElement=elementName;
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
NSLog(@"Element ended %@",elementName);
self.currentElement=@"";
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
if([self.currentElement isEqualToString:@"VideoListResult"])
// NSLog(@"The characters are %@",string);
id data = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSASCIIStringEncoding] options:0 error:nil];
NSLog(@"The page numbers are %@",[[[data valueForKey:@"List"] objectAtIndex:0] valueForKey:@"PageNumber"]);
@end
【讨论】:
嗨,我正在尝试使用您的代码并正在尝试此操作,但我在字符串 xlmns 行收到了预期表达式错误:-(void)parseXML NSString *xmlString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\ <string xmlns=\"http://server.com">\; NSData *xmlData = [xmlString dataUsingEncoding:NSASCIIStringEncoding]; NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData]; [xmlParser setDelegate:self]; [xmlParser parse];
【参考方案2】:
您想使用LibXML 解析XML,然后您想使用SBJSON 解析VideoListResult 中包含的JSON。
【讨论】:
还有NSXMLParser
、NSXMLParserDelegate
和NSJSONSerialization
。我肯定更喜欢内置类而不是第三方库。
除非你需要支持 ios 4.X,Richard 基本上是对的。 libXML 对 XML 解析稍微好一点(因为你可以只在 XPath 上解析,然后抓取你想要的字段),而如果你使用 NSXMLParser,你必须弄乱更多的文档。以上是关于如何解析响应 xml 字符串?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android 中获取 XML 响应字符串以及 Kotlin 数据模型?