你好,并提前致谢。xml没有提供正确的节点
对于加载XML数据和解析assoc数组的问题,我一直坚持了几天。我最终决定只是将XML配置数据嵌入到我的HTML5文档的头文件中。
下面我已经包含了整个文件。当通过parseFromString()加载数据时,我得到了一些节点,但我从其他一些解析错误一起甚至没有出现。我已经搞砸了几天,我看不出为什么我得到解析错误。
<!-- I2TM Game Engine [Developer] -->
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>I2TM: Example Template</title>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script id="resources" type="text/xmldata">
<resources name="Template" version="0.1" publisher="I2TM Software" copyright="2012-2013">
<credits>
<credit id="0" name="Andrew Donelson" url="http://www.i2tmsoftware.com" desc="Author" />
<credit id="1" name="Playcraft Labs" url="http://www.playcraftlabs.com" desc="I2TM Engine forked from PlaycraftJS v0.5.6" />
<credit id="2" name="SoundManager2" url="http://www.schillmania.com/projects/soundmanager2/" desc="Used in I2TM Engine as primary Sound API" />
<credit id="3" name="James Padolsey" url="https://github.com/padolsey/string/blob/master/string.js" desc="Used in I2TM Engine extended String API" />
<credit id="4" name="Alexandru Marasteanu" url="http://www.diveintojavascript.com/projects/javascript-sprintf" desc="Used in I2TM Engine to add sprintf capability" />
</credits>
<languages>
<language name="english" from="english" to="english" />
<language name="spanish" from="english" to="spanish" />
<language name="german" from="english" to="german" />
</languages>
<sounds>
<sound name="i2tm" ogg="true" mp3="true" channels="1" file="sounds/i2tm" />
</sounds>
<images>
<image name="publisher" file="images/publisher.png" />
<image name="touchpad" file="images/touchpad_buttons.png" />
<image name="title" file="images/title.png" />
<image name="starport" file="images/starport.png" />
</images>
</resources>
</script>
<!--[if lt IE 9]>
<script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
<![endif]-->
</head>
<body>
<script type="text/javascript" src="../playcraftengine/playcraftjs/lib/playcraft.js"></script>
<!-- Engine creates game elements and restores upon friendly exit. -->
<div id="pcGameDiv">
<div>
<!-- Development -->
<a class="caption" href="javascript:pc.start('js/', ['game.main.js','touch.main.js','game.resources.js','factory.entity.js','factory.sound.js','scene.publisher.js','scene.mainmenu.js','scene.touchpad.js','scene.game.js','system.touchpad.js'],'../playcraftengine/playcraftjs/lib/');">Play Now</a>
<!-- Production
<a class="caption" href="javascript:pc.start('js/', ['game.min.js']);">Play Now</a>
-->
</div>
<div><strong>Game Title</strong><p>replaces this text with the description of your game here</p></div>
<div><strong>Help</strong><p>Replace this with how to play or other helpful information</p></div>
</div>
</body>
</html>
[被使用的代码获得和解析]
loadResources:function()
{
function xml_to_array(xml,tag)
{
var data = xml.getElementsByTagName(tag);
var res = {};
if (data)
{
for (i=0;i<data.length;++i)
{
var root = data[i].nodeName;
res[root] = new Array();
for (n=0;n<data[i].childNodes.length;++n)
{
if (data[i].childNodes[n].nodeName != "#text")
{
var node = data[i].childNodes[n].nodeName;
var attr = data[i].attributes;
for (a=0;a<attr.length;++a)
{
var field = attr[a].nodeName;
var value = attr[a].nodeValue;
res[root][node][field]=value;
}
}
}
}
}
return res;
} //end xml_to_array()
var xml = this.parseXML(document.getElementById('resources').innerHTML);
//look at the value of xml in your favorite debugger...that is the problem.
//nodes are missing, parse errors, ect.
//following code is work in progress and does not work completely until i get the XML data right.
this.resources = new Array();
this.resources['game'] = xml_to_array(xml,'game');
this.resources['credits'] = xml_to_array(xml,'credits');
this.resources['strings'] = xml_to_array(xml,'string');
this.resources['sounds'] = xml_to_array(xml,'sound');
this.resources['images'] = xml_to_array(xml,'image');
},
/**
* Parses XML and returns an XMLDoc
*/
parseXML:function (xml)
{
if (window.DOMParser)
{
// standard
if (!this.xmlParser)
this.xmlParser = new DOMParser();
try {
return this.xmlParser.parseFromString(xml, "text/xml")
} catch(e) {
this.error('DOH! error loading '+xml+'.');
}
} else // ie
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
return xmlDoc.loadXML(xml)
}
} //end parseXML()
});
我想感谢所有帮助你们可以给。我希望以这种方式定义XML ......它对于开发人员来说更简单快捷,基本上每个资源都定义在一行中,并使用属性而不是具有多个键的部分。这样做应该没有问题,只要将每一个都作为NAME属性提供即可。其他属性会被系统忽略,并且是特定用途的!
'playcraft.js'是404 ,无论如何,问题应该包含一个独立的例子,而不是链接到外部代码。 (在问题中包含XML) –
我这样做是因为我无法让HTML文档显示为代码而不是HTML。我试过,代码前和代码。至于playcraft它不需要。 diplayed代码是所有使用的。只需将name:function()更改为函数name()。我更加担心为什么我的节点一旦读入DOM就出错了。 – NlaakALD
向左边缩进四个空格,或者只使用'{}'按钮。如果点击''''按钮,工具栏有很多帮助。 OMG! –