0

下面给出的代码是一个 webservice 项目的例子,它给出了元素周期表中元素的符号名称。当我运行这个项目时,我没有从 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>

如何将其转换为字符串并将其显示到文本字段中?

4

1 回答 1

0

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

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

https://github.com/nicklockwood/XMLDictionary

于 2013-08-21T14:41:27.573 回答