Home: Perl Programming Help: Intermediate:
Merge duplicated xml lines



cibien
Novice

Aug 8, 2012, 6:48 AM


Views: 3288
Merge duplicated xml lines

With xml:libxml and Spreadsheet::ParseExcel

and the command " $materialmapping_table_xml->createElement("item"); " I generate lines from excel document.

for example:

<item pr_night_curtain="a" duble="2" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="b" duble="2" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="c" duble="2" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="a" duble="1" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="b" duble="1" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="c" duble="1" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="a" duble="2" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="b" duble="2" internal="co_1" pr_family="2" description=" example"/>
<item pr_night_curtain="c" duble="2" internal="co_1" pr_family="2" description=" example"/>

How I can merge the duplicated xml lines (the first 3 and the last 3 in example)??

thanks


Laurent_R
Veteran / Moderator

Aug 8, 2012, 11:43 AM


Views: 3268
Re: [cibien] Merge duplicated xml lines

Assuming you want to filter out the duplicate lines, you can populate a hash with your lines and skip lines that already exist in the hash.

Something like this, assuming that all the lines are stored in @array_of_lines:


Code
my %seen_lines; # the hash %seen_lines contains the lines that have already been met 
foreach my $line (@array_of_lines) {
next if defined $seen_lines{$line};
print $line;
$seen_lines{$line} = 1;
}



cibien
Novice

Aug 9, 2012, 1:23 AM


Views: 3241
Re: [Laurent_R] Merge duplicated xml lines

Thankyou very much for your answer, but sorry I'am novice in perl and is very difficult for me.

this is a little part of my code:


my $materialmapping_table_xml = XML::LibXML->createDocument( "1.0", "UTF-8");
my $materialmapping_table_xml_root = $materialmapping_table_xml->createElement("masterdata");
$materialmapping_table_xml->setDocumentElement($materialmapping_table_xml_root);


my $materialmapping_item3 = $materialmapping_table_xml->createElement("item3");

$materialmapping_item3->setAttribute(decode('cp1252',$pr_cell_name3->{Val}),$pr3);

$materialmapping_item3->setAttribute("pr_family",$family);

$materialmapping_table_xml_root->addChild($materialmapping_item3);


and the same for item2...


for example, generate this output:


<item2 duble="1" pr="c" pr_width="1250" pr_family="2" />
<item2 duble="2" pr="c" pr_width="1250" pr_family="2" />
<item2 pr="a" duble="2" pr_width="1250" pr_family="2" />
<item2 pr="b" duble="2" pr_width="1250" pr_family="2" />
<item2 pr="c" duble="2" pr_width="1250" pr_family="2" />
<item3 pr="a" duble="1" pr_width="1250" pr_family="2" />
<item3 pr="b" duble="1" pr_width="1250" pr_family="2" />
<item3 pr="c" duble="1" pr_width="1250" pr_family="2" />
<item3 pr_width="1250" duble="1" pr="c" pr_family="2" />
<item3 pr="a" duble="2" pr_width="1250" pr_family="2" />
<item3 pr="b" duble="2" pr_width="1250" pr_family="2" />
<item3 pr="c" duble="2" pr_width="1250" pr_family="2" />
<item3 pr_width="1250" duble="2" pr="c" pr_family="2" />
<item3 pr_width="1250" pr="c" duble="2" pr_family="2" />
<item3 pr_width="1250" pr="c" duble="2" pr_family="2" />
<item3 pr_width="1250" pr="c" duble="2" pr_family="2" />
<item2 pr="a" duble="3" pr_width="1250" pr_family="2" />

but the OUTPUT must be:

<item3 pr="a" duble="1" pr_width="1250" pr_family="2" />
<item3 pr="b" duble="1" pr_width="1250" pr_family="2" />
<item3 pr="c" duble="1" pr_width="1250" pr_family="2" />
<item3 pr="a" duble="2" pr_width="1250" pr_family="2" />
<item3 pr="b" duble="2" pr_width="1250" pr_family="2" />
<item3 pr="c" duble="2" pr_width="1250" pr_family="2" />
<item2 pr="a" duble="3" pr_width="1250" pr_family="2" />


where I have to put the code?


thankyou very much for your precious help


(This post was edited by cibien on Aug 9, 2012, 1:30 AM)


Laurent_R
Veteran / Moderator

Aug 9, 2012, 10:42 AM


Views: 3213
Re: [cibien] Merge duplicated xml lines

Hi,

I do not know XML::LibXML, so I don't really understand what your code is doing, and I don't know exactly where you should filter the output. But it should be right after the output is completely generated and before it is printed out.