473,472 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

group and print data seperately

7 New Member
I have an XML file of the form,

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <testResults version="1.2">
  3. <httpSample t="704" lt="704" ts="1306146504248" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-1" dt="text" by="411"/>
  4. <httpSample t="525" lt="525" ts="1306146505234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-2" dt="text" by="411"/>
  5.  
  6.  
  7. <httpSample t="586" lt="586" ts="1306146611316" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-1" dt="text" by="411"/>
  8. <httpSample t="523" lt="523" ts="1306146612307" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-2" dt="text" by="411"/>
  9. <httpSample t="507" lt="507" ts="1306146613306" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-3" dt="text" by="411"/>
  10.  
  11. <httpSample t="535" lt="535" ts="1306146615306" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-5" dt="text" by="411"/>
  12. <httpSample t="526" lt="526" ts="1306146506234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-3" dt="text" by="411"/>
  13. <httpSample t="499" lt="498" ts="1306146507234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-4" dt="text" by="411"/>
  14. <httpSample t="505" lt="505" ts="1306146508234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-5" dt="text" by="411"/>
  15. <httpSample t="536" lt="536" ts="1306146509249" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-6" dt="text" by="411"/>
  16. </testResults>
  17.  
In perl,
i need output for the above in the following form,

lt = 704
lt = 525

lt max value = 704
lt min value = 525
lt average value =

lt = 586
lt = 523
lt = 507

lt max value = 586
lt min value = 507
lt average value =

lt = 535
lt = 526
lt = 498
lt = 505
lt = 536

lt max value = 536
lt min value = 498
lt average value =

kindly help..
May 24 '11 #1

✓ answered by miller

I understand what you want now mate, but no, there is no easy way to do that. That's not standard XML or any such format.

Obviously anything is possible, but when your data doesn't follow accepted formats, then there is no guaranteed way to process it.

- M

5 1934
miller
1,089 Recognized Expert Top Contributor
Use XML::Twig to process the XML, and List::Util to find the min/max/sum/average.

