简体   繁体   English

使用JavaScript解析XML文件

[英]Using JavaScript to parse an XML file

I am new to Stack OverFlow and coding in general. 我是Stack OverFlow和编码的新手。 I am trying to take an XML file and render it in the browser using JavaScript. 我正在尝试获取XML文件并使用JavaScript在浏览器中呈现它。 I have looked around at some sample code of how to do this and came up with the following code: 我查看了一些如何执行此操作的示例代码,并提出了以下代码:

<!DOCTYPE html>
<html>
<body>

<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","social.xml",false);
  xmlhttp.send();
  xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
  { 
  document.write("<tr><td>");
  document.write(x[i].getElementsByTagName("c_id")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("facebook_id")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
  document.write("</table>");
</script>

</body>
</html>

Anyway, when I run this on my local server none of the data that I am trying to display in the table appears. 无论如何,当我在本地服务器上运行它时,我试图在表中显示的数据都不会出现。 My .html file and .xml file are in the same folder, so I believe I have the correct file pathway. 我的.html文件和.xml文件在同一个文件夹中,所以我相信我有正确的文件路径。 I could just be making a rookie mistake here, but I can't for the life of me figure out why a table listing the c_id and facebook_id values is not being created. 我可能只是在这里犯了一个菜鸟错误,但我不能为我的生活弄清楚为什么没有创建列出c_id和facebook_id值的表。 I looked around for answers and haven't been able to find any. 我四处寻找答案,但却找不到任何答案。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

You need to add an onload event listener to the xmlhttprequest before sending the request. 在发送请求之前,您需要向xmlhttprequest添加onload事件侦听器。 Also, you might need to parse the XML with a DOMParser . 此外,您可能需要使用DOMParser解析XML。 Anyway, this should work on modern browsers: 无论如何,这应该适用于现代浏览器:

<!DOCTYPE html>
<html>
    <body>

        <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.onload = function() {
                var xmlDoc = new DOMParser().parseFromString(xmlhttp.responseText,'text/xml');

                console.log(xmlDoc);

                document.write("<table border='1'>");
                var x=xmlDoc.getElementsByTagName("CD");
                for (i=0;i<x.length;i++)
                { 
                    document.write("<tr><td>");
                    document.write(x[i].getElementsByTagName("c_id")[0].childNodes[0].nodeValue);
                    document.write("</td><td>");
                    document.write(x[i].getElementsByTagName("facebook_id")[0].childNodes[0].nodeValue);
                    document.write("</td></tr>");
                }
                document.write("</table>");

            }


            xmlhttp.open("GET","social.xml",false);
            xmlhttp.send();
            </script>

    </body>
</html>

Now, just a couple of things worth mentioning about what you're doing: 现在,只有几件值得一提的关于你正在做的事情:

  • xmlhttprequest objects have many different parameters that mean a variety of things: readystate , status code, the works. xmlhttprequest对象有许多不同的参数,意味着各种各样的东西: readystate ,status code,works。 You might benefit looking a bit more into those. 您可能会从中获益更多。

  • document.write should really never be used, ever. 永远不应该使用document.write In fact, any means of HTML injection should be handled very carefully. 事实上,任何HTML注入方式都应该非常谨慎地处理。 You could use a template-based solution common in many MVC-esque frameworks, or mine if you want :) 您可以使用许多MVC-esque框架中常见的基于模板的解决方案,如果您愿意,可以使用我的 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM