3

我正在尝试创建一个桌面应用程序,它将加载 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;
        }
    }
4

2 回答 2

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;
    }
}
于 2012-11-25T20:03:24.973 回答
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();
    }
}
于 2015-04-17T13:24:34.160 回答