CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



cibien
Novice

Aug 8, 2012, 6:48 AM

Post #1 of 4 (2013 views)
Merge duplicated xml lines Can't Post

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

Post #2 of 4 (1993 views)
Re: [cibien] Merge duplicated xml lines [In reply to] Can't Post

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

Post #3 of 4 (1966 views)
Re: [Laurent_R] Merge duplicated xml lines [In reply to] Can't Post

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

Post #4 of 4 (1938 views)
Re: [cibien] Merge duplicated xml lines [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives