XML代码:317.xml
<?xml version="1.0" encoding="UTF-8"?>
<ADI>
     <Objects>
         <Object ElementType="Program" >
          <Property Name="Name">大兵小将</Property>
          <Property Name="Language">汉语</Property>
        </Object>
    <Objects>
  
    <Mappings>
       <Mapping ParentType="Program"> 
        123
       </Mapping>
    <Mappings>
</ADI>js代码:
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js"></script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("317.xml");
x=xmlDoc.getElementsByTagName("Property")[0].childNodes[0];
txt=x.nodeValue;
document.write(txt);
</script>
</body>
</html>loadxmldoc.js导入xml文档没有问题。
解析出来为何得不到:大兵小将?
如果显示的不是中文,请问该怎么办?

解决方案 »

  1.   

    alert(xmlDoc)
    看看对象加载成功没有如果显示的不是中文,请问该怎么办?
    将文件另存为utf-8编码
      

  2.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script>
    function test(){  
    var xmlDom;     //XML DOM object
    var xmlFile="ff.xml";    //xml file name
    loadXML = function(fileRoute){
    xmlDoc=null;
    if (window.ActiveXObject){
    xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
    xmlDoc.async=false;
    xmlDoc.load(fileRoute);
    }else if (document.implementation && document.implementation.createDocument){
    var xmlhttp = new window.XMLHttpRequest();
    xmlhttp.open("GET",fileRoute,false);
    xmlhttp.send(null);
    var xmlDoc = xmlhttp.responseXML;
    }
    else {xmlDoc=null;}
    return xmlDoc;
    }
    xmlDom=loadXML(xmlFile);
    var newsList2=xmlDom.getElementsByTagName("ADI");
    for(var i = 0;i<newsList2.length;i++){
    var text = newsList2[0].getElementsByTagName("Property")[0].childNodes[0].nodeValue;
    alert(text)
    }
    }
    test();
    </script>
    </head><body>
    </body>
    </html>
    你的xml标签都不匹配应该这样写<?xml version="1.0" encoding="UTF-8"?>  
    <ADI>
      <Objects>
      <Object ElementType="Program" >
      <Property Name="Name">大兵小将</Property>
      <Property Name="Language">汉语</Property>
      </Object>
      </Objects>
       
      <Mappings>
      <Mapping ParentType="Program">123</Mapping>
      </Mappings>
    </ADI>