2014-11-06 31 views
1

有沒有人推論過從文件/字符串中成功加載XML的語法,並允許訪問OS X Yosemite(10.10)Javascript for Automation中的數據?OS X Yosemite JavaScript for Automation的XML解析語法?

文檔和代碼示例仍然相當薄的(如2014年11月的),和我的感應技術運行在三個不同的方法乾燥,此刻讀取XML(OPML)文件:

  1. 最有前途的:$.NSXMLDocument

獲得以各種方式字符串數據的保持順利,

function readTextFromFile(strPath) { 
    return $.NSString.stringWithContentsOfFile(strPath); 
} 
function readDataFromFile(strPath) { 
    return $.NSData.alloc.initWithContentsOfFile(strPath); 
} 
function filePath(strPath) { return Path(strPath); } 

但是,沒有PE有關此主題rmutations已見成效:

var strPath='/Users/houthakker/Desktop/notes-2014-11-04.opml', 
    dataXML = readData(strPath), 
    strXML = readTextFile(strPath), 
    oXMLDoc1, oXMLDoc2; 

oXMLDoc1 = $.NSXMLDocument.alloc.initWithXMLString(strXML,0,null); 
oXMLDoc2 = $.NSXMLDocument.alloc.initWithData(dataXML,0,null); 

(即「函數未定義」的錯誤消息表明,這兩個初始化函數可以不暴露,雖然initWithRootElement()似乎是)

  • 最進步:$.NSXMLParser

    var oParser = $.NSXMLParser.alloc.initWithData(dataXML);

    return oParser.parse; //true

  • 但事件驅動的解析似乎需要一些進一步的複雜性,這些複雜性對我來說仍然不透明,並且可能不符合我的簡單需求(讀取和轉換適度大小的本地OPML文件)。

  • 最熟悉的:Application("System Events")
  • 在AppleScript的這可以通過系統事件代碼來完成:

    set fOPML to (POSIX file "/Users/houthakker/Desktop/notes-2014-11-04.opml" as alias) as string 
    tell application "System Events" 
        tell XML file fOPML 
        -- now access XML Elements by name or index 
    end tell 
    

    ,但我還沒有找到一個成功的javascript用於使用unix Path(),字符串或冒號分隔的mac路徑字符串的任何排列初始化XMLFile對象的習慣用法。

    在這裏有什麼想法或更成功的經驗?

    回答

    2

    這原來的(很慢執行)Application("System Events")路線工作:

    var app = Application("System Events"), 
        strPath = '~/Desktop/summarise.opml'; 
    
    var oFile = app.xmlFiles[strPath], 
        oRoot = oFile.xmlElements[0], 
        oHead = oRoot.xmlElements.head, 
        oBody = oRoot.xmlElements.body, 
        lstOutlineXML = oBody.xmlElements.whose({ 
         name: 'outline' 
        }); 
    

    以及從一個XML字符串初始化一個NSXMLDocument功能,根據JSExport約定(其中字母以下每個「:」是大寫,然後把「:」 S被刪除).initWithXMLStringOptionsError()

    因此,選擇一個本地OPML文件,並將其解析到一個簡單的JSON概要:

    function run() { 
        var app = Application.currentApplication(); 
        app.includeStandardAdditions = true; 
    
        function readTextFromFile(strPath) { 
         return $.NSString.stringWithContentsOfFile(strPath); 
        } 
    
        var strPath = (
          app.chooseFile({withPrompt: "Choose OPML file:"}) 
         ).toString(), // Path → String 
         strXML = strPath ? readTextFromFile(strPath) : ''; 
    
        if (!strXML) return; 
    
        var oXMLDoc1 = $.NSXMLDocument.alloc.initWithXMLStringOptionsError(strXML, 0, null), 
         oRoot = oXMLDoc1.rootElement, 
         oBody = ObjC.unwrap(oRoot.children)[1], 
         lstOutline = ObjC.unwrap(oBody.children), 
         lstParse, strJSON; 
    
        function parseOPML(lst) { 
         var lstParse=[], oNode, dctNode, lstChiln; 
    
         for (var i = 0, lng=lst.length; i<lng; i++) { 
          oNode = lst[i]; 
          dctNode = {}; 
          dctNode.txt = ObjC.unwrap(oNode.attributeForName('text').stringValue); 
          lstChiln = ObjC.unwrap(oNode.children); 
          if (lstChiln && lstChiln.length) 
           dctNode.chiln = parseOPML(lstChiln); 
          lstParse.push(dctNode); 
         } 
         return lstParse; 
        } 
    
        lstParse = parseOPML(lstOutline); 
        strJSON = JSON.stringify(lstParse,null, 2); 
        app.setTheClipboardTo(strJSON); 
        return strJSON; 
    }