Create xml file using a content from another xml file

I need to create a xml file(master.xml) with contents from another xml files(children). I have below list of xml files in a temporary location (C:/temp/xmls)

1. child1.xml
2. child2.xml

Below is the content of the child1.xml & child2.xml files,
child1.xml

<root>
    <emp>
          <name>sam</name>
         <rollno>10</rollno>
    </emp>
</root>

child2.xml

<dept>
    <emp-dep>
          <name>sam</name>
         <dname>dept1</dname>
    </emp-dep>
</dept>

Now i need to create the master.xml using the content of the above two child xmls. Below is the expected output of master.xml

<master>
    <root>
           <emp>
                 <name>sam</name>
                 <rollno>10</rollno>
           </emp>
    </root>
    <dept>
          <emp-dep>
                 <name>sam</name>
                <dname>dept1</dname>
        </emp-dep>
     </dept>
</master>

Please help to do this using shell scripting.

( echo "<master>"; cat child1.xml child2.xml ; echo "</master>" ) > master.xml
1 Like

Works perfectly. Thank you for your help!

$ awk -vM="master>" 'NR==1{print "<"M}{print}END{print "</"M}' child1.xml child2.xml