2014-11-06 29 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; 
    }