如何将XML文档转换为Js String?

时间:2017-07-16 16:50:09

标签: javascript xml string

我需要这个XML文档(register.xml):

<?xml version="1.0" encoding="utf-8"?>
<All>
<Shoe>
    <Name>All Stars</Name>
    <BrandName>Converse</BrandName>
    <ReleaseDate>10/2/08</ReleaseDate>
</Shoe>
<Shoe>
    <Name>All Star1s</Name>
    <BrandName>Converse1</BrandName>
    <ReleaseDate>11/2/08</ReleaseDate>
</Shoe>

存储在此Javascript变量中:

var ShoesXML

因为我实际上是通过这种方式从这个包含XML数据的Js变量创建一个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></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" + 
            "</tr>";
        $("table.shoetable tbody").append(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></Shoe></All>";

我手动设置了值,但是我需要从实际的(register.xml)文档中获取它,然后将其存储在:

var ShoesXML

供以后使用。

0 个答案:

没有答案