如何加载XML文件并使用Haxe解析它

时间:2012-11-25 19:41:10

标签: xml-parsing haxe nme

我正在尝试创建一个桌面应用程序,它将加载itunes播放列表xml文件并使用Haxe和框架haxenme解析它。

我认为我遇到的问题可能是文件名中的空格未正确转义,但我对此并不是100%肯定。

这是我到目前为止的代码。

package com.mattwallace.appname.actions;

    import nme.Assets;
    import haxe.xml.Fast;

    class GetPlayListAction 
    {
        public function new():Void
        {
            super();
        }

        public function execute():Void
        {

            var xml:Xml = Xml.parse(getXMLDesktopString());
            trace(xml);
        }

        private function getXMLDesktopString():String
        {

            var xmlString:String = sys.io.File.getContent(
                nme.filesystem.File.userDirectory.url + 
                "/Music/iTunes/iTunes Music Library.xml");
            return xmlString;
        }
    }

2 个答案:

答案 0 :(得分:6)

好像我发帖后似乎总能找到答案。问题是“File.userDirectory.url”我需要使用“File.userDirectory.nativePath”

以下是为我解决问题的更新代码。

class GetPlayListAction
{
    public function new():Void
    {
    }

    public function execute():Void
    {

        var xml:Xml = Xml.parse(getXMLDesktopString());
        trace(xml);
    }

    private function getXMLDesktopString():String
    {
        var nativeUserDir:String = nme.filesystem.File.userDirectory.nativePath;
        var filePath:String = "/Music/iTunes/iTunes Music Library.xml";
        var fullPath = nativeUserDir + filePath;

        var xmlString:String = sys.io.File.getContent(fullPath);
        return xmlString;
    }
}

答案 1 :(得分:0)

import haxe.Http;
import haxe.xml.Fast;

class XmlParsRead {

    function new() {

        //asynchronous call 
        var resource_url = new haxe.Http("http://localhost:96/SCO1.2/imsmanifest.xml");

        // onData event method will catch the response data if request is success 
         resource_url.onData = function(resource_url) { 

              var xml = Xml.parse(resource_url);
              // to see the xml file
              //trace(xml);
              // enter into xml file 1st element 
              var xmlReadFirstElement = new Fast(xml.firstElement());
              // see the element name 
              trace(xmlReadFirstElement.name);
              // I want to go resources tag "'node' is predefine attribute it reprasent node(tag<some>) in xml file "
              var resesNode = xmlReadFirstElement.node.resources;
              // name is predefined attribute to see the tag name
              trace(resesNode.name);
              // I want to go resource(it is sub of resources tag )
              var res = resesNode.node.resource;
              // to see the resource tag name
              trace(res.name);
              // I've to read href attribute in <resource>tag i.e -<resource identifier="SCO_RES" adlcp:scormtype="sco" href="some.htm" type="webcontent"> ..</resource>
              var res_href = res.att.href;
              trace(res_href);
              // next read the list of sub tags( <file>) resource tag using loop
              // RES(res) is point the resource tag and NODES(nodes) is predefine attributes we can use to iterate list of sub tags or sub nodes 
              for (subtaghref in res.nodes.file) {
                  //<file href="some.htm"/>
                  trace(subtaghref.att.href);
              }
         }
        // making the asynchronous request 
         resource_url.request(false);
    }

    public static function main() {

        new XmlParsRead();
    }
}