在bash脚本中将数据添加到XML元素

时间:2015-10-22 16:08:16

标签: xml bash jboss scripting

我有以下XML:

<remote host="${jboss.domain.master.address:127.0.0.1}"     port="${jboss.domain.master.port:9999}"  security-realm="ManagementRealm" />

我想在此块中添加username = admin行。什么是解决这个问题的最佳方式。我已经尝试了每一种sed组合,并且现在已经开始......

2 个答案:

答案 0 :(得分:0)

推荐反对,但是:

xml=$( sed 's# /># username="admin"&#' <<< "$xml" )

答案 1 :(得分:0)

不要使用文本处理工具来编辑XML。使用支持XML的工具。例如,在xsh中,您可以

open file.xml ;
$host = {'${jboss.domain.master.address:127.0.0.1}'} ; # ${ is special in xsh.
$port = {'${jboss.domain.master.port:9999}'} ;
$realm = 'ManagementRealm' ;

$n = //remote[@host=$host][@port=$port][@security-realm=$realm] ;
if (count($n) != 1) throw 'Found 0 or too many nodes.' ;
set $n/@username 'admin' ;
save :b ;