xcode 如何解析响应 xml 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14201187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:28:39  来源:igfitidea点击:

How can I parse the response xml string?

iosxmlxcodejsonparsing

提问by Le'Kirdok

<?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>

This is my response xml string. How can I parse the PageNumber from this xml string?

这是我的响应 xml 字符串。如何从这个 xml 字符串解析 PageNumber?

回答by Srikanth

This is a simple code fragment which you can use to expand

这是一个简单的代码片段,您可以使用它来扩展

ViewController.h file

视图控制器.h 文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <NSXMLParserDelegate>

@property(retain,nonatomic)NSString *videoListResult;

@property(retain,nonatomic)NSString *currentElement;

@end

ViewController.m file

视图控制器.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

回答by jfaller

You want to use LibXMLto parse the XML, and then you want to use SBJSONto parse the JSON contained in the VideoListResult.

您想使用LibXML来解析 XML,然后您想使用SBJSON来解析VideoListResult中包含的 JSON。