2012-11-25 4 views
3

itunes 재생 목록 XML 파일을로드하고 Haxe 및 프레임 워크 haxenme을 사용하여 구문 분석 할 데스크톱 응용 프로그램을 만들려고합니다.XML 파일을로드하고 Haxe를 사용하여 구문 분석하는 방법

필자는이 문제가 파일 이름의 공백 일 수 있지만 올바르게 이스케이프 처리되지는 않을 것이라고 생각하지만 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; 
     } 
    } 

답변

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; 
    } 
} 
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(); 
    } 
} 
+1

당신의 접근 방식에 대한 약간의 코멘트를 추가하는 것을 고려하십시오 –

+0

gitlab https://github.com/mindset/scorm/blob/master/examples/example/imsmanifest.xml에서 XML 파일을 샘플보기 –

관련 문제