Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight


by Anonymous Monk
on Jul 30, 2005 at 17:47 UTC ( #479625=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everyone,

I have a doubt in using XML in perl. I am trying to put an output of a perl script to an xml file. The idea being that the xml file can be opened in the MS excel spread sheet.

This is the print statement of the perl script.

foreach $app(keys %appsrvr) { print "\n$app"; foreach $attribute(keys %{ $appsrvr{$app} }) { print " $appsrvr{$app}{$attribute}"; } } print "\n";
How do I put the above print stmt in an xml format file so that I can open the xml file in an excel spread sheet.

CODE tags added by Arunbear

Replies are listed 'Best First'.
by atcroft (Monsignor) on Jul 30, 2005 at 18:13 UTC

    Have you taken a look at the XML::Simple module? It includes an example in its docs for outputting data from a hash structure as XML.


by sk (Curate) on Jul 30, 2005 at 18:08 UTC
by pg (Canon) on Jul 30, 2005 at 19:36 UTC

    If all what you wanted was to be able to open the file in Excel, XML is sort of over-engineered. Why not just go CSV file format?

      It depends on whether you want a spreadsheet or lines of data. Granted, most on the time lines of data work but saying CSV is a spreadsheet is like saying Excel is a database. With the right XML, you can also create functional cells (SUM, AVERAGE, etc. ) and with some work, PivotTables and Charts.

      Everything I've learned in life can be summed up in a small perl script!
by BaldPenguin (Friar) on Jul 30, 2005 at 20:50 UTC
    You didn't say was format of XML, whether you are going with the basics or using ExcelXML, but this works for me.
    use strict; use warnings; use XML::Element; my $root = new XML::Element('root'); foreach my $app ( keys %appsrvr ) { my $el = new XML::Element( $app , %{$appsrvr{$app}} ); $root->push_content($el); } print $root->as_XML();
    It's untested but should work. Currently I push all my xml to a really basic form, then use XSL StyleSheets to transform into the requested formats ( html, csv, ExcelXML, etc. );

    Everything I've learned in life can be summed up in a small perl script!
      Hi Everyone,
      I want a spread sheet of data.
      I did try some xml code of my own but I am not sure ..if this is working.For some reason all the data is coming into 1 cell....
      I was the data to appear as
      server1 Running 2005-07-24 22:58:06 server2 Running 2005-07-26 13:48:23 server3 Running 2005-07-24 22:51:20
      in the spread sheet. I have an immediate deadline...any further help to debug the code will be appreciated.

      BTW:Don I did try your suggestion with the XML::Element but I think we need some lib' for that which we dont hv in our prod environment. <
        Here is the code I was trying to work on...but it gets created on 1 cell when I try to open it in a excel spread sheet
        # start an XML worksheet open (my $xmlfl,"+>>$path/$file") or die "$file failed: $!\n" +; print $xmlfl "<?xml version=\"1.0\"?>\n"; print $xmlfl "<Workbook xmlns=\"urn:schemas-microsoft-com:offi +ce:spreadsheet\"\n"; print $xmlfl " xmlns:o=\"urn:schemas-microsoft-com:office:offi +ce\"\n"; print $xmlfl " xmlns:x=\"urn:schemas-microsoft-com:office:exce +l\"\n"; print $xmlfl " xmlns:ss=\"urn:schemas-microsoft-com:office:spr +eadsheet\">\n"; print $xmlfl " <Worksheet ss:Name=\"Policy-Groups\">\n"; print $xmlfl " <Table>\n"; # print $xmlfl the header row # <group> <policy1> <policy2> ... <policyn> print $xmlfl " \n<Row>"; foreach $app(keys %appsrvr) { print $xmlfl " <Cell><Data ss:Type=\"String\">$app< +/Data></Cell>"; print $xmlfl " </Row>"; print $xmlfl " <Row>\n"; foreach $attribute(keys %{ $appsrvr{$app} }) { #print " $appsrvr{$app}{$attribute}"; print $xmlfl " <Row>\n"; print $xmlfl " <Cell><Data ss:Type=\"String\">$appsrvr{$app +}{$attribute}</Data></Cell>\n"; print $xmlfl " </Row>\n"; } # print $xmlfl " </Row>\n" } # print "\n"; # End the XML worksheet print $xmlfl " </Table>\n"; print $xmlfl " </Worksheet>\n"; print $xmlfl "</Workbook>\n"; # close this group file

        CodeTags™ by holli

        HTML::TreeBuilder is required for XML::Element

        Everything I've learned in life can be summed up in a small perl script!
by davidrw (Prior) on Jul 31, 2005 at 19:33 UTC
    A lot of good answers above.. i just wanted to mention two side notes:
    first is that (if it suits your requirements) you can use Win32::OLE to control excel.
    Second is a quick point to meryln's node "Question" vs "Doubt" in reference to the phrasing of the node ("I have a doubt in using..."). (and by no means making any judgements based on language usage--i just also, as a pretty much english-only monk, find the word switch interesting; onther than that single word i'd have no reason to believe the node wasn't written by a native english speaker)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://479625]
Approved by gmax
[LanX]: they?
[LanX]: click!
[choroba]: they
[LanX]: why do you think it's more than one person?
[Your Mother]: Because we are LEGION! Fix YOUR SIG SIG BLOCK!!!!
[Your Mother]: Forgot to sign in as my sockpuppet, how embarrassing!
[1nickt]: pryrt I am creating a Type to check valid user IDs, which must be a whole number greater than or equal to zero. I would like to disallow 1.0 but because of this behaviour, by the time it is checked by the constraint, it *is* an Int.
[LanX]: should this be considered? Re: Parsing .txt into arrays
[LanX]: and this Re^4: Hash user input
[pryrt]: LanX, I would vote "keep" if it were considered: it's not helpful, but it could be taken multiple ways, not all of which are offenseive...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-05-24 20:11 GMT
Find Nodes?
    Voting Booth?