如何使用 javascript 将新属性动态插入到 xml 标记中?

发布于 2024-10-20 13:22:15 字数 1780 浏览 2 评论 0原文

我有一些如下所示的 xml:

<dict>
        <key>Track ID</key>
        <integer>4896</integer>
        <key>Name</key>
        <string>Let 'cha Boy Go (DIRTY) F/B.o.B</string>
        <key>Artist</key>
        <string>4-Ize</string>
        <key>Grouping</key>
        <string>www.2dopeboyz.com</string>
        <key>Genre</key>
        <string>Hip-Hop</string>
        <key>Kind</key>
        <string>MPEG audio file</string>
        <key>Size</key>
        <integer>5837048</integer>
        <key>Total Time</key>
        <integer>243121</integer>
        <key>Year</key>
        <integer>2010</integer>
        <key>BPM</key>
        <integer>80</integer>
        <key>Date Modified</key>
        <date>2010-09-05T05:51:23Z</date>
        <key>Date Added</key>
        <date>2010-09-04T15:37:43Z</date>
        <key>Bit Rate</key>
        <integer>192</integer>
        <key>Sample Rate</key>
        <integer>44100</integer>
        <key>Comments</key>
        <string>80.3</string>
        <key>Normalization</key>
        <integer>7646</integer>
        <key>Persistent ID</key>
        <string>D9FAAD3E0203FA18</string>
        <key>Track Type</key>
        <string>File</string>
        <key>Location</key>
    </dict>

How do I write the JavaScript to insert attribute on certain labels?

I have some xml like the following:

<dict>
        <key>Track ID</key>
        <integer>4896</integer>
        <key>Name</key>
        <string>Let 'cha Boy Go (DIRTY) F/B.o.B</string>
        <key>Artist</key>
        <string>4-Ize</string>
        <key>Grouping</key>
        <string>www.2dopeboyz.com</string>
        <key>Genre</key>
        <string>Hip-Hop</string>
        <key>Kind</key>
        <string>MPEG audio file</string>
        <key>Size</key>
        <integer>5837048</integer>
        <key>Total Time</key>
        <integer>243121</integer>
        <key>Year</key>
        <integer>2010</integer>
        <key>BPM</key>
        <integer>80</integer>
        <key>Date Modified</key>
        <date>2010-09-05T05:51:23Z</date>
        <key>Date Added</key>
        <date>2010-09-04T15:37:43Z</date>
        <key>Bit Rate</key>
        <integer>192</integer>
        <key>Sample Rate</key>
        <integer>44100</integer>
        <key>Comments</key>
        <string>80.3</string>
        <key>Normalization</key>
        <integer>7646</integer>
        <key>Persistent ID</key>
        <string>D9FAAD3E0203FA18</string>
        <key>Track Type</key>
        <string>File</string>
        <key>Location</key>
    </dict>

How do I write the JavaScript to insert attributes on certain tags?

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

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

发布评论

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

评论(1

脸赞 2024-10-27 13:22:15

您可以使用 XmlDOM 加载 xml,使用 XPath 查找元素,然后进行 DOM 操作来修改它:

var xpath = "/dict/key[. = 'Name']";
var element = null;

// IE:
if (ie)
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML("<dict><key>Track ID</key><integer>4896</integer><key>Name</key><string>Let 'cha Boy Go (DIRTY) F/B.o.B</string></dict>"); 

    // Find the element using xpath.
    var values = xmlDoc.selectNodes(xpath);
    element = values[0];
}
// Others:
else
{
    parser = new DOMParser();
    xmlDoc = parser.parseFromString("<dict><key>Track ID</key><integer>4896</integer><key>Name</key><string>Let 'cha Boy Go (DIRTY) F/B.o.B</string></dict>", "text/xml");

    // Find the element using xpath.    
    var values = xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null);
    element = values.iterateNext();
}

// Change/add the id of the selected element.
element.setAttribute("id", "aga");

You can load the xml using XmlDOM, use XPath to find an element and then DOM manipulation to modify it:

var xpath = "/dict/key[. = 'Name']";
var element = null;

// IE:
if (ie)
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML("<dict><key>Track ID</key><integer>4896</integer><key>Name</key><string>Let 'cha Boy Go (DIRTY) F/B.o.B</string></dict>"); 

    // Find the element using xpath.
    var values = xmlDoc.selectNodes(xpath);
    element = values[0];
}
// Others:
else
{
    parser = new DOMParser();
    xmlDoc = parser.parseFromString("<dict><key>Track ID</key><integer>4896</integer><key>Name</key><string>Let 'cha Boy Go (DIRTY) F/B.o.B</string></dict>", "text/xml");

    // Find the element using xpath.    
    var values = xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null);
    element = values.iterateNext();
}

// Change/add the id of the selected element.
element.setAttribute("id", "aga");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文