Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

First, I don't think the code you posted work as you expect. The the elements of the two arrays (@Name and @Address must be enclosed between parens. Otherwise, you will only get the first element of the array.

Secondly, I find a bit cumbersome the way your datas are divided in two arrays, one for the names and one for the addresses, unless they are provided to you in this way, I would suggest you put all the user information in an array (or a hash), and put all these arrays into a big one (see perldsc for more infos on data structures.)

Now, about XML::Writer, it tend to agree with Mr. Q&A, I don't like the File::IO voodoo thing you have to do, so I prefer just printing the XML to STDOUT.
As for optimizing you XML::Writer code, you could probably use a loop to go through each element of your structure and write the proper dataElements for each name and address.
The final code should probably look like the following:

#!usr/bin/perl -w use strict; use XML::Writer; my $writer = new XML::Writer( DATA_MODE => 1, DATA_INDENT => 4 ); my @Name = ("Fred Flintsone", "Barney Rubble"); my @Address = ("111 Rock River Lane", "113 Rock River Lane"); my @flinstones; for (0 .. $#Name){ push @flinstones, [ $Name[$_], $Address[$_] ] } # which is the same as : @flinstones = ( [ 'Fred Finstone', '111, Rock River Lane' ], [ 'Barney Rubble', '113, Rock River Lane' ], ); $writer->xmlDecl( "UTF-8", 1 ); $writer->startTag( "Application"); foreach my $character (@flinstones){ $writer->startTag("Customer"); $writer->dataElement("Name", $character->[0] ); $writer->dataElement("Address", $character->[1] ); $writer->endTag("Customer"); } $writer->endTag("Application"); $writer->end(); __END__ Results: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Application> <Customer> <Name>Fred Finstone</Name> <Address>111, Rock River Lane</Address> </Customer> <Customer> <Name>Barney Rubble</Name> <Address>113, Rock River Lane</Address> </Customer> </Application>

Oh, and I'll probably lowercase the name of all the XML elements, but that's just me, maybe you can't do that in your application...

my $OeufMayo = new PerlMonger::Paris({http => ''});</kbd>

In reply to Re: Help with XML::Writer by OeufMayo
in thread Help with XML::Writer by basicdez

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (6)
    As of 2018-06-19 15:02 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (114 votes). Check out past polls.