使用angular从xml中提取标签

时间:2017-08-05 01:36:51

标签: javascript angularjs xml

我有以下格式的XML:

<lfm status="ok">
<artisttracks user="RJ" artist="shakira" page="1" perPage="50" totalPages="0" total="0">
<track>
<artist mbid="bf24ca37-25f4-4e34-9aec-460b94364cfc">Shakira</artist>
<name>Can't Remember to Forget You</name>
<streamable>0</streamable>
<mbid/>
<album mbid="">Shakira. (Deluxe Version)</album>
<url>
https://www.last.fm/music/Shakira/_/Can%27t+Remember+to+Forget+You
</url>
<image size="small">
https://lastfm-img2.akamaized.net/i/u/34s/6f52cd9bdf564954c9df5a574db26cc4.png
</image>
<image size="medium">
https://lastfm-img2.akamaized.net/i/u/64s/6f52cd9bdf564954c9df5a574db26cc4.png
</image>
<image size="large">
https://lastfm-img2.akamaized.net/i/u/174s/6f52cd9bdf564954c9df5a574db26cc4.png
</image>
<image size="extralarge">
https://lastfm-img2.akamaized.net/i/u/300x300/6f52cd9bdf564954c9df5a574db26cc4.png
</image>
<date uts="1472405290">28 Aug 2016, 17:28</date>
</track>

如何使用ng-repeat提取曲目名称? 我尝试了以下

<table class="table">
    <tr ng-repeat="track in model.tracks">
    <tr ng-repeat="name in model.artisttrack">
        <td>
            {{track.name}}
        </td>
    </tr>
    </tr>
</table>

显示整个xml文件。

1 个答案:

答案 0 :(得分:0)

所有现代浏览器都有一个内置的XML解析器,可以将文本转换为XML DOM对象。

var text, parser, xmlDoc;

text = "<bookstore><book>" +
    "<title>Everyday Italian</title>" +
    "<author>Giada De Laurentiis</author>" +
    "<year>2005</year>" +
    "</book></bookstore>";

parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");

document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;

另外,请查看x2js

w3schools