如何使用xmlstartlet选择和编辑xml节点? [英] How do I select and edit an xml node with xmlstartlet?

查看:229
本文介绍了如何使用xmlstartlet选择和编辑xml节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我选择节点:

$ xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value conf/nutch-default.xml 
<value/>

这不会对其进行

$ xmlstarlet edit  "/configuration/property[name='http.agent.name']"/value -v 'test' conf/nutch-default.xml 
I/O warning : failed to load external entity "/configuration/property[name='http.agent.name']/value"

将更改更改的xmlstartlet命令是什么? xmlstartlet尚不支持AFAIK -x.

What would be an xmlstartlet command that does change the change? AFAIK -x is not supported in xmlstartlet yet.

我正在研究

推荐答案

您可以将nutch-default.xml的全部内容读取为一个变量,使用xmlstarlet编辑该变量的内容,然后将结果写回到nutch-再次使用default.xml.

You may read the entire contents of nutch-default.xml to a variable, edit the contents of that variable with xmlstarlet and then write the result back to nutch-default.xml again.

另一种方式是使用打开文件句柄,如重定向中所述sed的s/c/d/'myFile输出到myFile .

Another way would be to use open file handles as described in Redirect output from sed 's/c/d/' myFile to myFile .

xmlstarlet --version  # 1.0.6
xmlstarlet ed --help | less -Ip 'inplace'

# 1.
# in-place version using xmlstarlet only
curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain'
xmlstarlet edit -L -u "/configuration/property[name='http.agent.name']"/value -v 'test' nutch-default.xml
xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml 


# 2.
# variable version
curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain'
xmlstr="$(< nutch-default.xml)"   # save file contents to variable

printf '%s\n' "$xmlstr" |
xmlstarlet edit -u "/configuration/property[name='http.agent.name']"/value -v 'test' > nutch-default.xml

xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml 


# 3.
# file handle version
# cf. https://stackoverflow.com/questions/2585438/redirect-output-from-sed-s-c-d-myfile-to-myfile
curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain'
exec 3<nutch-default.xml
rm nutch-default.xml   # prevent open file from being truncated
xmlstarlet edit -u "/configuration/property[name='http.agent.name']"/value -v 'test' <&3 >nutch-default.xml
xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml 

这篇关于如何使用xmlstartlet选择和编辑xml节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