Re: generating attributes and content for an element
Michael Ludwig <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Manuel Souto Pico schrieb:
> I'm starting to learn XML with Perl (and I'm not an experienced Perl
> programmer either), so I'd be thankful if you could be patient ;)
Hi Manuel,
as you're saying you're a newbie, and in case you don't already know,
always start your programs with:
use strict;
use warnings;
That'll spare you a lot of trouble.
> The doubt that I have is about generating both content* and attributes
> for an element.
>
> (*): content which is not other elements, that is.
>
> By means of
>
> my $data = {
> e => ['element value'],
> a => 'attribute value'*
> *};
This is a syntax error, and incomplete. You're using XML::Simple, which
may become very complicated if what you want to do isn't simple enough.
> I can get this:
>
> <root a="attribute value">
> <e>element value</e>
> </root>
>
> However, what I'm after is something like
>
> <root>
> <e a="attribute value">element value</e>
> </root>
Take a look at the documentation and look for "ContentKey". XML::Simple
uses nested data structures in Perl to represent XML, which falls short
of representing XML. Hence one of several workarounds, to define a magic
key "content" (or whatever you choose to name it using "ContentKey"),
which allows you to add text content.
See: perldoc XML::Simple
milu@colinux:~ > cat /tmp/simple.pl
use strict;
use warnings;
use XML::Simple;
my $data = {
Salat => {
content => 'Gurken',
mit => 'Tomaten',
}
};
print XMLout( $data, KeepRoot => 1);
milu@colinux:~ > perl /tmp/simple.pl
<Salat mit="Tomaten">Gurken</Salat>
I hope this helps.
Michael Ludwig
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs