{"id":171,"date":"2011-01-26T14:22:54","date_gmt":"2011-01-26T13:22:54","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=171"},"modified":"2011-01-26T14:31:32","modified_gmt":"2011-01-26T13:31:32","slug":"dynamic-variable-to-parse-in-a-xml","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/dynamic-variable-to-parse-in-a-xml\/","title":{"rendered":"Dynamic variable to parse in a XML"},"content":{"rendered":"<p>A current problem in AS3 with XML : if you want to parse a XML with a dynamic variable as a root, you can&#8217;t with a <strong>for each<\/strong> statement. It doesn&#8217;t recognize the variable.<\/p>\n<p>The XML Format :<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n<boite>\r\n\t\r\n\t<Box name=\"Box1\">\r\n\t\t<lien nom=\"BENCHMARK\">http:\/\/www.google.fr<\/lien>\r\n\t\t<lien nom=\"FOCUS GROUP\">http:\/\/www.google.fr<\/lien>\r\n\t<\/Box>\r\n\t\r\n\t<Box name=\"Box2\">\r\n\t\t<lien nom=\"FORMATION\">http:\/\/www.google.fr<\/lien>\r\n\t\t<lien nom=\"COACHING\">http:\/\/www.google.fr<\/lien>\r\n\t<\/Box>\r\n\r\n<\/boite>\r\n<\/pre>\n<p>The way which doesn&#8217;t work (N.B. in this case the XML is Box1&#8230;\/Box1   Box2&#8230;\/Box2:<\/p>\n<pre lang=\"actionscript3\">\r\nvar myBox:String = \"Box1\";\r\nfor each (var box:XML in myXML.myBox) {\r\n\ttrace(box);\r\n}\r\n<\/pre>\n<p>The good way :<\/p>\n<pre lang=\"actionscript3\">\r\nprivate function _xmlLoaded(mEvt:Event):void {\r\n\t\t\t\r\n\tmEvt.target.removeEventListener(Event.COMPLETE, _xmlLoaded);\r\n\t_xml = new XML(mEvt.target.data);\r\n\r\n        _boiteNom = \"Box1\";\r\n\t\t\t\r\n\t_textes = [];\r\n\t_liens = [];\r\n\t\t\t\r\n\tvar indexBoite:uint = _getIndexBox(_boiteNom);\r\n\t_nbrFiches = _xml.Box[indexBoite].lien.length();\r\n\t\t\t\r\n\tfor (var i:uint; i < _nbrFiches; ++i) {\r\n\t\t_textes.push(_xml.Box[indexBoite].lien[i].@nom);\r\n\t\t_liens.push(_xml.Box[indexBoite].lien[i]);\r\n\t}\r\n\t\t\t\r\n\t_ajoutFiche();\r\n}\r\n\t\t\r\nprivate function _getIndexBox($name:String):uint {\r\n\t\t\t\r\n  \tfor(var i:uint; i < _xml.Box.length(); ++i) {\r\n   \t\tif(String(_xml.Box[i].@name) == $name)\r\n   \t\t\treturn i; \r\n   \t}\r\n   return 666; \/\/ The devil's number, too evil to be real\r\n }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A current problem in AS3 with XML : if you want to parse a XML with a dynamic variable as a root, you can&#8217;t with a for each statement. It doesn&#8217;t recognize the variable. The XML Format : http:\/\/www.google.fr http:\/\/www.google.fr http:\/\/www.google.fr http:\/\/www.google.fr The way which doesn&#8217;t work (N.B. in this case the XML is Box1&#8230;\/Box1 &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/dynamic-variable-to-parse-in-a-xml\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Dynamic variable to parse in a XML<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[8,15,17,16],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/171"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":10,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":181,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions\/181"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}