如何使用xmlstarlet更新xml文件 [英] how to update the xml file using xmlstarlet
问题描述
我正在使用Windows版本的xmlstarlet通过Windows批处理文件来更新xml文件.
I am using windows version of xmlstarlet to update an xml file, via windows batch file.
xml edit --update "/xml/table/rec[@id=3]/@id" --value 10 %xmlfile%
我希望这会将rec节点的id属性更新为10.运行此命令时,我在命令行中看到了更新的xml,但是文件从未更新.
I expected this to update the id attribute of rec node to 10. When I run this I see the updated xml as expected in the command line, but the file is never updated.
我该怎么做,我想避免重写整个文件,因为文件可能很大.
How can I do it, I want to stay away rewriting the whole file as the file could be big one.
更新前:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1" />
<rec id="2" />
<rec id="3" />
</table>
</xml>
更新后:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1" />
<rec id="2" />
<rec id="10" />
</table>
</xml>
推荐答案
You did not show your input document, but I assume it is the following, taken from the xmlstarlet documentation:
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
xmlstarlet修改文件,但是结果发送到标准输出,而不保存在原始文件中.使用另一个选项--inplace
修改该文件:
xmlstarlet modifies the file, but the result is sent to standard output, not saved in the original file. Use another option --inplace
to modify the file in place:
$ xml ed --inplace -u "/xml/table/rec[@id='3']/@id" -v 5 rec.xml
然后:
$ cat rec.xml
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="5">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
顺便说一句,这个问题似乎提出了与这个问题非常相似的问题
By the way, this question seems to ask something very similar to this question.
编辑:如@npostavs所建议,此选项在编辑帮助中列出:
EDIT: As suggested by @npostavs, this option is listed in the edit help:
$ xml edit --help
...
-L (or --inplace) - edit file inplace
...
这篇关于如何使用xmlstarlet更新xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!