日期:2014-05-16  浏览次数:20391 次

问一个读取xml,显示节点的问题
test.xml

<?xml   version= "1.0 "   encoding= "UTF-8 "   ?>  
<zip>
<city>
    <eric> a </eric>  
    <eric> b </eric>  
    <eric> c </eric>  
    <eric> d </eric>  
    <eric> e </eric>  
    </city>

    <state> mystate </state>  
</zip>

请问用javascript怎么样把city下面的所有eric读取出来显示在页面上

------解决方案--------------------
var xml = new ActiveXObject( "Microsoft.XMLDOM ");
var xmlobj = xml.load( "test.xml ");
var xmlnodes = xmlobj.getElementsByTagName( "city ");
for (var i=0; i <xmlnodes.childNodes.length;i++){
document.write(xmlnodes.childNodes[i].text);
}
------解决方案--------------------
<script language= "javascript ">
var xmlDom=new ActiveXObject( "MSXML2.DOMDocument.3.0 ");
xmlDom.async= "false ";
xmlDom.load( "test.xml ");


var xmlObj=xmlDom.documentElement.childNodes;
for(var i=0;i <xmlObj.length;i++)
{

if(xmlObj.item(i).hasChildNodes)
{
xmlSubNode=xmlObj.item(i).childNodes;
for(var j=0;j <xmlSubNode.length;j++)
{
document.write(xmlSubNode.item(j).text);
}
------解决方案--------------------
将document.write(xmlSubNode.item(j).text);替换为:
document.write(xmlSubNode.item(j).attributes.item(0).value);