xml - 如何使用 sed 在 xml 文件中添加值
问题描述
我这里有 xml 文件,如下所示
<?xml version="1.0" encoding="utf-8"?><Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/mytype" xmlns="http://we.xyti.com/2011/01/gone"> <Parameters>
<Parameter Name="mytype" Value="-1" />
<Parameter Name="new1" Value="" />
<Parameter Name="new2" Value="" />
<Parameter Name="new3" Value="" />
<Parameter Name="new4" Value="" /> </Parameters></Application></Application>`
在上面的 xml 中,我需要在每一行中添加值,例如:- 而不是 "" 必须将值作为测试,每个Name
属性都不同。例如
<?xml version="1.0" encoding="utf-8"?><Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/mytype" xmlns="http://we.xyti.com/2011/01/gone"> <Parameters>
<Parameter Name="mytype" Value="-1" />
<Parameter Name="new1" Value="test1" />
<Parameter Name="new2" Value="test2" />
<Parameter Name="new3" Value="test3" />
<Parameter Name="new4" Value="test4" /> </Parameters></Application></Application>
解决方案
有了这个 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://we.xyti.com/2011/01/gone" Name="fabric:/mytype">
<Parameters>
<Parameter Name="mytype" Value="-1"/>
<Parameter Name="new1" Value=""/>
<Parameter Name="new2" Value=""/>
<Parameter Name="new3" Value=""/>
<Parameter Name="new4" Value=""/>
</Parameters>
</Application>
和 xmlstarlet:
xmlstarlet edit -N x="http://we.xyti.com/2011/01/gone" --update '//x:Parameter/@Value' --value "test" file.xml
输出:
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://we.xyti.com/2011/01/gone" Name="fabric:/mytype">
<Parameters>
<Parameter Name="mytype" Value="test"/>
<Parameter Name="new1" Value="test"/>
<Parameter Name="new2" Value="test"/>
<Parameter Name="new3" Value="test"/>
<Parameter Name="new4" Value="test"/>
</Parameters>
</Application>
推荐阅读
- javascript - 如何在“npm run build”后修复空白网页
- reactjs - 如何仅发送图像消息?(也没有发送文本)
- html - 闪亮的 html5 视频输入
- tableau-api - 工具提示更改绘图并在工具提示中显示绘图,但我只想更改绘图
- c# - Webbrowser 控件不会在不触发任何事件的情况下在屏幕上加载元素
- python - 使用 3d 数组的索引来填充 4d 数组
- python - 计算打开和关闭时间内的时间差异
- python - 上传文件和字符串时,Ajax FormData()返回空文件
- c - 如何将日期格式:2019-08-22 16:16:08] 转换为 C 语言中的格式:aammjjhhmmss
- pentaho-cde - 如何将从查询组件获得的变量传递给 Pentaho CDE 上的查询?