Splitting the XML file into three different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting the XML file into three different files
# 1  
Old 03-06-2018
Splitting the XML file into three different files

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.
Code:
  <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
Code:
  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
Code:
  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
Code:
   name= UMSJMSServer522129776
   target=UMSJMSServer_auto_1
   name=UMSJMSServer1709690790
   target=UMSJMSServer_auto_2

Any help on this will be deeply appreciated.

Last edited by Don Cragun; 03-06-2018 at 08:45 AM.. Reason: Add missing CODE and ICODE tags.
# 2  
Old 03-06-2018
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.

By the way, what have you tried?

Last edited by balajesuri; 03-06-2018 at 04:21 AM..
# 3  
Old 03-06-2018
And, in addition to what balajesuri has already said, please also tell us what operating system and shell you're using.
# 4  
Old 03-15-2018
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

Code:
<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
Code:
  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

Code:
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
Code:
JMSModuleName=UMSJMSSystemResource
JMSModuleName=UMSJMSServer522129776
JMSModuleName=UMSJMSServer1709690790

Any help on this will be greatly helpful.

Last edited by Scrutinizer; 03-15-2018 at 03:37 AM.. Reason: Additional code tags
# 5  
Old 03-15-2018
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?
Code:
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...
This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-15-2018
Thanks RudiC For your valuable input.
Let me try your suggestion first
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the XML file and renaming the files

Hello Gurus, I have a requirement to split the xml file into different xml files. Can you please help me with that? Here is my Source XML file <jms-system-resource> <name>PS6SOAJMSModule</name> <target>soa_server1</target> <sub-deployment> ... (3 Replies)
Discussion started by: Siv51427882
3 Replies

2. Shell Programming and Scripting

Splitting CSV into variables then to XML file

I have a text file that looks like this: FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 I need it to turn it into an XML file to run against a custom application. My ultimate goal is for it to look like... (15 Replies)
Discussion started by: jeffs42885
15 Replies

3. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

4. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

5. Shell Programming and Scripting

XML Splitting into multi files

Hi , I have a XML file like below file name : sample.xml <?xml version="1.0"?> <catalog> <author>Rajini</author> <title>XML Guide</title> <Text> </Text> <genre>Computer</genre> <price>44.95</price> </catalog> <?xml version="1.0"?> <catalog> ... (5 Replies)
Discussion started by: karthinvk
5 Replies

6. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

7. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

8. Shell Programming and Scripting

splitting a file (xml) into multiple files

To split the files Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Test.xml --------- <?xml version="UTF_8"> <emp: ....> <name>a</name> <age>10</age> </emp> <?xml version="UTF_8"> <emp: ....> <name>b</name> <age>10</age>... (11 Replies)
Discussion started by: sasi_u
11 Replies

9. Shell Programming and Scripting

splitting huge xml into multiple files

hi all i have a some huge html files (500MB to 1GB). Each file has multiple <html></html> tags <html> ................. .................... .................... </html> <html> ................. .................... .................... </html> <html> .................... (5 Replies)
Discussion started by: uttamhoode
5 Replies

10. Shell Programming and Scripting

Splitting huge XML Files into fixsized wellformed parts

Hi, I need to split xml-files with sizes greater than 2 gb into smaler chunks. As I dont want to end up with billions of files, I want those splitted files to have configurable sizes like 250 MB. Each file should be well formed having an exact copy of the header (and footer as the closing of the... (0 Replies)
Discussion started by: Malapha
0 Replies
Login or Register to Ask a Question