如何在 Yahoo! 中使用 Xpath 遍历树的小部件?

发布于 2024-07-15 03:00:30 字数 3893 浏览 5 评论 0原文

我正在创建一个 Yahoo! 小部件并且之前已经完成此操作没有任何问题(创建小部件)。 我通过网络链接获取 xml 文档,并希望获取树中的所有节点。 我正在这样做:

var request = new XMLHttpRequest();
        request.open( "GET", url, false );
        request.send();
        if ( request.status == 200 )
        {
            doc = request.responseXML;
            tree = doc.evaluate("/lfm");
            status = tree.item(0).getAttribute("status")
            if(status == "ok")
            {
                print ("status is ok!");
                tracks = doc.evaluate("/lfm/recenttracks[1]/track");
                for(i=0;i<tracks.length;i++)
                {
                    artist = tracks.item(i).firstChild.firstChild.data;
                }
            }
        }
    }

这样您就可以从中获取节点artist。 但如果你想要下一个兄弟姐妹,那就有问题了。 你必须打电话

tracks.item(i).firstChild.nextSibling.firstChild.data;
tracks.item(i).firstChild.nextSibling.nextSibling.firstChild.data;
tracks.item(i).firstChild.nextSibling.nextSibling.nextSibling.firstChild.data;

来完成这件事。 旁边的节点向其添加'nextsibling',依此类推。 我不想继续添加这些节点,并认为可以像这样使用 childNodes[i]

artist = tracks.item(i).childNodes[0].firstChild.data;
nextitem = tracks.item(i).childNodes[1].firstChild.data;

但这不起作用。 无论我使用它的方式如何,它都会返回“childNodes[0]没有属性”。 现在我认为 Xpath 中还有一种方法可以在 for 循环中执行此操作:

name = doc.evaluate("string(/lfm/recenttracks[1]/track["+i+"]/name)");
                    print(name);
othernode = doc.evaluate("string(/lfm/recenttracks[1]/track["+i+"]/album)");
                    print(othernode);

然后为下一个轨道增加 i 。 但不知何故,这仅返回一个项目。 它不会在 for 循环中检索更多项目。 i-1 也不起作用。

任何人都知道如何使用 Xpath 表达式和我的 i 值来选择一个节点,然后获取每个超级节点的子节点? 对于每个曲目,我想要获取艺术家、名称、流媒体、mbid、专辑、url、图像(小)、图像(中)、图像(大)日期。

我的 xml 文件如下所示:

<lfm status="ok">
   <recenttracks user="xaddict">
      <track nowplaying="true"> 
         <artist mbid="f5b8ea5f-c269-45dd-9936-1fedf3c56851">The Presets</artist>
         <name>Girl (You Chew My Mind Up)</name>
         <streamable>1</streamable>
         <mbid></mbid>
         <album mbid="b150d099-b0f3-4feb-9a05-34e693c6dd24">Beams</album>
         <url>http://www.last.fm/music/The+Presets/_/Girl+%28You+Chew+My+Mind+Up%29</url>
         <image size="small">http://userserve-ak.last.fm/serve/34s/8696437.jpg</image>
         <image size="medium">http://userserve-ak.last.fm/serve/64s/8696437.jpg</image>
         <image size="large">http://userserve-ak.last.fm/serve/126/8696437.jpg</image>
         <date uts="1236440600">7 Mar 2009, 15:43</date>
      </track>
      <track > 
         <artist mbid="f5b8ea5f-c269-45dd-9936-1fedf3c56851">The Presets</artist>
         <name>Get Outta Here</name>
         <streamable>1</streamable>
         <mbid></mbid>
         <album mbid="0469956f-d895-4120-8ec5-29ad41b9e2fd">Blow Up</album>
         <url>http://www.last.fm/music/The+Presets/_/Get+Outta+Here</url>
         <image size="small">http://userserve-ak.last.fm/serve/34s/20923179.png</image>
         <image size="medium">http://userserve-ak.last.fm/serve/64s/20923179.png</image>
         <image size="large">http://userserve-ak.last.fm/serve/126/20923179.png</image>
         <date uts="1236440242">7 Mar 2009, 15:37</date>
      </track>
   </recenttracks>
</lfm>

I'm creating a Yahoo! Widget and have done this before without any problems (creating a widget).
I'm getting a xml document via a weblink and want to get all the nodes intro a tree. I'm doing this:

var request = new XMLHttpRequest();
        request.open( "GET", url, false );
        request.send();
        if ( request.status == 200 )
        {
            doc = request.responseXML;
            tree = doc.evaluate("/lfm");
            status = tree.item(0).getAttribute("status")
            if(status == "ok")
            {
                print ("status is ok!");
                tracks = doc.evaluate("/lfm/recenttracks[1]/track");
                for(i=0;i<tracks.length;i++)
                {
                    artist = tracks.item(i).firstChild.firstChild.data;
                }
            }
        }
    }

This way you can get the node artist out of the tree. there's a problem though if you want to have the next sibling. You have to call

tracks.item(i).firstChild.nextSibling.firstChild.data;
tracks.item(i).firstChild.nextSibling.nextSibling.firstChild.data;
tracks.item(i).firstChild.nextSibling.nextSibling.nextSibling.firstChild.data;

to get this done. The node next to that adds a 'nextsibling' to it and so on. I do not want to keep on adding these nodes and thought it would be possible to use childNodes[i] like this:

artist = tracks.item(i).childNodes[0].firstChild.data;
nextitem = tracks.item(i).childNodes[1].firstChild.data;

