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

Help with XML::Writer

by basicdez (Pilgrim)
on Aug 31, 2001 at 22:42 UTC ( [id://109487]=perlquestion: print w/replies, xml ) Need Help??

basicdez has asked for the wisdom of the Perl Monks concerning the following question:

Could anyone help me out with creating the following XML::File Creation with the following format...
<?xml version = "1.0"?> <Application> <Customer> <Name> "Fred Flintsone" </Name> <Address> "111 Rock River Lane" </Address> </Customer> <Customer> <Name> "Barney Rubble" </Name> <Address> "113 Rock River Lane" </Address> </Customer> </Application>
As near as I can tell I could do the following in Perl to create this tree...
# !usr/bin/perl -w use strict; use XML::Writer; use IO; my output = new IO::File(">xxx.xml"); @Name = "Fred Flintsone", "Barney Rubble"; @Address = "111 Rock River Lane", "113 Rock River Lane"; my $writer = new XML::Writer(OUTPUT => $output,NEWLINES =>1); $writer->xmlDecl("UTF-8","1"); $writer->startTag("Application", $writer->startTag("Customer"), $writer->startTag("Name"); $writer->characters($Name[0]); $writer->endTag("Name"); $writer->startTag("Address"); $writer->characters($Address[0]); $writer->endTag("Name"); $writer->startTag("Name"); $writer->characters($Name[1]); $writer->endTag("Name"); $writer->startTag("Address"); $writer->characters($Address[1]); $writer->endTag("Address"); $writer->endTag("Customer"); $writer->endTag("Application"); $writer->end(); $output->close();
Please tell me if there is a more efficient way of completing this task... thanks. dez L

Replies are listed 'Best First'.
Re: Help with XML::Writer
by OeufMayo (Curate) on Sep 01, 2001 at 04:09 UTC

    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...

    <kbd>--
    my $OeufMayo = new PerlMonger::Paris({http => 'paris.mongueurs.net'});</kbd>
      Can you please tell me how to redirect the output to a file named xxx.xml without using the IO output redirection?
Answer: Help with XML::Writer
by mirod (Canon) on Aug 31, 2001 at 22:56 UTC

    You can use $writer->dataElement( 'Name', $Name[0]); to generate <Name>Fred Flintsone</Name> and replace 3 lines in your code. This would shorten it and make it a lot more clear.

    I tend to dislike using the IO::File thing and write to STDOUT instead (just don't use the OUTPUT argument and redirect the output of your script) but maybe that's just me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://109487]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-18 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found