[英]Cannot parse xml data with javascript HTML DOM on local system
I tried this sample code from tutorialspoint website on a number of browsers.我在许多浏览器上尝试了来自 tutorialspoint 网站的示例代码。 But the xml data is not being parsed.
但是没有解析 xml 数据。 Both these files are on my local system and the address.xml file is inside a folder "xml".
这两个文件都在我的本地系统上,address.xml 文件位于文件夹“xml”中。
How can I parse data in javascript from the xml files on my local system?如何从本地系统上的 xml 文件中解析 javascript 中的数据?
Here is my HTML file sample.html from tutorialspoint website:这是我的 HTML 文件 sample.html 来自 tutorialspoint 网站:
<!DOCTYPE html>
<html>
<body>
<h1>TutorialsPoint DOM example </h1>
<div>
<b>Name:</b> <span id="name"></span><br>
<b>Company:</b> <span id="company"></span><br>
<b>Phone:</b> <span id="phone"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/xml/address.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.getElementById("name").innerHTML=
xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
document.getElementById("company").innerHTML=
xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue;
document.getElementById("phone").innerHTML=
xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
This is the xml data file address.xml:这是xml数据文件地址。xml:
<?xml version="1.0"?>
<contact-info>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</contact-info>
Update: The problem was with getting an http response on local system.更新:问题在于在本地系统上获得 http 响应。 It is solved after I installed XAMPP.
我安装XAMPP后就解决了。
use parsing like the following 使用解析如下
if (window.DOMParser)
{
parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlhttp.responseXML, "text/xml");
}
else // Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlhttp.responseXML);
}
This example of XML parsing, if you have xml value in variable. 这个XML解析的例子,如果你在变量中有xml值。
Sample = "<contact-info><name>Tanmay Patil</name><company>TutorialsPoint</company><phone>(011) 123-4567</phone></contact-info>"; if (window.DOMParser) { parser = new DOMParser(); xmlDoc = parser.parseFromString(Sample, "text/xml"); } document.getElementById("name").innerHTML= xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue; document.getElementById("company").innerHTML= xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue; document.getElementById("phone").innerHTML= xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <b>Name:</b> <span id="name"></span><br> <b>Company:</b> <span id="company"></span><br> <b>Phone:</b> <span id="phone"></span>
As Jaromanda X mentioned in the comments, and as I found out the hard way, Chrome won't parse local xml files using the DOM parser.正如评论中提到的 Jaromanda X 以及我发现的困难方法,Chrome 不会使用 DOM 解析器解析本地 xml 文件。 However, if you directly use the console and save your xml text there (as a var/let,etc..) you will be able to parse it using the DOM parser.
但是,如果您直接使用控制台并将 xml 文本保存在那里(作为 var/let 等),您将能够使用 DOM 解析器解析它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.