简体   繁体   English

XML JavaScript 中的文档“未定义”

[英]XML Document “undefined” in JavaScript

I'm working on an HTML5 implementation of a game idea I'm playing with.我正在研究我正在玩的游戏创意的 HTML5 实现。 Part of this, naturally, is storing the content, among which is the data for different abilities in the game, as well as characters and such.当然,其中一部分是存储内容,其中包括游戏中不同能力的数据,以及角色等。

While dynamic content, such as specific characters, will be moved later on, I will probably stick to XML for static content, such as stats for abilities and gear, which means I'd prefer fixing the issues I'm having with XML as soon as possible.虽然动态内容(例如特定角色)将在稍后移动,但对于 static 内容(例如能力和装备的统计数据),我可能会坚持使用 XML,这意味着我更愿意尽快解决 Z3501BB7193D36938EDF310B 的问题尽可能。

Now, to do this, I'm loading in XML documents at the beginning of a combat phase, first the character, then the abilities etc relevant to this character.现在,为此,我在战斗阶段开始时加载 XML 文档,首先是角色,然后是与该角色相关的能力等。

To do this, I have a "loadXMLFile" function, that takes a filename:为此,我有一个“loadXMLFile”function,它采用文件名:

var loadXMLFile = function(filename) {
  req = new XMLHttpRequest();
  req.onreadystatechange = function() {
    return req.responseXML;
  }
  req.open("GET", filename, true);
  req.send();
}

An example of loading and manipulating the data:加载和操作数据的示例:

function Character(id) {
  doc = loadXMLFile("characters.xml");
  characters = doc.getElementsByTagName("Character");

My intention with this was to make the function call wait for the return value before continuing.我的意图是让 function 调用在继续之前等待返回值。 As you may be able to tell from the title of the question, this does not work.正如您可以从问题的标题中看出的那样,这不起作用。 Instead, as soon as I attempt to manipulate the data, the third line in the example, I receive the following runtime error: Uncaught TypeError: Cannot call method 'getElementsByTagName' of undefined相反,一旦我尝试操作数据(示例中的第三行),我就会收到以下运行时错误: Uncaught TypeError: Cannot call method 'getElementsByTagName' of undefined

As the example shows, I load these XML files in my constructors, using the XML file to populate the class.如示例所示,我在构造函数中加载这些 XML 文件,使用 XML 文件填充 class。 Hence, a structure of calling a different function inside the readystatechange event handler is less than preferable.因此,在 readystatechange 事件处理程序中调用不同的 function 的结构不太可取。 Am I forced to repeat the XML loading code everywhere I do this, just to make separate event handlers?我是否被迫在任何地方重复 XML 加载代码,只是为了制作单独的事件处理程序?

I would suggest to use jQuery or another javascript framework.我建议使用 jQuery 或其他 javascript 框架。 It's much easier to handle problems like this and you will save a lot of time and code lines.处理这样的问题要容易得多,您将节省大量时间和代码行。 Another problem is the cross-browser compatibility which is not given in your code.另一个问题是代码中没有给出的跨浏览器兼容性。

You want to change:你想改变:

req.open("GET", filename, true);

To:至:

req.open("GET", filename, false);

To make a synchronous request (which means wait for the request before continuing.)发出同步请求(这意味着在继续之前等待请求。)

You can not return something from onreadystatechange, that part of your code is wrong.你不能从 onreadystatechange 返回一些东西,你的那部分代码是错误的。

And you are not checking what the actual state is in that function, so that's wrong too.而且您没有检查 function 中的实际 state 是什么,所以这也是错误的。

So basically your code is not correct at all.所以基本上你的代码根本不正确。 I would suggest finding a tutorial site for XMLHttpRequest and copying their code.我建议找到 XMLHttpRequest 的教程站点并复制他们的代码。

As a side note, why are you making life hard for yourself?作为旁注,你为什么要让自己的生活变得艰难? Install jQuery and let it do all this stuff for you.安装 jQuery 并让它为您完成所有这些工作。

And a final note, json is a LOT easier to work with than xml, you may want to switch to that.最后一点,json 比 xml 更容易使用,您可能想要切换到它。

The piece of code that was mentioned is browser specific issue.提到的那段代码是浏览器特定的问题。 You should ideally be using the cross-browser compatible javascript based frameworks.理想情况下,您应该使用跨浏览器兼容的基于 javascript 的框架。 You can avoid most of the cross-browser related issues.您可以避免大多数与跨浏览器相关的问题。

Coming to resolution of your issue, getElementsByTagName function is not available within the doc element.为了解决您的问题,getElementsByTagName function 在 doc 元素中不可用。 So, you can modify the code in the following manner,因此,您可以通过以下方式修改代码,

if(doc.getElementsByTagName){
     characters = doc.getElementsByTagName("Character");
}

But this will only avoid the error but will not resolve the issue of getting the Character element.但这只会避免错误,但不会解决获取 Character 元素的问题。

The best option would be to convert the XML to JSON in the server and send the JSON to the client.最好的选择是将服务器中的 XML 转换为 JSON 并将 JSON 发送到客户端。 It is easy to traverse through the JSON easily and there are no cross-browser issues.很容易遍历 JSON 并且不存在跨浏览器问题。

One can traverse through the JSON easily as follows,可以通过如下方式轻松遍历 JSON,

function traverseJSON (jsonObj){
    for(var value in jsonObj){
        if(typeof value == 'function'){//this is to avoid 'remove' function in case of Array, Array object will be available in case of repeating elements in XML. 
            continue;
        }else if(typeof value == 'object'){ //refers to element in XML 
            traverseJSON(value);
        }else if(typeof value== 'string'){   //refers to attribute in XML
        jsonObj[value]; //refers to the attribute value in XML
        }
    }
}

The above function can be used to traverse through the JSON, which takes JSON object as the input.上面的function可以用来遍历JSON,它以JSON ZA8CFDE69811BD54B62669为输入。

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

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