如何解析响应xml字符串?

时间:2022-11-29 02:09:53

Possible Duplicate:
SOAP parsing in PHP

可能的重复:PHP中的SOAP解析

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

2 个解决方案

#1


7  

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

这是一个可以用来展开的简单代码片段

ViewController.h file

ViewController。h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <NSXMLParserDelegate>

@property(retain,nonatomic)NSString *videoListResult;

@property(retain,nonatomic)NSString *currentElement;

@end

ViewController.m file

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

#2


1  

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

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

#1


7  

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

这是一个可以用来展开的简单代码片段

ViewController.h file

ViewController。h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <NSXMLParserDelegate>

@property(retain,nonatomic)NSString *videoListResult;

@property(retain,nonatomic)NSString *currentElement;

@end

ViewController.m file

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

#2


1  

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

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