You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Windows 10脚本提取testxml.txt中节点的text与bounds值

提取XML节点中的text与bounds值并格式化输出

需求说明

从testxml.txt文件的XML内容中,提取所有<node>节点的text="..."和bounds="..."属性,按text="xxx"|bounds="xxx"的格式每行输出一组,最终保存到output.txt。

示例输入

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><hierarchy rotation="0"><node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.facebook.katana" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,2076]"><node index="0" text="Create account" resource-id="com.facebook.katana:id/(name removed)" class="android.widget.TextView" package="com.facebook.katana" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[168,108][533,264]" /></node></hierarchy>UI hierchary dumped to: /dev/tty//

期望输出(output.txt)

text=""|bounds="[0,0][1080,2076]"
text="Create account"|bounds="[168,108][533,264]"
....
text=""|bounds="[][]"

解决方案

方法1:Python脚本

创建extract_text_bounds.py脚本,运行后直接处理文件:

import re

# 读取输入文件内容
with open('testxml.txt', 'r', encoding='utf-8') as f:
    content = f.read()

# 匹配所有node节点的text和bounds属性
pattern = re.compile(r'text="([^"]*)".*?bounds="([^"]*)"', re.DOTALL)
matches = pattern.findall(content)

# 写入输出文件
with open('output.txt', 'w', encoding='utf-8') as f:
    for text, bounds in matches:
        f.write(f'text="{text}"|bounds="{bounds}"\n')

运行命令:

python extract_text_bounds.py

方法2:Shell awk命令

直接用awk处理文件,无需额外脚本:

awk -F'[<>]' '{for(i=1;i<=NF;i++){if($i ~ /^node /){match($i, /text="([^"]*)"/, t); match($i, /bounds="([^"]*)"/, b); print "text=\""t[1]"\"|bounds=\""b[1]"\""}}}' testxml.txt > output.txt

内容的提问来源于stack exchange,提问作者vinh nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 03:06:38