Need to Split Big XML into multiple xmls


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to Split Big XML into multiple xmls
# 1  
Old 10-28-2010
Need to Split Big XML into multiple xmls

Hi friends..
We have urgent requirement.We need to split the big xml having multiple orders into multiple xmls
having each order in each xml.
For Example
In input XMl will be in following format with multiple line orders..
Code:
<OrderDetail BillToKey="20100805337" Createuserid="CreateGuestOrder" >
<Order Number="1">
<OrderLine CarrierServiceCode="G2" FulfillmentType="ShipToHome"/>
<Item ItemDesc="iPearl 8GB MP3 " ItemID="11239924" ItemWeight="0.00" />
<LinePriceInfo ActualPricingQty="1.00" BundleTotal="0.00" DiscountPercentage="0.00" LineTotal="80.79" />
</Order>
<Order Number="2">
<OrderLine CarrierServiceCode="H2" FulfillmentType="ShipToHome" />
<Item ItemDesc="TV" ItemID="112345424" ItemWeight="67.00" />
<LinePriceInfo ActualPricingQty="1.00" BundleTotal="0.00" DiscountPercentage="0.00" LineTotal="80.79" />
</Order>
<Order Number="3">
<OrderLine CarrierServiceCode="M2" FulfillmentType="ShipToHome" />
<Item ItemDesc="TV" ItemID="4545345" ItemWeight="67.00" />
<LinePriceInfo ActualPricingQty="6.00" BundleTotal="0.00" DiscountPercentage="0.00" LineTotal="80.79" />
</Order>
</OrderDetail>

Output should:
Here we want to split this xml into 3 xmls.
Ist xml should contain Order Number=1 and 2nd xml will have Order Number=2 and so on...
Thanks & Regards
Prakash

Last edited by Scott; 10-28-2010 at 06:57 AM.. Reason: Use code tags, please...
This User Gave Thanks to dprakash For This Post:
# 2  
Old 10-28-2010
Try this,

Code:
awk '/Order Number=\"/ {a=$0;i=0;while(i<4){getline;a=a"\n"$0;++i};print a > ++j".xml"}' input.xml

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 10-28-2010
HI Pravin..
thanks for quick reply...
i executed your command...where im getting 3 xmls...
but ...</Order> tag is not coming in 2nd and 3rd xml ,but coming in 1st xml.
could you plz llook into once again.......
This User Gave Thanks to dprakash For This Post:
# 4  
Old 10-28-2010
Its working fine at my end.

try this,
Code:
awk '/Order Number=\"/ {a=$0;i=0;while(i<4){getline;a=a"\n"$0;i++};print a > ++j".xml"}' input.xml

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 10-28-2010
thanks a lot pravin..its working.....

---------- Post updated at 07:24 AM ---------- Previous update was at 06:38 AM ----------

Hi Pravin
we are not able to generate our real time Order xml with ur command..
Below dot shows n no of lines....
and we need to capture data between <Order Number="1"> tag to </Order> tag for different orders
and also we need to add other tags like than <Order> to each xml
for example:
here PaymentMethods and LineCharge tag data will be needed to add in each xml.
Code:
<OrderDetail BillToKey="20100805337" Createuserid="CreateGuestOrder" >
<Order Number="1">
<OrderLine CarrierServiceCode="G2" FulfillmentType="ShipToHome"/>
<Item ItemDesc="iPearl 8GB MP3 " ItemID="11239924" ItemWeight="0.00" />
.....
.
....
....
..

</Order>
<Order Number="2">
<OrderLine CarrierServiceCode="H2" FulfillmentType="ShipToHome" />
....
....
...
</Order>
<Order Number="3">
<OrderLine CarrierServiceCode="M2" FulfillmentType="ShipToHome" />
<Item ItemDesc="TV" ItemID="4545345" ItemWeight="67.00" />
<LinePriceInfo ActualPricingQty="6.00" BundleTotal="0.00" DiscountPercentage="0.00" LineTotal="80.79" />
....
......
.........
............
</Order>

<PaymentMethods>
<PaymentMethod HoldType="ADDRESS_VAL_HOLD" Status="1100" StatusDescription="Created" />
...
.....
</PaymentMethods>
<LineCharge ChargeAmount="0.00" ChargeCategory="SHIPPING" ChargeName="ShippingCharge" />
</OrderDetail>


Last edited by Scott; 10-28-2010 at 11:57 AM.. Reason: Code tags, please...
This User Gave Thanks to dprakash For This Post:
# 6  
Old 10-28-2010
Try this Perl script-- parse.pl,

Code:
#!/usr/bin/perl

use strict;
my $ord_flg;
my $paymnt_flag;
my $str;

open (FH,"<","/path/to/ur/inputxmlfile") || die "can not open file\n";

