从NSString解析XML以获取值 [英] Parsing XML from NSString to get values

查看:102
本文介绍了从NSString解析XML以获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是在xcode中操作NSString。

This question is for manipulating NSString in xcode.

我有一个XML文本字符串,我从网上看到这样的

I have a XML text string that I get from the web that looks like this

<current temperature="73" day="Mon" humidity="59" windspeed="10"></current>

如何从此字符串中获取单个值并将它们放入我的NSString变量中?

How can I get individual values from this string and put them in my NSString variables?

例如

NSString *tempStr = ??
NSString *dayStr = ??
NSString *windspeedStr = ??


推荐答案

首先,按照描述在项目中下载并包含RaptureXML在 RaptureXML项目网站上。

First, download and include RaptureXML within your project as described on the RaptureXML project site.

用于解析单个给定行,使用以下代码段 - 您的输入传递为 inXmlString ;

For parsing the single given line, use the following snippet - your input is passed as inXmlString;

//transform string into an XML DOM
RXMLElement *rootNode = [RXMLElement elementFromXMLString:inXmlString 
                                             withEncoding:NSUTF8StringEncoding];
if (rootNode == nil || ![rootNode isValid])
{
    //do something, we failed!
}
else 
{
    NSString *temperature = [rootNode attribute:@"temperature"];
    NSString *day = [rootNode attribute:@"day"];
    NSString *windspeed = [rootNode attribute:@"windspeed"];
}

这篇关于从NSString解析XML以获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