2013-08-21 64 views
0

下面给出的代码是webservice项目的一个示例,它给出元素周期表中的元素名称。当我运行这个项目时,我没有从xml解析器获取字符串值。如何在xml解析器解析后获取xml格式的字符串值

-(IBAction)action:(id)sender 
{ 
    NSString *soapFormat = [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>\n" 
          "<GetElementSymbol xmlns=\"http://www.webserviceX.NET/\">\n" 
          "<ElementName>%@</ElementName>\n" 
          "</GetElementSymbol>\n" 
          "</soap:Body>\n" 
          "</soap:Envelope>\n",txtcelsius.text]; 

    NSLog(@"connextion:%@",soapFormat); 
    NSURL *locationofWebservice=[NSURL URLWithString:@"http://www.webservicex.net/webservices/periodictable.asmx"]; 
    NSMutableURLRequest *theRequest=[[NSMutableURLRequest alloc]initWithURL:locationofWebservice]; 
    NSLog(@"sopa len=%d",[soapFormat length]); 
    NSString *msgLength=[NSString stringWithFormat:@"%d",[soapFormat length]]; 
    [theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; 
    [theRequest addValue:@"http://www.webserviceX.NET/GetElementSymbol" forHTTPHeaderField:@"SOAPAction"]; 
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    //the below encoding is used to send data over the net 
    [theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 
    if (connection) 
    { 
     webData=[[NSMutableData alloc]init]; 
    } 
    else 
    { 
     NSLog(@"No connection"); 
    } 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [webData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Webservice" message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"DONE. Received Bytes: %d", [webData length]); 
    xmlParser= [[NSXMLParser alloc]initWithData:webData]; 
    [xmlParser setDelegate:self]; 
    [xmlParser parse]; 
} 

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    NSLog(@"string in parse=%@",string); 

    nodeContent=[[NSMutableString alloc]init]; 
    [nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 

    NSLog(@"string in parse node=%@",nodeContent); 
} 

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if([elementName isEqualToString:@"GetElementSymbol"]) 
    { 
     // finaldata=nodeContent; 
     NSLog(@"node content=%@",nodeContent); 
     txtft.text=nodeContent; 
    } 
} 

该代码是webservice项目的一个例子,它给出了元素周期表的符号。从主机侧的响应是在下面的格式:

<string xmlns="http://www.webserviceX.NET"> 
<NewDataSet> <Table> <Symbol>H</Symbol> </Table> </NewDataSet> 
</string> 

我怎么能转换到这个字符串,并将其显示到一个文本框?

回答

0

如果您想解析XML数据,请查看XMLDictionary。这是解析信息的一种更简单的方法,甚至更容易理解。

这是一个简单的框架,可以轻松地将数据解析为NSDictionaries和NSArrays的整齐有序的层次结构,并且所有数据都将显示为NSStrings。

https://github.com/nicklockwood/XMLDictionary