2013-08-21 159 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