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

The name of the pictureThe name of the pictureThe name of the pictureClash 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>









share|improve this question



















  • 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














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>









share|improve this question



















  • 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












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>









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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










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



  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 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.






share|improve this answer






















  • 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










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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



  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 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.






share|improve this answer






















  • 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














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



  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 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.






share|improve this answer






















  • 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












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



  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 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.






share|improve this answer














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 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.







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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






Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos