使用Javascript解析xml文件时,web浏览器不显示任何内容
我正在尝试使用java脚本解析一个简单的xml文件。当我将文件加载到webrowser“Chrome”中时,页面上什么也没有显示。 请让我知道我犯了什么错误 xml文件:使用Javascript解析xml文件时,web浏览器不显示任何内容,javascript,xml,Javascript,Xml,我正在尝试使用java脚本解析一个简单的xml文件。当我将文件加载到webrowser“Chrome”中时,页面上什么也没有显示。 请让我知道我犯了什么错误 xml文件: < ?xml version="1.0" encoding="UTF-8" ?> <company> <employee id="001" >John</employee> <turnover> <year id="2000"&
< ?xml version="1.0" encoding="UTF-8" ?>
<company>
<employee id="001" >John</employee>
<turnover>
<year id="2000">100,000</year>
<year id="2001">140,000</year>
<year id="2002">200,000</year>
</turnover>
</company>
Read XML in Microsoft Browsers</title>
<script type="text/javascript">
var xmlDoc;
function loadxml()
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.onreadystatechange = readXML; /* to be notified when the state changes */
xmlDoc.load("C:\Users\Amr\Desktop\files\xml.xml");
}
function readXML()
{
if(xmlDoc.readyState == 4)
{
//Using documentElement Properties
//Output company
alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);
//Using firstChild Properties
//Output year
alert("First Child: " + xmlDoc.documentElement.childNodes[1].firstChild.tagName);
//Using lastChild Properties
//Output average
alert("Last Child: " + xmlDoc.documentElement.childNodes[1].lastChild.tagName);
//Using nodeValue and Attributes Properties
//Here both the statement will return you the same result
//Output 001
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes[0].nodeValue);
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes.getNamedItem("id").nodeValue);
//Using getElementByTagName Properties
//Here both the statement will return you the same result
//Output 2000
alert("getElementsByTagName: " + xmlDoc.getElementsByTagName("year")[0].attributes.getNamedItem("id").nodeValue);
//Using text Properties
//Output John
alert("Text Content for Employee Tag: " + xmlDoc.documentElement.childNodes[0].text);
//Using hasChildNodes Properties
//Output True
alert("Checking Child Nodes: " + xmlDoc.documentElement.childNodes[0].hasChildNodes);
}
约翰
100,000
140,000
200,000
JAvaScript解析器:
< ?xml version="1.0" encoding="UTF-8" ?>
<company>
<employee id="001" >John</employee>
<turnover>
<year id="2000">100,000</year>
<year id="2001">140,000</year>
<year id="2002">200,000</year>
</turnover>
</company>
Read XML in Microsoft Browsers</title>
<script type="text/javascript">
var xmlDoc;
function loadxml()
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.onreadystatechange = readXML; /* to be notified when the state changes */
xmlDoc.load("C:\Users\Amr\Desktop\files\xml.xml");
}
function readXML()
{
if(xmlDoc.readyState == 4)
{
//Using documentElement Properties
//Output company
alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);
//Using firstChild Properties
//Output year
alert("First Child: " + xmlDoc.documentElement.childNodes[1].firstChild.tagName);
//Using lastChild Properties
//Output average
alert("Last Child: " + xmlDoc.documentElement.childNodes[1].lastChild.tagName);
//Using nodeValue and Attributes Properties
//Here both the statement will return you the same result
//Output 001
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes[0].nodeValue);
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes.getNamedItem("id").nodeValue);
//Using getElementByTagName Properties
//Here both the statement will return you the same result
//Output 2000
alert("getElementsByTagName: " + xmlDoc.getElementsByTagName("year")[0].attributes.getNamedItem("id").nodeValue);
//Using text Properties
//Output John
alert("Text Content for Employee Tag: " + xmlDoc.documentElement.childNodes[0].text);
//Using hasChildNodes Properties
//Output True
alert("Checking Child Nodes: " + xmlDoc.documentElement.childNodes[0].hasChildNodes);
}
在Microsoft浏览器中读取XML
var-xmlDoc;
函数loadxml()
{
xmlDoc=新的ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=false;
xmlDoc.onreadystatechange=readXML;/*在状态更改时收到通知*/
load(“C:\Users\Amr\Desktop\files\xml.xml”);
}
函数readXML()
{
if(xmlDoc.readyState==4)
{
//使用documentElement属性
//输出公司
警报(“XML根标记名:”+xmlDoc.documentElement.tagName);
//使用firstChild属性
//产出年
警报(“第一个子节点:+xmlDoc.documentElement.childNodes[1].firstChild.tagName”);
//使用lastChild属性
//平均产量
警报(“最后一个子节点:+xmlDoc.documentElement.childNodes[1].lastChild.tagName”);
//使用节点值和属性
//在这里,这两条语句将返回相同的结果
//输出001
警报(“节点值:”+xmlDoc.documentElement.childNodes[0]。属性[0]。节点值);
警报(“节点值:”+xmlDoc.documentElement.childNodes[0].attributes.getNamedItem(“id”).nodeValue);
//使用getElementByTagName属性
//在这里,这两条语句将返回相同的结果
//2000年产出
警报(“getElementsByTagName:+xmlDoc.getElementsByTagName(“年份”)[0]。attributes.getNamedItem(“id”).nodeValue);
//使用文本属性
//输出约翰
警报(“员工标记的文本内容:+xmlDoc.documentElement.childNodes[0].Text”);
//使用hasChildNodes属性
//输出真值
警报(“正在检查子节点:+xmlDoc.documentElement.childNodes[0].hasChildNodes”);
}
这是因为您使用ActiveX对象进行解析,因此它仅在IE浏览器中可用。由于您是从文件加载XML,因此请改用XMLHttpRequest。从第7版开始,它与chrome、firefox、safari等一起在IE中实现。
下面是一个很好的解释和一些演示代码:
如果您查看console下的开发者工具栏,它会给您带来什么错误消息?请查看随附的图像,这是您收到的安全消息,因为您正在加载本地文件,这不是错误。请查看本文中的答案,以允许使用ActiveX加载本地文件: