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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required in Splitting a xml file into multiple and appending it in another .xml file
# 1  
Old 07-25-2012
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:

Code:
<?xml version="1.0"?>
<school>
<student id="1">
   <name>john</name>
   <age> 10</age>
</student>
<teacher id="1">
   <name>stella</name>
  <age> 24</age>
</teacher>
<student id="2">
   <name>amy</name>
   <age> 11</age>
</student>
</school>

out put should be 3 xmls and it needs to be appended in a file:
Code:
<?xml version="1.0"?>
<school>
 <student id="1">
   <name>john</name>
   <age> 10</age>
 </student>
</school>

<?xml version="1.0"?>
<school>
<teacher id="1">
   <name>stella</name>
  <age> 24</age>
</teacher>
</school>


<?xml version="1.0"?>
<school>
  <student id="2">
   <name>amy</name>
   <age> 11</age>
</student>
</school>

This User Gave Thanks to ganesan kulasek For This Post:
# 2  
Old 07-25-2012
I can make code that will work for that exact file but will probably fail for your real application. So it'd be better off to see the actual data you want split in the first place, with confidential parts obscured of course.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-25-2012
HI Corona, Thanks for the reply.
Please find the below sample actual xml, in this below example i need to create 4 seperate xmls and update in a .xml file. I will get xml file with only in any of the below four child elements under root element <notificatoin>. " alarmnew, AlarmCleared, AlarmChanged, ackstate" .

Code:
<?xml version="1.0"?>
<notification>

<alarmNew sys="abc">
   <Time>2006-04-18T16:04:35</Time>
    <Problem>30425</Problem>
</alarmNew>

<AlarmCleared sys= "xyz">
   <Time>2006-04-18T16:04:35</Time>
  <Problem>30163</Problem>
</AlarmCleared>

<AckState sys= "xyz">
   <Time>2006-04-18T16:04:35</Time>
   <Problem>30163</Problem>
</AckState>

<AlarmChanged sys= "xyz">
    <Time>2006-04-18T16:04:35</Time>
     <Problem>30163</Problem>
</AlarmChanged>

</notification>


Sample output first xml:
Code:
<?xml version="1.0"?>
<notification>

<alarmNew sys="abc">
   <Time>2006-04-18T16:04:35</Time>
    <Problem>30425</Problem>
</alarmNew>

</notification>

This User Gave Thanks to ganesan kulasek For This Post:
# 4  
Old 07-26-2012
perl

Code:
local $/;
my $str = <DATA>;
my @arr = $str =~ /(<(\S+).*?<\/\2>)(?=.*school)/msg;
for(my $i=0;$i<=$#arr;$i=$i+2){
	print '<?xml version="1.0"?>
<school>';
	print "\n";
	print $arr[$i],"\n";
	print "</school>\n\n\n";
}
__DATA__
<?xml version="1.0"?>
<school>
<student id="1">
   <name>john</name>
   <age> 10</age>
</student>
<teacher id="1">
   <name>stella</name>
  <age> 24</age>
</teacher>
<student id="2">
   <name>amy</name>
   <age> 11</age>
</student>
</school>

This User Gave Thanks to summer_cherry For This Post:
# 5  
Old 07-26-2012
Code:
$ awk -F"\n" -v RS="" 'NR>1 && NF>1 { print "<?xml version="1.0"?>\n<notification>\n", $0, "\n</notification>\n\n" > (NR-1)".xml" }' inputfile

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

3. Shell Programming and Scripting

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. ... (5 Replies)
Discussion started by: Siv51427882
5 Replies

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

5. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

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

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

8. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

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

10. Shell Programming and Scripting

appending content in a xml file

Please help me on this..... i have a file which has following content: <IPCoreProducerConfig> <Producer> <config> <key>machineId</key> <value>machine1</value> </config> <config> ... (4 Replies)
Discussion started by: Aditya.Gurgaon
4 Replies
Login or Register to Ask a Question