iphone objective-c解析xml字符串(接收格式参照上篇 webservie xml字符串传值)

来源:互联网 发布:淘宝可以代付吗 编辑:程序博客网 时间:2024/04/28 19:39

 

 

解析的方式有两种,这里只说一种

首先去把BlogTutorial给下载下来

然后把里面的TouchXML文件夹给导入工程

然后配置libxml,这个的配置以下地址有

http://blog.prosight.me/index.php/2010/02/586

 

然后就可以开始动作了,呵呵

这个东西要导入

#import "CXMLDocument.h"

 

 

变量格式定义:

 

NSMutableArray *tasksArray;

NSMutableArray *taskIdArray;

NSMutableArray *taskNameArray;

NSXMLParser *xmlParser;

NSMutableString *tempString;

NSMutableData *taskData;

NSMutableArray *bookArray;

NSMutableString *xmlTry;

BOOL recordResults;

 

@property (nonatomic,retain) IBOutlet NSXMLParser *xmlParser;

@property (nonatomic,retain) IBOutlet NSMutableArray *tasksArray;

@property (nonatomic,retain) IBOutlet NSMutableArray *taskIdArray;

@property (nonatomic,retain) IBOutlet NSMutableArray *taskNameArray;

@property (nonatomic,retain) IBOutlet NSMutableString *tempString;

@property (nonatomic,retain) IBOutlet NSMutableData *taskData;

@property (nonatomic,retain) IBOutlet NSMutableArray *bookArray;

@property (nonatomic,retain) IBOutlet NSMutableString *xmlTry;

 

有些冗余的东西,这里就不细化了。

 

 

 

这段代码贴在程序入口处

 

taskData = [[NSMutableData alloc] init];

    NSString *soapMessage = [NSString stringWithFormat:

@"<?xml version=/"1.0/" encoding=/"utf-8/"?>/n"

"<soap:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">/n"

"<soap:Body>"

"<getUserList xmlns=/"http://view//">"

"<userID>%@</userID>"

"</getUserList >"

"</soap:Body>"

"</soap:Envelope>",textField1.text];

    //建立连接

    NSURL *url = [NSURL URLWithString:@"http://192.168.0.1:8080/test/services/UserList"];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    //post方式提交数据

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [theRequest addValue: @"" forHTTPHeaderField:@"SOAPAction"]; //这个地方的Action名参照webservice提供的文件说明

    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    [theRequest setHTTPMethod:@"POST"];

    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    //NSURLConnection于服务器交互

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    [theConnection release];

 

 

 

//收到服务器回答

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    [taskData setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [taskData appendData:data];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"ERROR with theConenction");

    [connection release];

    [taskData release];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"DONE. Received Bytes: %d", [taskData length]);

    NSString *theXML = [[NSString alloc] initWithBytes: [taskData mutableBytes] length:[taskData length] encoding:NSUTF8StringEncoding];

    //NSLog(theXML);

    [theXML release];

 

    if( xmlParser )

    {

        [xmlParser release];

    }

 

    xmlParser = [[NSXMLParser alloc] initWithData: taskData];

    [xmlParser setDelegate: self];

    [xmlParser setShouldResolveExternalEntities: YES];

    [xmlParser parse];

 

    [connection release];

    [taskData release];

}

 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName

   attributes: (NSDictionary *)attributeDict

{

    if( [elementName isEqualToString:@"getUserListResponse"])

    {

        if(!tempString)

        {

            tempString = [[NSMutableString alloc] init];

        }

        recordResults = TRUE;

    }

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

    if( recordResults )

    {

        [tempString appendString: string];

    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

    if( [elementName isEqualToString:@"getUserListResponse"])

    {

        recordResults = FALSE;

NSLog(@"soapResults: %@", tempString);

NSMutableArray *res = [[NSMutableArray alloc] init];

CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString: tempString options: 0 error: nil];

//创建一个数组,以存放我们从xml中得到的节点

NSArray *nodes = NULL;

//寻找节点为taskItem的所有节点 

nodes = [doc nodesForXPath:@"//taskItem" error:nil];

for (CXMLElement *node in nodes) {

NSMutableDictionary *item = [[NSMutableDictionary alloc] init];

int counter;

for(counter = 0; counter < [node childCount]; counter++) {

// 从节点中取出值,按照键值对存储在NSMutableDictionary

[item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];

}

// 取得节点属性值,并存储在NSMutableDictionary

[item setObject:[[node attributeForName:@"taskId"] stringValue] forKey:@"taskId"];  // <------ this magical arrow is pointing to the area of interest

[res addObject:item];

[item release];

}

NSLog(@"%@", res);

[res release];

        [tempString release];

        tempString = nil;

    }

}

 

 

原创粉丝点击