如何在xml解析器解析后获取xml格式的字符串值

时间:2013-08-21 11:36:03

标签: ios nsxmlparser

下面给出的代码是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>

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

1 个答案:

答案 0 :(得分:0)

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

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

https://github.com/nicklockwood/XMLDictionary

相关问题