xml没有提供适当的节点

时间:2013-01-21 20:13:18

标签: javascript xml formatting nodes

您好,并提前致谢。

我已经在加载XML数据和解析为关联数组的问题上被困了几天。我最终决定将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属性。系统会忽略其他属性,这些属性仅供特定用途使用!

1 个答案:

答案 0 :(得分:0)

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

desc="Author & Publisher"

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

对于记录,这是我在webkit上从parseFromString得到的(序列化)输出,它直接指向问题(第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>