An XML::Generator Question
An XML::Generator Question
am 19.12.2007 01:00:25 von Oliver Weichhold
In the example below, I've written a sub in which I output additional XML
tags for TAG2. So after the sub AddMoreTagsToTAG2(); is called, the
TAG2 block would have additional tags added, and appear something like
this:
print $xml->TAG2(
$xml->TAG2A("CCC"),
$xml->TAG2B("DDD"),
$xml->TAG2X("ANOTHER TAG"),
$xml->TAG2Y("ANOTHER TAG HERE 2"),
);
How can I call a sub which adds tags, and have them placed in the same XML
block as the previous block, and then continue printing $xml tags?
-Thanks
------------------------
print $xml->TAG1(
$xml->TAG1A("AAA"),
$xml->TAG1B("BBB"),
);
print $xml->TAG2(
$xml->TAG2A("CCC"),
$xml->TAG2B("DDD"),
);
AddMoreTagsToTAG2();
print $xml->TAG3(
$xml->TAG3A("EEE"),
$xml->TAG3B("FFF"),
);
Re: An XML::Generator Question
am 19.12.2007 02:30:07 von Ben Morrow
Quoth nospam :
> In the example below, I've written a sub in which I output additional XML
> tags for TAG2. So after the sub AddMoreTagsToTAG2(); is called, the
> TAG2 block would have additional tags added, and appear something like
> this:
>
>
> print $xml->TAG2(
> $xml->TAG2A("CCC"),
> $xml->TAG2B("DDD"),
> $xml->TAG2X("ANOTHER TAG"),
> $xml->TAG2Y("ANOTHER TAG HERE 2"),
> );
>
>
> How can I call a sub which adds tags, and have them placed in the same XML
> block as the previous block, and then continue printing $xml tags?
>
> print $xml->TAG2(
> $xml->TAG2A("CCC"),
> $xml->TAG2B("DDD"),
> );
>
>
> AddMoreTagsToTAG2();
You can't: you've already printed TAG2, including the closing ,
so unless you're writing to a seekable file and you back up and
overwrite that (a bad idea) you can't undo it.
Instead write MoreTagsForTAG2, called like this
print $xml->TAG2(
$xml->TAG2A(...),
...
MoreTagsForTAG2(...),
);
or otherwise restructure your program so you don't need to do this. If
you want to perform arbitrary manipulations on the document before
printing it you may want to look at one of the DOM modules or
XML::Simple instead.
Ben
Re: An XML::Generator Question
am 19.12.2007 04:04:52 von Oliver Weichhold
On Wed, 19 Dec 2007 01:30:07 +0000, Ben Morrow wrote:
> Instead write MoreTagsForTAG2, called like this
>
> print $xml->TAG2(
> $xml->TAG2A(...),
> ...
> MoreTagsForTAG2(...),
> );
>
> or otherwise restructure your program so you don't need to do this. If
> you want to perform arbitrary manipulations on the document before
> printing it you may want to look at one of the DOM modules or
> XML::Simple instead.
>
> Ben
Thanks. That allows me at least to call MoreTagsForTAG2(). But I'm still
creating another TAG2 block in the MoreTagsForTAG2() sub like this:
print $xml->TAG2(
$xml->MORE_TAGS('AAA')),
$xml->EVEN_MORE_TAGS('BBB'),
),
I'm trying to have only one TAG2 block, and add more to it from by calling
the MoreTagsForTAG2() sub routine.
-Thanks again.
Re: An XML::Generator Question
am 19.12.2007 05:16:59 von Ben Morrow
Quoth nospam :
> On Wed, 19 Dec 2007 01:30:07 +0000, Ben Morrow wrote:
> > Instead write MoreTagsForTAG2, called like this
> >
> > print $xml->TAG2(
> > $xml->TAG2A(...),
> > ...
> > MoreTagsForTAG2(...),
> > );
> >
> > or otherwise restructure your program so you don't need to do this. If
> > you want to perform arbitrary manipulations on the document before
> > printing it you may want to look at one of the DOM modules or
> > XML::Simple instead.
>
> Thanks. That allows me at least to call MoreTagsForTAG2(). But I'm still
> creating another TAG2 block in the MoreTagsForTAG2() sub like this:
>
> print $xml->TAG2(
> $xml->MORE_TAGS('AAA')),
> $xml->EVEN_MORE_TAGS('BBB'),
> ),
>
>
> I'm trying to have only one TAG2 block, and add more to it from by calling
> the MoreTagsForTAG2() sub routine.
Don't do that then. I think you need to learn a bit more basic Perl
before you start trying relatively complicated things like generating
XML.
You need to return a list of elements from MoreTagsForTAG2, like this:
sub MoreTagsForTAG2 {
return
$XML->MORE_TAGS(...),
$XML->EVEN_MORE(...);
}
This list will then be interpolated into the list passed to ->TAG2,
exactly as though you had specified it there literally.
Ben