Hello and thanks in advance.I have been stuck for a few days now on an issue of loading XML data and parsing into an assoc array. I finally decided to just embed the XML configuration data right into my header of the startup HTML5 document.Below I have included the entire file. When load the data via parseFromString() i get some nodes but i get parse errors from other along with just some not even showing up. I have messed at this for days, i cannot see why i am getting parse errors.\[code\]<!-- 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="http://stackoverflow.com/questions/14446433/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="http://stackoverflow.com/questions/playcraftengine/playcraftjs/lib/playcraft.js"></script> <!-- Engine creates game elements and restores upon friendly exit. --> <div id="pcGameDiv"> <div> <!-- Development --> <a class="caption" href="javascriptc.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="javascriptc.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>\[/code\][code being used to obtain and parse]
\[code\] loadResources:function() { function xml_to_array(xml,tag) { var data = http://stackoverflow.com/questions/14446433/xml.getElementsByTagName(tag); var res = {}; if (data) { for (i=0;i<data.length;++i) { var root = data.nodeName; res[root] = new Array(); for (n=0;n<data.childNodes.length;++n) { if (data.childNodes[n].nodeName !="#text") { var node = data.childNodes[n].nodeName; var attr = data.attributes; for (a=0;a<attr.length;++a) { var field = attr[a].nodeName; var value = http://stackoverflow.com/questions/14446433/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()});\[/code\]I'd appreciate any help you guys can give. I want the XML defined in that way...its quicker and simpler for the developer to basically have each resource defined on a single line and use attributes instead of sections with multiple keys. There should be no problem with doing it this way provided each one as a NAME attribute. other attributes are ignored by the system and are for specific use!
\[code\] loadResources:function() { function xml_to_array(xml,tag) { var data = http://stackoverflow.com/questions/14446433/xml.getElementsByTagName(tag); var res = {}; if (data) { for (i=0;i<data.length;++i) { var root = data.nodeName; res[root] = new Array(); for (n=0;n<data.childNodes.length;++n) { if (data.childNodes[n].nodeName !="#text") { var node = data.childNodes[n].nodeName; var attr = data.attributes; for (a=0;a<attr.length;++a) { var field = attr[a].nodeName; var value = http://stackoverflow.com/questions/14446433/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()});\[/code\]I'd appreciate any help you guys can give. I want the XML defined in that way...its quicker and simpler for the developer to basically have each resource defined on a single line and use attributes instead of sections with multiple keys. There should be no problem with doing it this way provided each one as a NAME attribute. other attributes are ignored by the system and are for specific use!