[H-GEN] How do I reference this in perl?

Byron Ellacott bje at apnic.net
Sun Jul 7 20:33:13 EDT 2002


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]

On Sat, 2002-07-06 at 21:21, Michael Anthon wrote:
> I'm attempting to do something in perl that is *way* beyond my abilities.
> The basic thrust of what I am attempting is to convert an XML document from
> glade into another XML document suitable for XWT.

It may be appropriate[1] to use XSLT to do the transformation for you,
in which case your perl code would look roughly[2] like:

---- snip ----
#!/usr/bin/perl -w

use XML::XSLT;

my $source = shift;
my $xslt = shift;

my $filter = new XML::XSLT(Source => $xslt);

print $filter->serve(Source => $source, http_headers => 0);

1;
---- snip ----

Of course, you're left needing to write the XSLT.

If that's not your cup of tea, you might want to think about whether you
Really Need to use Style => Object.  From what I can tell, this just
blesses the hash returned into a package, which doesn't make it easier
for you to access the components:

print $VAR1->[0]->{Kids}->[1]->{Kids}->[3]->{Kids}->[0]->{Text}, "\n";

The above prints[3] "src" which is the first child of the base
document's second child's fourth child's first child's Text element.

You can "walk" the tree:

---- snip ----
sub walk {
     my ($tree, $indent) = @_;
     my $type = ref($tree);      # returns the type of this node

     print "$indent$type\n";
     foreach my $key (keys %{$tree}) {
         print "  $indent$key = '", $tree->{$key}, "'\n" if ($key ne "Kids");
     }

     foreach my $kid (@{$tree->{'Kids'}}) {
         walk($kid, "$indent    ");
     }
}

walk($VAR1->[0], "");
---- snip ----

Which produces output[4]:

---- snip ----
                    Glade::child_name
                        Glade::Characters
                          Text = 'Toolbar:button'
---- snip ----

That might answer your question of how to reference the children, but it
might raise new questions about what you do next :)

-- 
bje

[1] By appropriate, I mean that it may be a simple transform.  If it's
more complex, XSLT can probably still do it, but it may get ugly.  I
don't know a hell of a lot about XSLT.
[2] Haven't even installed XML::XSLT, ymmv
[3] echo "print ..." >> object_dump.txt; perl object_dump.txt
[4] also verified to work with object_dump.txt

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list