javascript xml阅读器

编程入门 行业动态 更新时间:2024-10-26 06:32:00
本文介绍了javascript xml阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我会保持这么短:我正在尝试通过一个库的xml文档循环。我有一个应该有效的脚本,但没有。任何人都可以告诉我我哪里做错了吗?

I will keep this very short: I'm trying to make a loop through an xml-document for a gallery. I got a script that should work, but doesn't. Can anyone please tell me where I did wrong?

我不想让这个更长,因为这个问题很简单,从昨天起就一直在思考这个问题,这是我得到的最接近。

I didn't want to make this longer because the problem is simple and have been pondering over this since yesterday and this is the closest I get.

我想循环遍历xml文件并首先打印出path和file。我正在构建一个库,并认为保存图像的所有数据的最佳方法是xml文件,但现在我无法正确循环。在脚本中,我使页面打印出x和i,结果是x为1而i为0,因此在我看来它根本没有通过for循环。

I want to loop through the xml-file and print out "path" and "file" first and most. I'm building a gallery and thought that the best way to save all the data for the images was an xml-file, but now I can't get it to loop correctly. In the script I made the page print out both x and i, which resulted with x being 1 and i being 0, hence it hasn't worked through the for-loop at all as I see it.

任何帮助都会受到赞赏,因为我被困住了。一直在尝试这么多解决方案让我的脑袋在旋转,如果没有朝着正确的方向轻推,我就无法进一步。

Any help would be appreciated, because I'm stuck. Been trying so many solutions that my head is spinning and I can't get any further without a nudge in the right direction.

html / javascript:

The html/javascript:

<script type="text/javascript"> function displayResult() { 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","../gallery/gallery.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; x=xmlDoc.getElementsByTagName("session"); for (i=0;i<x.length;i++) { img = "<img src='"; path = (x[0].getElementsByTagName("path")[0].childNodes[0].nodeValue); file = (x[i].getElementsByTagName("file")[i].childNodes[0].nodeValue); end = "' /><br />"; name = (x[i].getElementsByTagName("name")[i].childNodes[0].nodeValue); txt = "x:" + x.length + "| i " + i + "<br />" + img + path + file + end + name + "<br />"; document.getElementById("content").innerHTML = txt; //document.write(txt); } } </script> </head> <body onload="displayResult()"> <div id='content'></div> </body> </html>

xml-file:

<gallery> <session> <path>../gallery/beauty/</path> <item> <file>_DSC2331.jpg</file> <name>Picture 1</name> </item> <item> <file>_DSC2339.jpg</file> <name>Picture 2</name> </item> <item> <file>_DSC2350.jpg</file> <name>Picture 3</name> </item> <date>2011-08-03</date> </session> </gallery>

推荐答案

如果我可以提出一些建议:

If I can make some suggestions:

  • 在函数中使用 var 关键字使这些变量成为该函数的本地变量。您目前使用的代码将在全局命名空间中设置值,这通常被认为是不好的做法(例如,您可能会覆盖其他人的变量,或者其他人可能会覆盖您的变量)。还要在函数的开头声明变量,因为它们将是 hoisted 无论如何。
  • 将代码拆分为更有意义的功能。通过这种方式,它们变得更容易阅读,然后变得更加可重复使用。
  • 确保循环项目以及会话。
  • 考虑使用像 jQuery 。它们通常可以简化您必须编写的代码,并且您通常最终会自己编写更少的代码。
  • Use the var keyword within functions to make those variables local to that function. The code you have at the moment would set values in the global namespace, which is often considered bad practice (e.g. you could overwrite other people's variables, or other people could overwrite yours). Also declare your variables at the start of a function, as they will be hoisted there anyway.
  • Split your code up into more meaningful functions. This way they become easier to read and often then become more reusable.
  • Make sure you loop through items as well as sessions.
  • Consider using a Javascript framework like jQuery. They can often simplify the code you have to write, and you will usually end up writing less code yourself.
  • <html> <head> <script type="text/javascript"> function loadDoc(url) { var xmlhttp = null; 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", url, false); xmlhttp.send(); return xmlhttp.responseXML; } function getContent(sessions) { var items = null, i = 0, j = 0, img = "", path = "", file = "", end = "", name = "", txt = ""; for (i = 0; i < sessions.length; i++) { items = sessions[i].getElementsByTagName("item"); path = sessions[i].getElementsByTagName("path")[0].childNodes[0].nodeValue; for (j = 0; j < items.length; j++) { img = "<img src='"; file = items[j].getElementsByTagName("file")[0].childNodes[0].nodeValue; end = "' /><br />"; name = items[j].getElementsByTagName("name")[0].childNodes[0].nodeValue; txt += "session[" + i + "] item[" + j + "]<br />" + img + path + file + end + name + "<br />"; } } return txt; } function displayResult() { var xmlDoc = loadDoc("../gallery/gallery.xml"); var sessions = xmlDoc.getElementsByTagName("session"); var txt = getContent(sessions); document.getElementById("content").innerHTML = txt; } </script> </head> <body onload="displayResult()"> <div id='content'></div> </body> </html>

    更多推荐

    javascript xml阅读器

    本文发布于:2023-07-15 23:11:16,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1115327.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:阅读器   javascript   xml

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!