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>
| [reply] [Watch: Dir/Any] [d/l] |
Re: Converting a data structure to XML
by jeffa (Bishop) on Jan 15, 2004 at 00:25 UTC
|
| [reply] [Watch: Dir/Any] |
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"
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |
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.
> _
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] |
|
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
| [reply] [Watch: Dir/Any] |
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
| [reply] [Watch: Dir/Any] |
|
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. :)
| [reply] [Watch: Dir/Any] |
|
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
| [reply] [Watch: Dir/Any] |