this doesn't work though. it returns "childNodes[0] has no properties" in whatever way I use it.
Now I think there's also a way in Xpath to do this in a for-loop:

name = doc.evaluate("string(/lfm/recenttracks[1]/track["+i+"]/name)");
                    print(name);
othernode = doc.evaluate("string(/lfm/recenttracks[1]/track["+i+"]/album)");
                    print(othernode);

and then increasing i for the next track.
but somehow this returns only one item. it doesn't retrieve more items in a for-loop. i-1 doesn't work either.

Anyone knows how to use a Xpath expression with my i value to choose a node and then get the subnodes per supernode? Per track I want to get artist, name, streamable, mbid, album, url, image(small), image(medium), image(large) and date.

my xml file looks like this:

<lfm status="ok">
   <recenttracks user="xaddict">
      <track nowplaying="true"> 
         <artist mbid="f5b8ea5f-c269-45dd-9936-1fedf3c56851">The Presets</artist>
         <name>Girl (You Chew My Mind Up)</name>
         <streamable>1</streamable>
         <mbid></mbid>
         <album mbid="b150d099-b0f3-4feb-9a05-34e693c6dd24">Beams</album>
         <url>http://www.last.fm/music/The+Presets/_/Girl+%28You+Chew+My+Mind+Up%29</url>
         <image size="small">http://userserve-ak.last.fm/serve/34s/8696437.jpg</image>
         <image size="medium">http://userserve-ak.last.fm/serve/64s/8696437.jpg</image>
         <image size="large">http://userserve-ak.last.fm/serve/126/8696437.jpg</image>
         <date uts="1236440600">7 Mar 2009, 15:43</date>
      </track>
      <track > 
         <artist mbid="f5b8ea5f-c269-45dd-9936-1fedf3c56851">The Presets</artist>
         <name>Get Outta Here</name>
         <streamable>1</streamable>
         <mbid></mbid>
         <album mbid="0469956f-d895-4120-8ec5-29ad41b9e2fd">Blow Up</album>
         <url>http://www.last.fm/music/The+Presets/_/Get+Outta+Here</url>
         <image size="small">http://userserve-ak.last.fm/serve/34s/20923179.png</image>
         <image size="medium">http://userserve-ak.last.fm/serve/64s/20923179.png</image>
         <image size="large">http://userserve-ak.last.fm/serve/126/20923179.png</image>
         <date uts="1236440242">7 Mar 2009, 15:37</date>
      </track>
   </recenttracks>
</lfm>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不可一世的女人 2024-07-22 03:00:30

 for (var i = 0; i < xmlDoc.getElementsByTagName('track').length; i++)
            {
                alert(xmlDoc.getElementsByTagName('track')[i].getElementsByTagName('name')[0].childNodes[0].nodeValue); 
            }

如果你想使用xpath,也可以尝试下面的解决方案

var s = xmlDoc.evaluate( '//track' ,xmlDoc, null, XPathResult.ANY_TYPE, null );
var track = s.iterateNext();

while (track)
{
     alert(track.getElementsByTagName('name')[0].textContent );
     track = s.iterateNext();
}

Try this

 for (var i = 0; i < xmlDoc.getElementsByTagName('track').length; i++)
            {
                alert(xmlDoc.getElementsByTagName('track')[i].getElementsByTagName('name')[0].childNodes[0].nodeValue); 
            }

if you want to use xpath, you can also try the below solution.

var s = xmlDoc.evaluate( '//track' ,xmlDoc, null, XPathResult.ANY_TYPE, null );
var track = s.iterateNext();

while (track)
{
     alert(track.getElementsByTagName('name')[0].textContent );
     track = s.iterateNext();
}
我偏爱纯白色 2024-07-22 03:00:30

您能发布相关的 XML 片段吗? 我们正在查看解析它的代码,但如果我们有 XML 本身,那么使用 Xpath 查询会更容易。
元素的位

<lfm>
    <recenttracks>
        <track>
            <album>

最有用的是具有诸如等

Can you post the relevant XML fragment? We're looking at your code that parses it, but if we had the XML itself, it'd be easier to cons up an Xpath query.
Most useful would be the bit that has elements like

<lfm>
    <recenttracks>
        <track>
            <album>

etc

寂寞笑我太脆弱 2024-07-22 03:00:30

完全支持 Xpath 的方法是使用 Xpath 中的字符串转换。 我已经发布此代码作为另一个问题的答案,并将再次这样做。 我很高兴我发现了这种做事方式。

var entries = xmlDoc.evaluate("lfm/recenttracks/track");
var length = entries.length;
for(var i = 0; i < length; i++) {
   var entry = entries.item(i);
   var obj = {
      artist: entry.evaluate("string(artist)"),
      name: entry.evaluate("string(name)"),
      url: entry.evaluate("string(url)"),
      image: entry.evaluate("string(image[@size='medium'])")
   };
posts[i] = obj;
}

the fully Xpath-enabled way of doing this is using the string conversion in Xpath. Ive already posted this code as an answer to another question and will do this again. I'm very glad I found out about this way of doing things.

var entries = xmlDoc.evaluate("lfm/recenttracks/track");
var length = entries.length;
for(var i = 0; i < length; i++) {
   var entry = entries.item(i);
   var obj = {
      artist: entry.evaluate("string(artist)"),
      name: entry.evaluate("string(name)"),
      url: entry.evaluate("string(url)"),
      image: entry.evaluate("string(image[@size='medium'])")
   };
posts[i] = obj;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文