-1

您好,提前致谢。

几天来,我一直在加载 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 属性,这样做应该没有问题。其他属性被系统忽略,具体使用!

4

1 回答 1

0

您的 XML 无效,错误消息应该指出您正确的问题:

desc="Author & Publisher"

看到未转义的 & 符号了吗?应该是Author &amp; Publisher

作为记录,这是我从parseFromStringwebkit 上获得的(序列化)输出,它直接指向问题(第 4 行第 37 列--xmlParseEntityRef):

<resources name="Template" version="0.1" publisher="I2TM Software" copyright="2012-2013">
  <parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
      <h3>This page contains the following errors:</h3>
      <div style="font-family:monospace;font-size:12px">error on line 4 at column 37: xmlParseEntityRef: no name</div>
      <h3>Below is a rendering of the page up to the first error.</h3>
  </parsererror>
  <credits></credits>
</resources>
于 2013-01-21T20:31:26.153 回答