如何从特定的xml String节点设置值?

时间:2017-07-16 04:21:06

标签: javascript xml string set

我已经读过字符串并从中构建一个html表:

var ShoesXML = "<All><Shoe><Name>All Stars</Name><BrandName>Converse</BrandName><ReleaseDate>10/2/08</ReleaseDate><Picture>pic.jpg</Picture></Shoe><Shoe><Name>All Star1s</Name><BrandName>Converse1</BrandName><ReleaseDate>11/2/08</ReleaseDate><Picture>pic.jpg</Picture></Shoe></All>";

$(document).ready(function() {  
xmlDoc=$.parseXML( ShoesXML );
$(xmlDoc).find("Shoe").each(function(i, n) {
    var html = "<tr>\n" + 
        "<td><span>" + $(n).find("Name").text() + "</span></td>\n" +
        "<td>" +  $(n).find("BrandName").text() + "</td>\n" +
        "<td>" + $(n).find("ReleaseDate").text() + "</td>\n" + 
        "<td><img src='" + $(n).find("Picture").text() + "'></td>\n" +
        "</tr>";
    $("table.shoetable tbody").append(html);
});
});

我尝试以这种方式设置值,但没有成功:

$(n).find("Name").text("NEW VALUE")

1 个答案:

答案 0 :(得分:1)

在构建.textContext字符串

之前设置HTML
    $(n).find("Name").text("NEW VALUE")

    var html = "<tr>\n" + 
    "<td><span>" + $(n).find("Name").text() + "</span></td>\n" +
    "<td>" +  $(n).find("BrandName").text() + "</td>\n" +
    "<td>" + $(n).find("ReleaseDate").text() + "</td>\n" + 
    "<td><img src='" + $(n).find("Picture").text() + "'></td>\n" +
    "</tr>";