Insert custom XML tag into a XML file in a bash script

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
So I'm trying to create a script using bash to arbitrarily run through an JBoss XML config file and when I see a specific tag I 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 si to find the tags i the server group tag ad then check if the tag is there if it isnt then add it in, if it is there then add the 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>
bash scripting xml
add a comment |
up vote
1
down vote
favorite
So I'm trying to create a script using bash to arbitrarily run through an JBoss XML config file and when I see a specific tag I 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 si to find the tags i the server group tag ad then check if the tag is there if it isnt then add it in, if it is there then add the 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>
bash scripting xml
1
Look into using an xml parser. It will make life easier for you.
– Raman Sailopal
Aug 18 '17 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
Feb 20 at 17:36
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
So I'm trying to create a script using bash to arbitrarily run through an JBoss XML config file and when I see a specific tag I 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 si to find the tags i the server group tag ad then check if the tag is there if it isnt then add it in, if it is there then add the 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>
bash scripting xml
So I'm trying to create a script using bash to arbitrarily run through an JBoss XML config file and when I see a specific tag I 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 si to find the tags i the server group tag ad then check if the tag is there if it isnt then add it in, if it is there then add the 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>
bash scripting xml
bash scripting xml
edited Nov 26 at 1:01
Rui F Ribeiro
38.3k1477127
38.3k1477127
asked Aug 18 '17 at 13:32
Hao-lin.Liang
62
62
1
Look into using an xml parser. It will make life easier for you.
– Raman Sailopal
Aug 18 '17 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
Feb 20 at 17:36
add a comment |
1
Look into using an xml parser. It will make life easier for you.
– Raman Sailopal
Aug 18 '17 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
Feb 20 at 17:36
1
1
Look into using an xml parser. It will make life easier for you.
– Raman Sailopal
Aug 18 '17 at 13:46
Look into using an xml parser. It will make life easier for you.
– Raman Sailopal
Aug 18 '17 at 13:46
1
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
Feb 20 at 17:36
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
Feb 20 at 17:36
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
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
- Delete the element.
- 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 all server_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.
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?
– Hao-lin.Liang
Aug 21 '17 at 8:55
@Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
– Kusalananda
Aug 21 '17 at 9:00
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
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
- Delete the element.
- 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 all server_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.
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?
– Hao-lin.Liang
Aug 21 '17 at 8:55
@Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
– Kusalananda
Aug 21 '17 at 9:00
add a comment |
up vote
3
down vote
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
- Delete the element.
- 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 all server_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.
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?
– Hao-lin.Liang
Aug 21 '17 at 8:55
@Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
– Kusalananda
Aug 21 '17 at 9:00
add a comment |
up vote
3
down vote
up vote
3
down vote
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
- Delete the element.
- 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 all server_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.
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
- Delete the element.
- 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 all server_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.
edited Aug 18 '17 at 15:14
answered Aug 18 '17 at 14:07
Kusalananda
118k16223362
118k16223362
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?
– Hao-lin.Liang
Aug 21 '17 at 8:55
@Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
– Kusalananda
Aug 21 '17 at 9:00
add a comment |
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?
– Hao-lin.Liang
Aug 21 '17 at 8:55
@Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
– Kusalananda
Aug 21 '17 at 9:00
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?
– Hao-lin.Liang
Aug 21 '17 at 8:55
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?
– Hao-lin.Liang
Aug 21 '17 at 8:55
@Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
– Kusalananda
Aug 21 '17 at 9:00
@Hao-lin.Liang XMLStarlet is a Python program for use on Unix systems, yes.
– Kusalananda
Aug 21 '17 at 9:00
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f386965%2finsert-custom-xml-tag-into-a-xml-file-in-a-bash-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Look into using an xml parser. It will make life easier for you.
– Raman Sailopal
Aug 18 '17 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
Feb 20 at 17:36