Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX将多个XML文件保存到阵列_Javascript_Html_Ajax_Xml - Fatal编程技术网

Javascript 使用AJAX将多个XML文件保存到阵列

Javascript 使用AJAX将多个XML文件保存到阵列,javascript,html,ajax,xml,Javascript,Html,Ajax,Xml,我目前还不熟悉XML和AJAX,我只是在一些方面遇到了困难。我试图解析2个XML文件并将其保存到单独的数组中,然后在HTML文件中打印出来。我已将它们保存到数组中,但由于某种原因它无法工作?我一直在阅读和研究这个话题,但仍在为整个概念而挣扎。有人能告诉我我做错了什么吗(提前谢谢你的帮助,我真的非常感谢!) 变量类别、c_id、c_名称; var prod、p_id、p_名称、p_cid; 函数loadXMLDocs() { loadXMLDoc(“categories.xml”、“catego

我目前还不熟悉XML和AJAX,我只是在一些方面遇到了困难。我试图解析2个XML文件并将其保存到单独的数组中,然后在HTML文件中打印出来。我已将它们保存到数组中,但由于某种原因它无法工作?我一直在阅读和研究这个话题,但仍在为整个概念而挣扎。有人能告诉我我做错了什么吗(提前谢谢你的帮助,我真的非常感谢!)


变量类别、c_id、c_名称;
var prod、p_id、p_名称、p_cid;
函数loadXMLDocs()
{
loadXMLDoc(“categories.xml”、“categories”);
loadXMLDoc(“products.xml”、“products”);
}
函数loadXMLDoc(url,元素)
{
var-xmlhttp;
变量i,x,xx;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
//txt=“TitleArtist”;
x=xmlhttp.responseXML.documentElement.getElementsByTagName(元素);
如果(元素==“类别”){
cat=x;
}
如果(元素=“产品”){
prod=x;
}

例如(i=0;iDear Ben,告诉我们您尝试过什么。如果您发布一些代码来显示您正在尝试做什么,这可能有助于让您的问题更清楚。或者更详细地说:您到底在哪里挣扎?它一定比myarray[0]=myxml更清楚;不是吗?;)很抱歉,我无意中发布了没有代码的问题!!代码已包含在编辑中
<!DOCTYPE html>
<html>
<head>
<script>
var cat, c_id, c_name;
var prod, p_id, p_name, p_cid;
function loadXMLDocs()
{
loadXMLDoc("categories.xml", "Categories");
loadXMLDoc("products.xml", "Products");
}

function loadXMLDoc(url, element)
{
var xmlhttp;
var i, x, xx;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //txt="<table border='1'><tr><th>Title</th><th>Artist</th></tr>";
    x=xmlhttp.responseXML.documentElement.getElementsByTagName(element);

    if (element == "Categories") {
      cat = x;
    }
    if (element == "Products") {
      prod = x;
    }

    for (i=0;i<x.length;i++)
      {
      txt=txt + "<tr>";

      xx=cat[i].getElementsByTagName("CategoryName");
      c_name = xx;
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch {
            document.write("doesnt work")
          }
        }
    xx=prod[i].getElementsByTagName("ProductName");
    p_name = xx;
      {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
          catch {
            document.write("doesnt work")
          }
        }
      txt=txt + "</tr>";
      }
    txt=txt + "</table>";
    document.getElementById('txtxmlInfo').innerHTML=txt;
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
}

</script>
</head>

<body>

<h2>Product List</h2>
<div id="txtxmlInfo">
<button type="button" onclick=loadXMLDoc"(categories.xml", "Categories)";>Get Products</button>

</body>
</html>