【问题标题】:Applescript - Parsing XMLApplescript - 解析 XML
【发布时间】:2014-02-12 09:11:19
【问题描述】:

我尝试创建 Applescript 以从 .xfdf

中提取价值

XFDF

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
  <annots>
    <square color="#FF0000" creationdate="D:20130828114843+05'30'" date="D:20130828114901+05'30'" flags="print" name="Xi6cOkAWgWHcAhpfBkR5A7" page="0" rect="347.7599999991828,1041.8400000004283,453.5999999989341,1056.9600000003927" subject="Rectangle" title="1 im, awltest7 (AWLTEST7.IM)">
      <contents-richtext>
        <body>
          <p>Text Not Clear</p>
        </body>
      </contents-richtext>
      <popup open="yes" page="0" rect="453.5999999989341,944.4600000003926,573.5999999989341,1056.9600000003927" />
    </square>
    <square color="#FF0000" creationdate="D:20130828114910+05'30'" date="D:20130828114919+05'30'" flags="print" name="ptmmBKtfoDEbVzirMgZLnY" page="0" rect="511.1999999987987,1092.960000000308,550.7999999987057,1123.9200000002352" subject="Rectangle" title="2 im, awltest7 (AWLTEST7.IM)">
      <contents-richtext>
        <body>
          <p>Incorrect dimension</p>
        </body>
      </contents-richtext>
      <popup open="yes" page="0" rect="550.7999999987057,1011.4200000002352,670.7999999987056,1123.9200000002352" />
    </square>
    <square color="#FF0000" creationdate="D:20130828114956+05'30'" date="D:20130828115004+05'30'" flags="print" name="8LaAl2Upx4LEaQptQKXoZx" page="0" rect="355.67999999916424,731.5200000011573,431.99999999898483,750.2400000011135" subject="Rectangle" title="3 im, awltest7 (AWLTEST7.IM)">
      <contents-richtext>
        <body>
          <p>Incorrect Text</p>
        </body>
      </contents-richtext>
      <popup open="yes" page="0" rect="431.99999999898483,637.7400000011133,551.9999999989849,750.2400000011135" />
    </square>
  </annots>
</xfdf>

Applescript

set theXMLFile to ((choose file) as string)

tell application "System Events"
    tell XML element "xfdf" of contents of XML file theXMLFile
        set typeText to (value of XML element "p")
        set nameText to (value of XML element "p")
    end tell
end tell

当我运行脚本时出现以下错误:系统事件出现错误:无法获取 XML 文件内容的 XML 元素“xfdf”的 XML 元素“p”。有什么地方可以从 XML 中提取这些值。提取出 "popup"、"p" 的值。
编辑

set theXMLFile to ((choose file) as string)
tell application "System Events"
    set theXMLFile to XML file theXMLFile
    set loops to XML elements of XML element "annots" of XML element "xfdf" of theXMLFile whose name is "square"
    set coor to {}
    repeat with i from 1 to (count loops)
        --set end of p to value of XML element "square" of XML element "body" of XML element "contents-richtext" of item i of squares
        set end of coor to value of XML attributes of XML element "square" of item i of loops
    end repeat

end tell
squares

【问题讨论】:

    标签: xml xml-parsing applescript xfdf


    【解决方案1】:

    您直接向xfdf 元素询问p,但xfdf 不是它的父元素。您需要深入了解层次结构才能到达它。

    set theXMLFile to ((choose file) as string)
    tell application "System Events"
        set theXMLFile to XML file theXMLFile
        set squares to XML elements of XML element "annots" of XML element "xfdf" of theXMLFile whose name is "square"
        set p to {}
        set attrs to {}
        repeat with i from 1 to (count squares)
            set end of p to value of XML element "p" of XML element "body" of XML element "contents-richtext" of item i of squares
            set end of attrs to value of XML attributes of XML element "popup" of item i of squares
        end repeat
    end tell
    p --> {"Text Not Clear", "Incorrect dimension", "Incorrect Text"}
    attrs --> {{"yes", "0", "453.5999999989341,944.4600000003926,573.5999999989341,1056.9600000003927"}, {"yes", "0", "550.7999999987057,1011.4200000002352,670.7999999987056,1123.9200000002352"}, {"yes", "0", "431.99999999898483,637.7400000011133,551.9999999989849,750.2400000011135"}}
    

    另一种选择是使用来自 Late Night Software 的 XML Tools Scripting Addition。

    【讨论】:

    • 我收到 错误“无法将 \"xfdf\" 转换为整数类型。”数字 -1700 从 "xfdf" 到整数
    • 确保 theXMLFile 是一个 XML 文件元素。我已经更新了我的答案中的代码。
    • 它只提取

      。我如何从 ****&lt;popup open="yes" page="0" rect="431.99999999898483,637.7400000011133,551.9999999989849,750.2400000011135"/&gt;

      之类的值中获取
    • Using the System Events dictionary 表明你需要访问popup元素的XML attributes。代码已被编辑以反映这一点。
    • 最后一个问题。我已经编辑了我的问题以获得 square 值,但它抛出了,Can’t get XML element \"square\" of XML element 1 of XML element 1 of XML element 1 of contents of XML file。请帮助。
    【解决方案2】:

    我写了一个递归辅助函数可以解决这个问题。

    它在 xml 中搜索特定名称并返回一个 XML 元素列表,然后您可以迭代以获取您的值。

    这样做的好处是您无需预先知道元素的绝对路径。

    示例:

    set theXMLFile to ((choose file) as string)
    
    tell application "System Events"
        set xmlData to XML file theXMLFile
    
        set found_elements to my getXMLElementsByName("p", contents of xmlData)
    
        log ("Found " & (count of found_elements) & " <p> nodes")
    
        repeat with i from 1 to count of found_elements
            log (tab & quote & (the value of (item i of found_elements) as text) & quote)
        end repeat
    
        set found_elements to my getXMLElementsByName("popup", contents of xmlData)
        log ("Found " & (count of found_elements) & " <popup> nodes")       
    end tell
    
    on getXMLElementsByName(search_name, search_xml_element)
    
        set found to {}
    
        using terms from application "System Events"            
            tell search_xml_element
                set c to the count of XML elements
                repeat with i from 1 to c
                    if (the name of XML element i is search_name) then
                        set found to found & {XML element i}
                    end if
    
                    if (the (count of XML elements of XML element i) > 0) then
                        set children_found to my getXMLElementsByName(search_name, XML element i)
                        if (the (count of children_found) > 0) then
                            set found to found & children_found
                        end if
                    end if
    
                end repeat
            end tell
        end using terms from
    
        return found
    end getXMLElementsByName
    

    【讨论】:

    • 我没有得到任何输出
    • @chinna_82 确保您已在 AppleScript 编辑器中打开事件日志(未选中结果切换),或者将“日志”调用更改为“显示对话框”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多