applescript - AppleScript,在解析大型 XML 文件时收到“系统事件出错:连接无效。(-609)”
问题描述
我创建了一个 AppleScript 来解析 MS Word XML 文件,以便生成一个制表符分隔的文档,以便在 Excel 中打开。一切都适用于中小型 XML 文件。但是,在解析较大的文件时,我收到错误消息“系统事件出错:连接无效。(-609)”。下面是发生错误的告诉块:
tell application "System Events"
activate
with timeout of 0 seconds
set quit delay to 0
set theXMLFile to XML file theDocument
set theRecords to XML elements of XML element "wx:sect" of XML element "w:body" of XML element "w:wordDocument" of theXMLFile whose name is "w:p"
end timeout
end tell
我尝试调整超时和退出延迟参数,包括将它们注释掉,但即使将它们设置为 0,我仍然会收到错误消息。我测试了脚本以确保它通过了“设置 XMLFile.xml”。 ..” 行,但没有使其通过“set theRecords ...”行。
为了确认 XML 文件没有问题,我手动将文档拆分为两个较小的文件。这些文件中的每一个都通过脚本处理得很好。这使我相信错误是执行 theRecords 变量的大小或由于解析文件所需的时间而导致的某种超时。
非常感谢解决或解决此错误的任何帮助。谢谢你。
解决方案
正如我所检查的,问题是使用who子句的属性不是 XML 元素的直接属性。您可以在脚本调试器中检查这一点。以下适用于大型 XML 文件。至少对我来说它有效:
-- set theDocument to "HARD_DISK:Users:123:Desktop:TEST files:SampleLarge.xml"
set aList to {}
tell application "System Events"
activate
tell (contents of XML file theDocument) to tell (XML element "w:wordDocument")
tell (XML element "wx:sect" of XML element "w:body")
set XMLElements to XML elements
set XMLElementNames to name of XML elements
repeat with i from 1 to count XMLElementNames
if (item i of XMLElementNames) is "w:p" then
set end of my aList to item i of my XMLElements
end if
end repeat
end tell
end tell
end tell
return aList
推荐阅读
- laravel - yajra Datatables 过滤并排序不在数据库中的数据
- java - 如何在 2d Arraylist 上使用 TrimToSize 方法?
- xslt - 使用 xls 删除属性值等于 0 的 xml 节点
- mysql - Mysql LEFT JOIN 结合 INNER 和 Group By
- java - 如何使用 javax.xml.bind 将 XML 写入带有 xmlns 字段的 POJO?
- r - split-lapply- 组合一个大数据帧以避免内存问题?
- android - 如何在我的联系人列表中搜索和拨打电话?
- r - 闪亮的仪表板 tabitem 没有显示
- flask - Flask:无法解析包中的端点位置
- javascript - 如何处理在我的 React 原生项目中查看的阿拉伯语和英语数据?