gpt4 book ai didi

python - 使用 Python 或 shell 脚本更新 XML 文档

转载 作者:行者123 更新时间:2023-11-30 23:23:33 24 4
gpt4 key购买 nike

我有一个 XML 文件,用作具有以下结构的动态面板的索引:

<?xml version="1.0"?>
<Addons>
<line>
<text>My collection 1</text>
<level>3</level>
<comment/>
<file>Collection1.zip</file>
<size>120</size>
<parent>Collection</parent>
<directory>picture</directory>
<type>jpeg</type>
<filedate>22/05/2014</filedate>
</line>
<line>
<text>My collection 2</text>
<level>3</level>
<comment/>
<file>Collection2.zip</file>
<size>880</size>
<parent>Collection</parent>
<directory>picture</directory>
<type>jpeg</type>
<filedate>01/04/2013</filedate>
</line>
</Addons>

我的面板使用此文件作为构建索引。当我更新服务器上的文件时,我需要更新filedate手动元素,...但是该文件有 80 行,这很糟糕。

有没有办法通过脚本来完成这项工作?顺序是:

  • 逐行解析文件
  • <file> 读取文件名元素
  • 获取文件的最后修改日期
  • 更新<filedate>元素(如果存在)
  • 转到下一行

服务器上可以使用 Shell 脚本和 Python。

谢谢!!

最佳答案

使用xmlstarlet命令行工具:

xmlstarlet sel -t -v '//file' -n file.xml |
while IFS= read -r filename; do
filedate=$(date -d "@$(stat -c %Y "$filename")" +%d/%m/%Y)
xmlstarlet ed --inplace -u "//filedate[../file = '$filename']" -v "$filedate" file.xml
done

关于python - 使用 Python 或 shell 脚本更新 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23921485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com