XML Splitting into multi files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML Splitting into multi files
# 1  
Old 12-04-2014
XML Splitting into multi files

Hi ,

I have a XML file like below

file name : sample.xml

Code:
<?xml version="1.0"?>
<catalog>
      <author>Rajini</author>
      <title>XML Guide</title>
      <Text> [<?xml version="1.0"?>]</Text>
      <genre>Computer</genre>
      <price>44.95</price>
</catalog>
<?xml version="1.0"?>
<catalog>
      <author>Kamal</author>
      <title>XML Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
</catalog>
<?xml version="1.0"?><catalog><author>Rajini</author><title>XML Guide</title><genre>Lap</genre><price>44.95</price></catalog>
<?xml version="1.0"?><catalog><author>Kamal</author><Text> [<?xml version="1.0"?>]</Text><genre>Lap</genre><price>44.95</price></catalog>


Here the XML format is invalid which means the alignment is not proper.
Need to format as valid alignment.
then i need to split into multi file based on distinct <genre> value.
For example output would be like below (2 files)

FYI: input and output file has been attached in this post for reference

File 1 : Computer.xml

Code:
<?xml version="1.0"?>
<catalog>
      <author>Rajini</author>
      <title>XML Guide</title>
      <Text> [<?xml version="1.0"?>]</Text>
      <genre>Computer</genre>
      <price>44.95</price>
</catalog>
<?xml version="1.0"?>
<catalog>
      <author>Kamal</author>
      <title>XML Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
</catalog>

---------------------------------

File 2 : Lap.xml

Code:
<?xml version="1.0"?>
<catalog>
      <author>Rajini</author>
      <title>XML Guide</title>
      <genre>Lap</genre>
      <price>44.95</price>
</catalog>
<?xml version="1.0"?>
<catalog>
      <author>Kamal</author>
      <Text> [<?xml version="1.0"?>]</Text>
      <genre>Lap</genre>
      <price>44.95</price>
</catalog>

Thanks for advance your help

Last edited by Don Cragun; 12-04-2014 at 11:16 PM.. Reason: Add CODE tags.
# 2  
Old 12-05-2014
Any attempts from your side?
# 3  
Old 12-05-2014
XML Splitting into multi files

RudiC,


Thanks for looking my post,


Ya just i am getting the exact line out using this command
Then can use fol loop using temp.txt file.
This is my rough idea


Code:
grep -nx '<?xml version="1.0"?>' Sample.xml | awk -F ":" '{print $1}' > temp.txt


Last edited by vbe; 12-05-2014 at 08:08 AM.. Reason: Code tag added
# 4  
Old 12-05-2014
Formatting
Code:
sed 's/></>\n</g' Sample.xml | sed -r "s/^<aut|^<tit|^<gen|^<pri|^<Tex/      &/" > temp.xml

Splitting
Code:
awk 'BEGIN {CF="Computer.xml"; LF="Lap.xml"}
    $0 ~ /<genre>Computer/ { A[++c]=$0; C=1; next }
    $0 !~ /^<\/catalog>/ { A[++c]=$0; next }
    {
    A[++c]=$0
    for (i in A) if (C) {print A[i] >>CF} else {print A[i] >>LF}
    }
    {delete A; c=C=0}
' temp.xml

# 5  
Old 12-05-2014
Hi junior-helper,

Thank you

The output seems not expected. some tags are not included.
Anyway thanks guys.

exactly what i would like to know is that, is there any pre-defined function exist to parse the XML like xmllint ,xml -csplit etc..?

If it is there to parse and split , please inform
it would be helpful for us.
# 6  
Old 12-05-2014
I suspect your XML isn't what you posted. Could you post example data which hasn't been "prettied up"?

And, not really, no.
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 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

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

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

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

6. Shell Programming and Scripting

Xml multi parse question

awk -F "" '/<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>/ {print $2, $3}' OFS='\t' Test.xml The above code works great, but lets say I wanted the <Analyte> name (<Name>STAT3). The word <name> is unique so there will be multiple records pulled. Is there a way to... (8 Replies)
Discussion started by: cmccabe
8 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