且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 xmlstarlet 在另一个元素下插入新元素?

更新时间:2023-11-07 12:34:04

使用-s(或--subnode)代替-i.关于奖励,您不能直接插入带有属性的元素,但是由于每个编辑操作都是按顺序执行的,因此插入一个元素然后添加一个属性:

Use -s (or --subnode) instead of -i. Regarding the bonus, you can't insert an element with an attribute directly but since every edit operation is performed in sequence, to insert an element and then add an attribute:

> xml ed -s /config -t elem -n sub -v "" -i /config/sub -t attr -n class -v com.foo test.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<sub class="com.foo"></sub></config>