需要帮助使用javascript解析xml

时间:2022-04-10 05:06:36

First let me thank you for the assistance, I am new to Javascript, and want to learn to parse a >.xml file into my javascript. The file I want to parse is contact.xml, located in my root folder. Again, thank you.

首先,我要感谢您的帮助,我是Javascript的新手,并希望学习将一个> .xml文件解析为我的javascript。我要解析的文件是contact.xml,位于我的根文件夹中。再次谢谢你。

<!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>
<script type="text/javascript">
function loadXMLDoc(XMLname)
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",XMLname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(XMLname);
return xmlDoc;
}
alert("Error loading document!");
return null;
}
<title>Contacts</title>
</script>
</head>

<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("contactinfo.xml") // Path to the XML file;
var M = xmlDoc.getElementsByTagName("item");
for (i=0;i<M.length;i++){
document.write("<div style='width:450px;'>")
document.write("<h2>"+xmlDoc.getElementsByTagName("item")[i].childNodes[0].nodeValue+"</h2>");
document.write("<p>" + xmlDoc.getElementsByTagName("servicephone")[i].childNodes[0].nodeValue+    "</p>");
document.write("<p><a href='" + xmlDoc.getElementsByTagName("email")[i].childNodes[0].nodeValue   +"</p>);
document.write("</div>")
}
</script>

</body>
</html>


*Here is my .xml file*

<?xml version="1.0" encoding="utf-8" ?>
<Contacts>
<item servicephone="(800) 500-0066" 
email="customerservice@fsig.com" 
url="http://www.fsig.com" 
address="5000 Barcilona Beach Rd. Wilmington, NC 28000">
</item>
</Contacts>

1 个解决方案

#1


1  

You need to go down the hierarchy, so, first find the Contacts node, then inside there you can get all the tagnames as you have.

您需要沿着层次结构向下,因此,首先找到“联系人”节点,然后在那里,您可以获得所有标记名。

You have a great deal of attributes so you may find this useful also:

您有很多属性,因此您可能会发现这也很有用:

node.attributes["url"].nodeValue

So just loop through all the items, then I would just copy itemelem[t] to node just to make it easier, then you get the attributes you need.

所以只需循环遍历所有项目,然后我只是将itemelem [t]复制到节点只是为了使它更容易,然后你得到你需要的属性。

Depending on the browser you are using most of them come with some javascript debugger, so you can put in breakpoints and look at the values in the variables and see what the next step needs to be.

根据您使用的浏览器,大多数都带有一些javascript调试器,因此您可以放入断点并查看变量中的值,并查看下一步需要执行的操作。

#1


1  

You need to go down the hierarchy, so, first find the Contacts node, then inside there you can get all the tagnames as you have.

您需要沿着层次结构向下,因此,首先找到“联系人”节点,然后在那里,您可以获得所有标记名。

You have a great deal of attributes so you may find this useful also:

您有很多属性,因此您可能会发现这也很有用:

node.attributes["url"].nodeValue

So just loop through all the items, then I would just copy itemelem[t] to node just to make it easier, then you get the attributes you need.

所以只需循环遍历所有项目,然后我只是将itemelem [t]复制到节点只是为了使它更容易,然后你得到你需要的属性。

Depending on the browser you are using most of them come with some javascript debugger, so you can put in breakpoints and look at the values in the variables and see what the next step needs to be.

根据您使用的浏览器,大多数都带有一些javascript调试器,因此您可以放入断点并查看变量中的值,并查看下一步需要执行的操作。