# Splitting the XML file into three different files

**URL:** <https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220>\
**Category:** Shell Programming and Scripting\
**Created:** [March 6, 2018, 12:23am UTC](https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220 "2018-03-06T00:23:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siv51427882](https://community.unix.com/letter_avatar/siv51427882/32/6_449c533f586939b6e5f246632a46421f.png) [@Siv51427882](https://community.unix.com/u/Siv51427882)\
**Post date:** [March 6, 2018, 12:23am UTC](https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220/1 "2018-03-06T00:23:15Z")

</div>

Hello Shell Guru's

I have a requirement to split the source xml file into three different text file.  
And i need your valuable suggestion to finish this.

Here is my source xml snippet, here i am using only one entry of \<jms-system-resource\>. There may be multiple entries in the source file.

```nohighlight
  <jms-system-resource>
    <name>UMSJMSSystemResource</name>
    <target>soa_server1,bam_server1</target>
    <sub-deployment>
      <name>UMSJMSServer522129776</name>
      <target>UMSJMSServer_auto_1</target>
    </sub-deployment>
    <sub-deployment>
      <name>UMSJMSServer1709690790</name>
      <target>UMSJMSServer_auto_2</target>
    </sub-deployment>
    <descriptor-file-name>jms/UMSJMSSystemResource-jms.xml</descriptor-file-name>
  </jms-system-resource>

```

we would like to have three separate files like below for the above file  
1) `DSFilenames.txt`  
2) `JMSModule.txt`  
3) `Subdeployment.txt`

1) `DSFilenames.txt` should contains only the value of `<descriptor-file-name>` .  
In the above example, the expected output should be like below

```nohighlight
  dsfilename=jms/UMSJMSSystemResource-jms.xml

```

2) `JMSModule.txt` should contains values of `<name>` , `<target>` under `<jms-system-resource>`  
In the above example, the expected output should be like below

```nohighlight
  name=UMSJMSSystemResource
  target=soa_server1,bam_server1

```

3) `Subdeployment.txt` should contains values of `<name>` , `<target>` under each `<sub-deployment>` tag  
In the above example, the expected output should be like below

```nohighlight
   name= UMSJMSServer522129776
   target=UMSJMSServer_auto_1
   name=UMSJMSServer1709690790
   target=UMSJMSServer_auto_2

```

Any help on this will be deeply appreciated.

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [March 6, 2018, 3:13am UTC](https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220/2 "2018-03-06T03:13:03Z")

</div>

You may use simple shell techniques (reading file line by line, grep, sed/cut, output redirection) to achieve this; or go for bit more advanced techniques provided by, let's say python's [xml modules](https://docs.python.org/2/library/xml.etree.elementtree.html).

By the way, what have you tried?

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [March 6, 2018, 7:15am UTC](https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220/3 "2018-03-06T07:15:42Z")

</div>

And, in addition to what balajesuri has already said, please also tell us what operating system and shell you're using.

---

<div class="post-metadata">

**Author:** ![Siv51427882](https://community.unix.com/letter_avatar/siv51427882/32/6_449c533f586939b6e5f246632a46421f.png) [@Siv51427882](https://community.unix.com/u/Siv51427882)\
**Post date:** [March 15, 2018, 1:44am UTC](https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220/4 "2018-03-15T01:44:59Z")

</div>

Hello Shell Gurus,

I am able to get the first two files using 'awk'.  
But i need some help on the extraction of the third file  
Consider the following input

```nohighlight
<jms-system-resource>
    <name>UMSJMSSystemResource</name>
    <target>soa_server1,bam_server1</target>
    <sub-deployment>
      <name>UMSJMSServer522129776</name>
      <target>UMSJMSServer_auto_1</target>
    </sub-deployment>
    <sub-deployment>
      <name>UMSJMSServer1709690790</name>
      <target>UMSJMSServer_auto_2</target>
    </sub-deployment>
    <descriptor-file-name>jms/UMSJMSSystemResource-jms.xml</descriptor-file-name>
  </jms-system-resource>

```

In above code i need to extract the following parameters into a separate file.

For above code, i would like to have the following output

```nohighlight
  JMSModuleName=UMSJMSSystemResource
  sub-deploymentname=UMSJMSServer522129776
  targetserver=UMSJMSServer_auto_1
  sub-deploymentname=UMSJMSServer1709690790
  targetserver=UMSJMSServer_auto_2

```

i have tried the following script to get the same

```nohighlight
awk -F"[><]" '
/<jms-system-resource>/{
  a=1
}
a && /<name>/{
  print "JMSModuleName ="$3
  next
}
a && /<target>/{
    next
}
a && /<sub-deployment>/{
  b=1
  }
 b && /<name>/ {
  print "SubdeploymentName ="$3
  next
}
b && /<target>/{
 print "TargetServersName ="$3
 a=""
 b=""
 next
 }
' InputFile

```

But i got the following output

```nohighlight
JMSModuleName=UMSJMSSystemResource
JMSModuleName=UMSJMSServer522129776
JMSModuleName=UMSJMSServer1709690790

```

Any help on this will be greatly helpful.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 15, 2018, 5:33am UTC](https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220/5 "2018-03-15T05:33:09Z")

</div>

Now, this is different from what you requested in post#1, and it is not too clear to me what is actually requested. Given the .xml- file has EXACTLY the structure shown, how far would this get you?

```nohighlight
awk '
/<jms-system-resource>/,\
/<\/jms-system-resource>/ {if (/<.?jms-system-resource>/) {FN = "JMSModule.txt"
                                                                         next
                                                                        }
                                 if (/<.?sub-deployment>/) {FN = "Subdeployment.txt"
                                                                         next
                                                                        }
                                 if (/<.?descriptor-file-name>/) FN = "DSFilenames.txt"

                                 sub (/^ *</, _)
                                 sub (/>/, "=")
                                 sub (/<.*$/, _)
                                 if (FN) print > FN
                                }
' file
cf *.txt

---------- DSFilenames.txt: ----------

descriptor-file-name=jms/UMSJMSSystemResource-jms.xml

---------- JMSModule.txt: ----------

name=UMSJMSSystemResource
target=soa_server1,bam_server1

---------- Subdeployment.txt: ----------

name=UMSJMSServer522129776
target=UMSJMSServer_auto_1
name=UMSJMSServer1709690790
target=UMSJMSServer_auto_2

```

`cf` (cat files) shows the resulting files...

---

<div class="post-metadata">

**Author:** ![Siv51427882](https://community.unix.com/letter_avatar/siv51427882/32/6_449c533f586939b6e5f246632a46421f.png) [@Siv51427882](https://community.unix.com/u/Siv51427882)\
**Post date:** [March 15, 2018, 8:20am UTC](https://community.unix.com/t/splitting-the-xml-file-into-three-different-files/371220/6 "2018-03-15T08:20:30Z")

</div>

Thanks RudiC For your valuable input.  
Let me try your suggestion first
