如何解析节点标签中有空格的XML节点?
我正在使用 Weather.com Web 服务来请求某个位置的当前状况,并解析当前温度,感觉就像我的 Flash 应用程序中的温度和当前状况图标。当前温度和感觉温度节点嵌套在 NowItems -> 中。 NowItem 而 WeatherToday 则在提要中更靠前。我在 AS3 中编写了以下内容:
public class Main extends MovieClip
{
public function Main()
{
var urlRequest:URLRequest = new URLRequest("WebServiceURL");
var urlLoader:URLLoader=new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onXmlLoaded);
urlLoader.load(urlRequest);
}
private function onXmlLoaded(event:Event):void
{
XML.ignoreWhitespace = true;
var urlLoader:URLLoader = event.currentTarget as URLLoader;
var resultXML:XML = XML(urlLoader.data);
trace(resultXML.NowItems.NowItem.CURRENT TEMPERATURE.Value);
trace(resultXML.NowItems.NowItem.FEELS LIKE.Value);
trace(resultXML.WeatherToday.Icon);
var lowtemp_label :TextField = new TextField();
addChild(lowtemp_label);
var hitemp_label :TextField=new TextField();
addChild(hitemp_label);
var condicon_label :TextField=new TextField();
addChild(condicon_label);
lowtemp_label.text = resultXML.minTemp;
hitemp_label.text= resultXML.maxTemp;
condicon_label.text=resultXML.icon;
}
}
这是一些 XML:
<rss version="2.0" xmlns:a10="http://www.w3.org/2005/Atom">
<channel>
<title>WEATHERSCAPE</title>
<link>http://www.weatherchannel.com.au/</link>
<description>WEATHERSCAPE DATA</description>
<category>Weather</category>
<item>
<guid isPermaLink="false">18b88e0b-b53f-41a3-bdfb-0762ae440f60</guid>
<link>http://www.weatherchannel.com.au/</link>
<title>Weather</title>
<description><?xml version="1.0" encoding="utf-16"?>
<LocalWeather xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AreaType>suburb</AreaType>
<AreaId>555</AreaId>
<AreaName>SYDNEY</AreaName>
<UTCOffset>600</UTCOffset>
<ForecastCreated>2011-04-29T17:10:25Z</ForecastCreated>
<ObservationsCreated>2011-04-29T17:00:00Z</ObservationsCreated>
<NowItems>
<NowItem>
<Label>CURRENT TEMPERATURE</Label>
<Value>16.7</Value>
<Units>°C</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>FEELS LIKE</Label>
<Value>16.9</Value>
<Units>°C</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>DEW POINT</Label>
<Value>16</Value>
<Units>°C</Units>
<Change>DOWN</Change>
</NowItem>
<NowItem>
<Label>HUMIDITY</Label>
<Value>96</Value>
<Units>%</Units>
</NowItem>
<NowItem>
<Label>WIND SPEED</Label>
<Value>9</Value>
<Units>km/h</Units>
<Change>STEADY</Change>
<Direction>SW</Direction>
</NowItem>
<NowItem>
<Label>WIND GUSTS</Label>
<Value>15</Value>
<Units>km/h</Units>
</NowItem>
<NowItem>
<Label>PRESSURE</Label>
<Value>1022</Value>
<Units>hPa</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>RAIN SINCE 9AM</Label>
<Value>9</Value>
<Units>mm</Units>
<Change>N/A</Change>
</NowItem>
<NowItem>
<Label>FIRE DANGER LEVEL</Label>
<Value>UNAVAILABLE</Value>
<Units>N/A</Units>
</NowItem>
</NowItems>
<WMOID>94768</WMOID>
<SiteId>66062</SiteId>
<WeatherStation>SYDNEY</WeatherStation>
<WeatherToday>
<Icon>Fewshowers</Icon>
<FriendlyName>Few showers</FriendlyName>
<Precis>Few showers.</Precis>
<MaxTemp>22</MaxTemp>
<MinTemp>17</MinTemp>
<Units>°C</Units>
<MorningIcon>Showers</MorningIcon>
<AfternoonIcon>Fewshowers</AfternoonIcon>
<EveningIcon>Showers</EveningIcon>
<MorningPrecis>Showers</MorningPrecis>
<AfternoonPrecis>Few showers</AfternoonPrecis>
<EveningPrecis>Showers</EveningPrecis>
</WeatherToday>
我在请求当前温度和感觉的行上收到编译器错误,因为它们中有空格?如果这些节点有空格,我应该使用什么语法来引用它们?这是引用这些节点的正确方法,还是有更简单的方法?当我引用标签文本(最后 3 行)中的节点时,我是否像跟踪语句中那样使用完全限定的节点?
非常感谢!!
I'm using the weather.com web service to request current conditions for a location, and am parsing the current temperature, feels like temp and current condition icon to my Flash application. The Current Temperature and Feels Like Temperature nodes are nested within NowItems -> NowItem while WeatherToday is further along in the feed. I've written the following in AS3:
public class Main extends MovieClip
{
public function Main()
{
var urlRequest:URLRequest = new URLRequest("WebServiceURL");
var urlLoader:URLLoader=new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onXmlLoaded);
urlLoader.load(urlRequest);
}
private function onXmlLoaded(event:Event):void
{
XML.ignoreWhitespace = true;
var urlLoader:URLLoader = event.currentTarget as URLLoader;
var resultXML:XML = XML(urlLoader.data);
trace(resultXML.NowItems.NowItem.CURRENT TEMPERATURE.Value);
trace(resultXML.NowItems.NowItem.FEELS LIKE.Value);
trace(resultXML.WeatherToday.Icon);
var lowtemp_label :TextField = new TextField();
addChild(lowtemp_label);
var hitemp_label :TextField=new TextField();
addChild(hitemp_label);
var condicon_label :TextField=new TextField();
addChild(condicon_label);
lowtemp_label.text = resultXML.minTemp;
hitemp_label.text= resultXML.maxTemp;
condicon_label.text=resultXML.icon;
}
}
Here's some of the XML:
<rss version="2.0" xmlns:a10="http://www.w3.org/2005/Atom">
<channel>
<title>WEATHERSCAPE</title>
<link>http://www.weatherchannel.com.au/</link>
<description>WEATHERSCAPE DATA</description>
<category>Weather</category>
<item>
<guid isPermaLink="false">18b88e0b-b53f-41a3-bdfb-0762ae440f60</guid>
<link>http://www.weatherchannel.com.au/</link>
<title>Weather</title>
<description><?xml version="1.0" encoding="utf-16"?>
<LocalWeather xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AreaType>suburb</AreaType>
<AreaId>555</AreaId>
<AreaName>SYDNEY</AreaName>
<UTCOffset>600</UTCOffset>
<ForecastCreated>2011-04-29T17:10:25Z</ForecastCreated>
<ObservationsCreated>2011-04-29T17:00:00Z</ObservationsCreated>
<NowItems>
<NowItem>
<Label>CURRENT TEMPERATURE</Label>
<Value>16.7</Value>
<Units>°C</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>FEELS LIKE</Label>
<Value>16.9</Value>
<Units>°C</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>DEW POINT</Label>
<Value>16</Value>
<Units>°C</Units>
<Change>DOWN</Change>
</NowItem>
<NowItem>
<Label>HUMIDITY</Label>
<Value>96</Value>
<Units>%</Units>
</NowItem>
<NowItem>
<Label>WIND SPEED</Label>
<Value>9</Value>
<Units>km/h</Units>
<Change>STEADY</Change>
<Direction>SW</Direction>
</NowItem>
<NowItem>
<Label>WIND GUSTS</Label>
<Value>15</Value>
<Units>km/h</Units>
</NowItem>
<NowItem>
<Label>PRESSURE</Label>
<Value>1022</Value>
<Units>hPa</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>RAIN SINCE 9AM</Label>
<Value>9</Value>
<Units>mm</Units>
<Change>N/A</Change>
</NowItem>
<NowItem>
<Label>FIRE DANGER LEVEL</Label>
<Value>UNAVAILABLE</Value>
<Units>N/A</Units>
</NowItem>
</NowItems>
<WMOID>94768</WMOID>
<SiteId>66062</SiteId>
<WeatherStation>SYDNEY</WeatherStation>
<WeatherToday>
<Icon>Fewshowers</Icon>
<FriendlyName>Few showers</FriendlyName>
<Precis>Few showers.</Precis>
<MaxTemp>22</MaxTemp>
<MinTemp>17</MinTemp>
<Units>°C</Units>
<MorningIcon>Showers</MorningIcon>
<AfternoonIcon>Fewshowers</AfternoonIcon>
<EveningIcon>Showers</EveningIcon>
<MorningPrecis>Showers</MorningPrecis>
<AfternoonPrecis>Few showers</AfternoonPrecis>
<EveningPrecis>Showers</EveningPrecis>
</WeatherToday>
I get compiler errors on the lines requesting the Current Temperature and Feels Like, as they have spaces in them? What syntax do I use to reference these nodes given that they have spaces? Is this the correct way to reference these nodes, or are there simpler methods? When I reference the nodes in my label text (last 3 lines), do I use the fully qualified node as is in my trace statements?
Thanks much!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
描述节点内部是一个不是 XML 的 XML STRING
你需要获取描述节点然后将其转换为xml来解析它
[编辑]
好的,您的示例中基本上是 2 个 XML 文件,一个接一个。
所以我们需要做的是解析出描述节点,您可以从 HERE 和 HERE 是一个很好的例子loader
一旦您成功获取了描述节点的内容,您现在必须对数据进行转义,因为它已转换为 html 友好格式。(很确定 unescape 函数将处理此问题,如果不在此处发布另一个关于如何处理的问题)转换它 - 抱歉现在没有时间测试)
示例:
字符串转换后,您将创建一个新的 XML: 对象,然后像解析服务器响应时一样访问所需的节点。
Inside of the description node is an XML STRING that is not XML
you need to get the description node then convert it to xml to parse it
[EDIT]
OK, what you have in your example is basically 2 XML files one inside the other.
So what we need to do is parse out the description node you can learn how to do that from HERE and HERE is a good example of a loader
Once you have successfully obtained the contents of the description node you now have to unescape the data because it has been converted to html friendly format.(pretty sure the unescape function will handle this if not post another question on here about how to convert it - sorry no time to test right now)
Example:
After the string has been converted you will create a new XML: object off of that and then access the node you want like you did when you parsed the server response.