Expand|Select|Wrap|Line Numbers
  1. use List::Util;
  2. use XML::Twig;
  3.  
  4. use strict;
  5. use warnings;
  6.  
  7. my $xml = do {local $/; <DATA>};
  8.  
  9. my $twig = XML::Twig->new;
  10. $twig->parse($xml);
  11.  
  12. my @lts = map {$_->att("lt")} $twig->findnodes(q{//httpSample[@lt]});
  13.  
  14. print join(',', @lts), "\n";
  15.  
  16. print min(@lts), "\n";
  17. print max(@lts), "\n";
  18. print sum(@lts), "\n";
  19. # ... pretty easy from ehre
  20.  
  21. __DATA__
  22. <?xml version="1.0" encoding="UTF-8"?>
  23. <testResults version="1.2">
  24. <httpSample t="704" lt="704" ts="1306146504248" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-1" dt="text" by="411"/>
  25. <httpSample t="525" lt="525" ts="1306146505234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-2" dt="text" by="411"/>
  26. <httpSample t="586" lt="586" ts="1306146611316" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-1" dt="text" by="411"/>
  27. <httpSample t="523" lt="523" ts="1306146612307" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-2" dt="text" by="411"/>
  28. <httpSample t="507" lt="507" ts="1306146613306" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-3" dt="text" by="411"/>
  29. <httpSample t="535" lt="535" ts="1306146615306" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-5" dt="text" by="411"/>
  30. <httpSample t="526" lt="526" ts="1306146506234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-3" dt="text" by="411"/>
  31. <httpSample t="499" lt="498" ts="1306146507234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-4" dt="text" by="411"/>
  32. <httpSample t="505" lt="505" ts="1306146508234" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-5" dt="text" by="411"/>
  33. <httpSample t="536" lt="536" ts="1306146509249" s="true" lb="HTTP Request" rc="200" rm="OK" tn="Thread Group 2-6" dt="text" by="411"/>
  34. </testResults> 
  35.  
As already posted on perlmonks: http://www.perlmonks.com/?node_id=906521
May 24 '11 #2
techtween
7 New Member
Thanks a ton miller... But what should i do if there is an empty new line between my httpSample tags and i need to retrieve the tags above the empty new line into an array and the tags below the empty new line into another array and calculate the same(min, max and avg for each array seperately)
May 25 '11 #3
miller
1,089 Recognized Expert Top Contributor
I understand what you want now mate, but no, there is no easy way to do that. That's not standard XML or any such format.

Obviously anything is possible, but when your data doesn't follow accepted formats, then there is no guaranteed way to process it.

- M
May 25 '11 #4
mirod
1 New Member
It is a very bad idea to rely on non significant whitespace (ie empty lines) when processing XML. This will prevent you from using a good number of XML tools that assume that the structure of the data is tagged explicitly, with.. tags, not implicitly through formatting. You also run the risk of an XML tool removing those empty lines. The elements for each series of test should be wrapped in a composite element, to mark the structure. Just like in HTML an ul element wraps around the li's.

So if you can, change the structure of the XML to
Expand|Select|Wrap|Line Numbers
  1. <testResults version="1.2">
  2.   <test>
  3.     <httpSample>...</httpSample>
  4.     <httpSample>...</httpSample>
  5.   </test>
  6.   <test>...</test>
  7. </testResults>
If you can't, then the following code, using XML::Twig, will do: the lt values are stored in an array, and if the text element before the httpSample includes 2 line feeds, then it processes the array.

If you are not familiar with XML::Twig, what you have to know for this is that the twig_handlers bit declares handlers that are called for each httpSample element, the handler is called with $_ set to an object representing the element.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use XML::Twig;
  7. use List::Util qw( max min sum);
  8.  
  9. my $lt=[]; # an array with the list of lt values
  10.  
  11. XML::Twig->new( keep_spaces => 1, # so empty lines are not discarded
  12.                 twig_handlers => { httpSample => sub { process_test( $lt) if @$lt && $_->prev_sibling_text=~ m{\n.*\n};
  13.                                                        push @$lt, $_->att( 'lt'); },
  14.                                  },
  15.               )
  16.          ->parsefile( "test_data.xml");
  17.  
  18. process_test( $lt) if @$lt; # to process the last batch 
  19.  
  20.  
  21. sub process_test
  22.   {  print "lt max value: ", max( @$lt), "\nlt min value: ", min( @$lt), "\n lt average value: ", sum( @$lt)/@$lt,
  23.            "\n\n", join( "\n", map { "lt: $_" } @$lt), "\n\n";
  24.      $lt=[];
  25.   }
  26.  
I hope that helps
May 26 '11 #5
techtween
7 New Member
This was the exact result i was craving for.. Mirod loads of thanks to you, not only for the solution but also for the concept(XML::Twig , just now learnt coz of you) and also thanks again miller for your timely assistance:)
May 26 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marcel Seiler | last post by:
Hi NG I would like to print my productlist, stored in a access-database. therefore I fill a dataset with the data and with a loop I place the datarecords on the printdocument. On each page I...
0
by: RDI | last post by:
I need to watch a certain print spooler and if a print job appears, extract the print data to a PRN file on the hard disk and delete it from the spooler. Can VB.Net do this? If so, can someone...
2
by: Mitul | last post by:
Hello friends, I am getting stuck at a point in my project. I would like to give functionality of print data from site. but there is a problem is that if there are lots of data which takes...
2
by: igotyourdotnet | last post by:
I'm getting my dataset like this: SalesMan Sales Make Smith 25,000 1 Smith 9500 10 Smith 72,252 1 Smith 125,000 ...
1
hariharanmca
by: hariharanmca | last post by:
any know how to Print data through Store Procedure to the printer Directly
2
by: 183nbt | last post by:
At Access report, data printed vertically like below. 1 2 3 4 5 How can I setup printing these data display and print horizontally at report like below.
1
by: vanand | last post by:
hai, I have wanted to print the data in word document from vb. Now i can able to print the data from database to word without format layout. I need to format the data in word. Kindly send your...
1
by: mehdi1727 | last post by:
How Print Data Grid View in C#.Net??????????
3
by: ghjk | last post by:
I'm using php. In my application there are 2 pages. data.php and print.php. User select some data range to get a report in data.php and i want to open a print.php with the data according to user...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.