Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Converting a data structure to XML

by Tanalis (Curate)
on Jan 14, 2004 at 15:50 UTC ( [id://321278]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

I have a data structure (an arbitrary array of arrays containing data rows downloaded from a database) that I'd like to convert to XML in some simple form.

I've written a conceptual XML outline that I'd like to make use of, basically as follows:

<dataset> <row> <column1-name>value</column1-name> <column2-name>value</column2-name> <column3-name>value</column3-name> </row> <row> ... </row> </dataset>
but I'm unsure where to go from here. Is there a simple XML module that can assist with the creation of such an XML document, or am I better off hand-rolling the XML?

Any suggestions would be appreciated.

Thanks in advance.

Replies are listed 'Best First'.
Re: Converting a data structure to XML
by broquaint (Abbot) on Jan 14, 2004 at 16:19 UTC
    Is there a simple XML module that can assist with the creation of such an XML document
    Such as XML::Simple?
    use XML::Simple; my $data = { dataset => { row => { column1 => ["value"], column2 => ["value"], column3 => ["value"] }, }, }; print XMLout($data, KeepRoot => 1); __output__ <dataset> <row> <column1>value</column1> <column2>value</column2> <column3>value</column3> </row> </dataset>
    HTH

    _________
    broquaint

Re: Converting a data structure to XML
by jeffa (Bishop) on Jan 15, 2004 at 00:25 UTC
Re: Converting a data structure to XML
by Art_XIV (Hermit) on Jan 14, 2004 at 16:09 UTC

    XML::Simple may be able to do the trick. It can convert most records structures into pretty respectable XML, although you must keep an eye on your output to make sure that it's doing what you really want.

    Update: It would be deliciously subversive to create ADO.NET/xml datasets w/Perl.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: Converting a data structure to XML
by borisz (Canon) on Jan 14, 2004 at 16:25 UTC
    Look at XML::Writer it is easy to use and can do what you want.
    Boris
Re: Converting a data structure to XML
by boo_radley (Parson) on Jan 14, 2004 at 16:43 UTC
    You are in a monastery. Some monks are meditating quietly in the afternoon sun, while others are going about their daily tasks. One smiling brother stops his sweeping and asks you, "Can I help you?"
    > Ask about dumping data structures into XML
    The monk leads you into the library. This large building holds more books than you've seen before in your life. Thousands of researchers, monks and petitioners are reading quietly in the many sections of the library.
    He leads you to a section of the library, seemingly at random, and pauses for a moment. "What was it you were looking for again?"
    > Ask about dumping data structures into XML
    The monk says "Not so loud!"
    > Whisper about dumping data structures into XML
    The monk smiles and pulls a book of the shelf and hands it to you. The book is titled XML::Dumper.
    > _
      XML::Dumper is all well and good if I needed a fairly standard XML output that I can read back in (it outputs structures in a slurpable fashion, the way I understand it's POD).

      I need the XML output in a specific fashion, so I'm not sure how much use it would be.

      Thanks for the story, though.

        You shelve the book and walk out of the library, and back into the monastery grounds.
        > save and quit
        Saved.
        You had 183 items in your pack. You died 0 times.
        Your Score : 3027
        Your Rank : Saint
Re: Converting a data structure to XML
by stvn (Monsignor) on Jan 14, 2004 at 16:22 UTC

    I find that rolling your own is the best way to create XML, especially when you have simple, consistent and well understood data structures like the above. Its much faster and simpler than using any parser. To then parse that XML, or alter it after its initial creation, use a parser.

    -stvn

      By all means, if your needs are simple, this is workable. When you get to more complicated things, though, where encoding, namespaces, and character sets come into play, it can be more work to make a simple printer work than it is to use a module that's already solved those problems.

      Update: I don't think printing XML is stupid. It just has a nasty tendency to become very complicated. :)

        Quite true.

        I was not aware of XML::Simple, and after seeing the example by broquaint, I withdraw my original comment. Use that (if it meets your needs of course).

        I find I spend much less time writing my XML documents, and alot more time parsing, manipulating, transforming, etc etc etc. And 9 times out of 10 I end up writing quick one-off scripts to build that initial XML document.

        But, nuff said, I concede the stupidity of my initial remark.

        -stvn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-03-28 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found