xml - Applescript - 解析 XML

标签 xml xml-parsing applescript xfdf

我尝试创建 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

最佳答案

您直接向 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"}}

另一种选择是使用 XML Tools Scripting Addition来自深夜软件。

关于xml - Applescript - 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21280759/

相关文章:

android - com.facebook.widget.ProfilePictureView - 错误 : Error parsing XML: unbound prefix

iphone - 如何通过iphone中的xml解析文件将文本字段数据从iphone发送到服务器

xml-parsing - 使用 JAXB 和 Unmarshaller 过早结束文件。响应中的 xml 对我来说看起来有效

Android:如何在 SAX 解析器中解析相同的标签?

shell - applescript 'Can’ t 将当前应用程序转换为字符串类型'

html - 使用本地 HTML 导入 XML

java - 当我在 AndroidManifest.xml 中指定 minSdkVersion 时,标题栏会缩小

php - UTF-8 特殊字符作为奇怪字符插入表中

applescript oneliner

macos - 如何制作 Mac 终端弹出窗口/警报?苹果脚本?