为什么我无法访问没有命名空间的 xml 节点?
我几个小时以来一直在办公桌上敲头,试图找出为什么我无法访问entry.author.name 内容? 我尝试了所有可能的方法来访问它但失败了。
但是当我使用 xml.children 循环所有节点时,我看到了作者节点。
我看到作者节点是原子命名空间的一部分,但它没有像其他命名空间那样的命名空间。
package {
import flash.display.Sprite;
public class Gdata extends Sprite
{
private var xml:XML = <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/"
xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">
http://www.w3.org/2005/Atom,http://search.yahoo.com/mrss/,http://schemas.google.com/g/2005, http://gdata.youtube.com/schemas/2007
<id>
http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk
</id>
<published>
2009-04-17T19:11:48.000Z
</published>
<updated>
2009-04-29T10:19:50.000Z
</updated>
<category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="News" label="News & Politics"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Garofalo"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Olbermann"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="commentary"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Janeane"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="news"/>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Keith"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="analysis"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Party"/>
<category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Tea"/>
<title type="text">
Janeane Garofalo Calls Tea Party Protesters Racists
</title>
<content type="text">
Janeane Garofalo: http://tinyurl.com/cr6rt8
EPA: http://tinyurl.com/clpyqo
Veterans: http://tinyurl.com/ckbhb6
DHS: http://tinyurl.com/cctf54
DHS2: http://tinyurl.com/dahaxr
John Ziegler: http://tinyurl.com/d7qbo4
Teabagging: http://tinyurl.com/ddskjo
</content>
<link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=bwbZJw-C4Jk"/>
<link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk/responses"/>
<link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk/related"/>
<link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=bwbZJw-C4Jk"/>
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk"/>
<author>
<name>
HowTheWorldWorks
</name>
<uri>
http://gdata.youtube.com/feeds/api/users/howtheworldworks
</uri>
</author>
<gd:comments>
<gd:feedLink href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk/comments" countHint="2249"/>
</gd:comments>
<media:group>
<media:category label="News & Politics" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">
News
</media:category>
<media:content url="http://www.youtube.com/v/bwbZJw-C4Jk&f=videos&app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="236" yt:format="5"/>
<media:content url="rtsp://rtsp2.youtube.com/CiILENy73wIaGQmZ4IIPJ9kGbxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="236" yt:format="1"/>
<media:content url="rtsp://rtsp2.youtube.com/CiILENy73wIaGQmZ4IIPJ9kGbxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="236" yt:format="6"/>
<media:description type="plain">
Janeane Garofalo: http://tinyurl.com/cr6rt8
</media:description>
<media:keywords>
Janeane, Garofalo, Keith, Olbermann, Tea, Party, commentary, analysis, news
</media:keywords>
<media:player url="http://www.youtube.com/watch?v=bwbZJw-C4Jk"/>
<media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/default.jpg" height="90" width="120" time="00:01:58"/>
<media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/1.jpg" height="90" width="120" time="00:00:59"/>
<media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/3.jpg" height="90" width="120" time="00:02:57"/>
<media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/0.jpg" height="240" width="320" time="00:01:58"/>
<media:title type="plain">
Janeane Garofalo Calls Tea Party Protesters Racists
</media:title>
<yt:duration seconds="236"/>
</media:group>
<gd:rating average="3.9231105" max="5" min="1" numRaters="2289" rel="http://schemas.google.com/g/2005#overall"/>
<yt:statistics favoriteCount="304" viewCount="33463"/>
</entry>;
namespace atom = "http://www.w3.org/2005/Atom"
namespace media ="http://search.yahoo.com/mrss/";
namespace gd = "http://schemas.google.com/g/2005";
namespace yt = "http://gdata.youtube.com/schemas/2007";
public function Gdata()
{
var atom:Namespace = new Namespace();
trace("0",xml.children()[22]);
trace("1",xml.inScopeNamespaces());
trace("2",xml..media::group);
trace("3",xml..yt::statistics.@favoriteCount);
trace("4",xml..yt::duration.@seconds);
trace("5",xml..author ); // why?????????
trace("6",xml.author ); // why?????????
var x:int = 0;
for each (var i in xml.children())
{
if(i.localName() == 'author')
{
trace("author", i);
}
}
}
}
}
I've been banging my head on my desk for hours trying to figure out why I can't access the entry.author.name content? I've tried every possible way to access it but failed.
But when I loop through all nodes with xml.children I see the author node.
I see the author node is apart of the atom namespace but it doesn't have a namespace like the other namespaces.
package { import flash.display.Sprite;
public class Gdata extends Sprite { private var xml:XML = <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007"> http://www.w3.org/2005/Atom,http://search.yahoo.com/mrss/,http://schemas.google.com/g/2005, http://gdata.youtube.com/schemas/2007 <id> http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk </id> <published> 2009-04-17T19:11:48.000Z </published> <updated> 2009-04-29T10:19:50.000Z </updated> <category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="News" label="News & Politics"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Garofalo"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Olbermann"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="commentary"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Janeane"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="news"/> <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Keith"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="analysis"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Party"/> <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Tea"/> <title type="text"> Janeane Garofalo Calls Tea Party Protesters Racists </title> <content type="text"> Janeane Garofalo: http://tinyurl.com/cr6rt8 EPA: http://tinyurl.com/clpyqo Veterans: http://tinyurl.com/ckbhb6 DHS: http://tinyurl.com/cctf54 DHS2: http://tinyurl.com/dahaxr John Ziegler: http://tinyurl.com/d7qbo4 Teabagging: http://tinyurl.com/ddskjo </content> <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=bwbZJw-C4Jk"/> <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk/responses"/> <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk/related"/> <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=bwbZJw-C4Jk"/> <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk"/> <author> <name> HowTheWorldWorks </name> <uri> http://gdata.youtube.com/feeds/api/users/howtheworldworks </uri> </author> <gd:comments> <gd:feedLink href="http://gdata.youtube.com/feeds/api/videos/bwbZJw-C4Jk/comments" countHint="2249"/> </gd:comments> <media:group> <media:category label="News & Politics" scheme="http://gdata.youtube.com/schemas/2007/categories.cat"> News </media:category> <media:content url="http://www.youtube.com/v/bwbZJw-C4Jk&f=videos&app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="236" yt:format="5"/> <media:content url="rtsp://rtsp2.youtube.com/CiILENy73wIaGQmZ4IIPJ9kGbxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="236" yt:format="1"/> <media:content url="rtsp://rtsp2.youtube.com/CiILENy73wIaGQmZ4IIPJ9kGbxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="236" yt:format="6"/> <media:description type="plain"> Janeane Garofalo: http://tinyurl.com/cr6rt8 </media:description> <media:keywords> Janeane, Garofalo, Keith, Olbermann, Tea, Party, commentary, analysis, news </media:keywords> <media:player url="http://www.youtube.com/watch?v=bwbZJw-C4Jk"/> <media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/default.jpg" height="90" width="120" time="00:01:58"/> <media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/1.jpg" height="90" width="120" time="00:00:59"/> <media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/3.jpg" height="90" width="120" time="00:02:57"/> <media:thumbnail url="http://i.ytimg.com/vi/bwbZJw-C4Jk/0.jpg" height="240" width="320" time="00:01:58"/> <media:title type="plain"> Janeane Garofalo Calls Tea Party Protesters Racists </media:title> <yt:duration seconds="236"/> </media:group> <gd:rating average="3.9231105" max="5" min="1" numRaters="2289" rel="http://schemas.google.com/g/2005#overall"/> <yt:statistics favoriteCount="304" viewCount="33463"/> </entry>; namespace atom = "http://www.w3.org/2005/Atom" namespace media ="http://search.yahoo.com/mrss/"; namespace gd = "http://schemas.google.com/g/2005"; namespace yt = "http://gdata.youtube.com/schemas/2007"; public function Gdata() { var atom:Namespace = new Namespace(); trace("0",xml.children()[22]); trace("1",xml.inScopeNamespaces()); trace("2",xml..media::group); trace("3",xml..yt::statistics.@favoriteCount); trace("4",xml..yt::duration.@seconds); trace("5",xml..author ); // why????????? trace("6",xml.author ); // why????????? var x:int = 0; for each (var i in xml.children()) { if(i.localName() == 'author') { trace("author", i); } } } }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这个 ...
Try this ...
您也可以完全删除名称空间...这是我找到的最佳解决方案(它只是将名称空间详细信息转换为属性):
you can also just strip out the namespace completely... here's the best solution I found (it simply turns the namespace details into attributes):
在您的 mxml 文件中的某个位置,您需要以下代码片段(在使用 XML 文档之前)
Somewhere in your mxml file you need the following code snippet (before you work with your XML Document)