while (<FH>) {
if (/\<PaymentMethods\>/) {
       $str=$_;
       $paymnt_flag=1;
       next;
       }
if (/\<\/PaymentMethods\>/) { $str=$str.$_; $paymnt_flag = 0;}
if ($paymnt_flag == 1 && $_ !~ /\<\/PaymentMethods\>/) {
$str=$str.$_;
}
if (/<LineCharge/) { $str=$str.$_;}
}

close(FH);

open (FH,"<","/path/to/ur/inputxmlfile") || die "can not open file\n";
my $i=1;
my $filename;

while (<FH>) {
if (/\<Order Number=\"/) {
        $filename=$i."\.xml";
        open (FW,">","/path/output/$filename") || die "Can not create file for write\n";
        print FW $_;
        $ord_flg=1;
        $i++;
        next;
       }
if (/\<\/Order\>/) { print FW "$_$str"; $ord_flg = 0;close(FW);}
if ($ord_flg == 1 && $_ !~ /\<\/Order\>/) {
print FW $_;
}
}
close(FH);

Invocation
Code:
perl parse.pl

These 2 Users Gave Thanks to pravin27 For This Post:
# 7  
Old 10-28-2010
Code:
awk 'NR==1{x=$0;next}/<\/Order>/{print y RS $0 RS "</OrderDetail>">f}/Order Number/{f="file"++n".xml";y=x}{y=y RS $0}' file.xml

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split Big XML file Base on tag

HI I want to split file base on tag name. I have few header and footer on file <?xml version="1.33" encing="UTF-8"?> <bulkCmConfigDataFile" <xn:SubNetwork id="ONRM_ROOT"> <xn:MeContext id="PPP04156"> ... (4 Replies)
Discussion started by: pareshkp
4 Replies

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

3. Shell Programming and Scripting

Split a big file into multiple files using awk

this thread is a continuation from previous thread https://www.unix.com/shell-programming-and-scripting/223901-split-big-file-into-multiple-files-based-first-four-characters.html ..I am using awk to split file and I have a syntax error while executing the below code I am using AIX 7.2... (4 Replies)
Discussion started by: etldev
4 Replies

4. HP-UX

How to split big file on HP-UX and join on Windows?

Hi HP-admins, I have 120GB file on HP-UX and need to split to 4GB pieces and join them on Windows. As I don't want to use zipsplit, tried to use split command and join on windows using "copy /b" but it doesn't work (It merges and creates new file but file is corrupt) What is the correct... (6 Replies)
Discussion started by: prvnrk
6 Replies

5. Shell Programming and Scripting

Split a big file into multiple files based on first four characters

I have a requirement to split a huge file to smaller text files based on first four characters which look like ABCD 1234 DFGH RREX : : : : : 0000 Each of these records are OF EQUAL bytes with a different internal layout based on the above first digit identifier.. Any help to start... (5 Replies)
Discussion started by: etldev
5 Replies

6. Shell Programming and Scripting

split XML file into multiple files based on pattern

Hello, I am using awk to split a file into multiple files using command: nawk '{ if ( $1 == "<process" ) { n=split($2, arr, "\""); file=arr } print > file }' processes.xml <process name="Process1.process"> ... (3 Replies)
Discussion started by: chiru_h
3 Replies

7. UNIX for Advanced & Expert Users

Split a big file into two others files

Hello, i have a very big file that has more then 80 MBytes (100MBytes). So with my CVS Application I cannot commit this file (too Big) because it must have < 80 MBytes. How can I split this file into two others files, i think the AIX Unix command : split -b can do that, buit how is the right... (2 Replies)
Discussion started by: steiner
2 Replies

8. Shell Programming and Scripting

Help Needed : Split one big file to multiple files

Hi friends, I have data in flat file as following, first filed is the customer number. We have almost 50-100 customers in the system 100 ABC A123 100 BVC D234 100 BNC N324 200 CBC A122 200 AVC D294 200 HNC N324 300 GBC A173 300 FVC D234 300 DNC N344 I want to split the file and... (5 Replies)
Discussion started by: monicasgupta
5 Replies

9. UNIX for Dummies Questions & Answers

Split BIG report using nawk

I have the following nawk script: nawk -F: '{ if($0 ~ "^Report No") {fl=1; i=0;} if(fl==1){data=$0; i++} if($0 ~ "^BE NO:") { fname = "reprot_"$2".lis"; gsub(" ","",fname); for(j=0;j<i;j++) print data > fname; fl=0; } else if(fl==0) print $0 > fname; }' filename When I try to apply... (1 Reply)
Discussion started by: raychu65
1 Replies

10. Shell Programming and Scripting

Split a Big Report.

Hi All, I am a newbie for Unix Script. I have report like the following: (file name: Report.txt): Report No.: AAA BE NO: 111 asfsdflsjdfklsdjfklsjfklsfsflsjdlk fsdfjsfkjsklfsfj Report No.: AAA BE NO: 111 sdfsdfjsdklfjsfkj fsdflkjsdfklsjkl Report No.: AAA (16 Replies)
Discussion started by: raychu65
16 Replies
Login or Register to Ask a Question