【问题标题】:How to get the string value of xml format after parsing by the xml parserxml解析器解析后如何获取xml格式的字符串值
【发布时间】:2013-08-21 22:41:27
【问题描述】:

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

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

【问题讨论】:

    标签: ios nsxmlparser


    【解决方案1】:

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

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

    https://github.com/nicklockwood/XMLDictionary

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-09
      • 2017-07-25
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      相关资源
      最近更新 更多