3

I'm trying to create a script using bash to arbitrarily run through a JBoss XML config file and, when I see a specific tag, put some custom values into it.

The XML below is the important snippet of the XML config file from a JBoss example.  What I need to do is to find the <jvm> tags in the server group tag and then check if the <jvm-options> tag is there.  If it isn’t, then add it in; if it is there, then add the <options> tag with the values in it.

<server-groups> <server-group name="main-server-group" profile="full"> <jvm name="default"> <heap size="64m" max-size="512m"/> <jvm-options> <option value="-agentpath:"<DT_HOME>/agent/lib/libdtagent.so"=name=<AgentName>,server=<dynaTraceCollectorName>"/> </jvm-options> </jvm> <socket-binding-group ref="full-sockets"/> </server-group> </server-groups> </domain> 
2
  • 1
    Look into using an xml parser. It will make life easier for you.CommentedAug 18, 2017 at 13:46
  • 1
    Note that with modern versions of Wildfly (formerly JBoss AS) and JBoss EAP you can edit the configuration of your standalone or domain with a command-line tool that validates modifications and therefore is recommended over manually editing the XML configuration. The tool has its own syntax that is quickly learned, a websearch for "jboss cli" will teach you everything you need to know
    – Aaron
    CommentedFeb 20, 2018 at 17:36

1 Answer 1

8

With XMLStarlet, you can add a subnode to an element, or you can update an existing element. To cover both the case when the element exists and when it doesn't already exist, we can

  1. Delete the element.
  2. Add the element with the value that we want it to have.

Let's say the element that we want to add is <newtag> and that its value should be tagval. It should also have an attribute, newattr, with value attrval. This means that what we want to add is

<newtag newattr="attrval">tagval</newtag> 

To delete the element:

$ xmlstarlet ed -d '//server-group/newtag' file.xml 

To add the element with an attribute:

$ xmlstarlet ed -s '//server-group' -t elem -n 'newtag' -v 'tagval' \ -s '//server-group/newtag' -t attr -n 'newattr' -v 'attrval' file.xml 

Adding these together:

$ xmlstarlet ed -d '//server-group/newtag' \ -s '//server-group' -t elem -n 'newtag' -v 'tagval' \ -s '//server-group/newtag' -t attr -n 'newattr' -v 'attrval' file.xml 

Given the input XML

<?xml version="1.0"?> <server-groups> <server-group name="main-server-group" profile="full"> <jvm name="default"> <heap size="64m" max-size="512m"/> <jvm-options> <option value="somevalue"/> </jvm-options> </jvm> <socket-binding-group ref="full-sockets"/> </server-group> </server-groups> 

this results in the output

<?xml version="1.0"?> <server-groups> <server-group name="main-server-group" profile="full"> <jvm name="default"> <heap size="64m" max-size="512m"/> <jvm-options> <option value="somevalue"/> </jvm-options> </jvm> <socket-binding-group ref="full-sockets"/> <newtag newattr="attrval">tagval</newtag> </server-group> </server-groups> 

Note that the XPath expression //server-group will match allserver_group elements in the input XML. If you only want to match the one whose name attribute is main-server-group, you would have to change //server-group to //server-group[@name="main-server-group"].


XMLStarlet is available from http://xmlstar.sourceforge.net/, but there's most likely a pre-packaged version available for your Unix already (use that). Sometimes, the XMLStarlet executable is called just xml rather than xmlstarlet.

2
  • Thanks :), will script and test it once I have my hands on a linux box XD, thats a great help. So can i pressume that XML starlet .*nix systems?CommentedAug 21, 2017 at 8:55
  • @Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
    – Kusalananda
    CommentedAug 21, 2017 at 9:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.