XML-Twig - Re: reading and writing into xml

Posted on Sat Nov 11 03:51:34 2006 by jenda in response to 3018 (See the whole thread of 7)
Re: reading and writing into xml

Just in case anyone was interested, the first task would using XML::Rules look like this:

my $parser = new XML::Rules ( rules => [ _default => '', # not interested in most tags '^classes' => sub {return ($_[1]->{name} eq "Primary")}, # skip all classes whose names are not Primary. Including subtags. student => sub {print $_[1]->{name}."\n";} ]); $parser->parsestring($xml);

and the other, using a over the weekend to be released version, would look like this

my $parser = new XML::Rules ( rules => [ _default => 'raw', classes => sub { my ($tag, $attrs, $context, $parents, $parser) = @_; my $add = $parser->{parameters}{$attrs->{name}}; if ($add) { if (ref($attrs->{_content})) { push @{$attrs->{_content}}, @$add } else { $attrs->{_content} = [ $attrs->{_content}, @$add]; # there were no students in the class, the tag contained only the whitespace } } return $tag => $attrs; # the module will print the branch }, ], style => 'filter', # the default is : style => 'parser' ); $parser->filterstring($xml => $FILEHANDLE, { SomeClass => [ [student => {name => 'Johny', age => {_content => 31}}], "\n", ], Nursery => [ [student => {name => 'Paul', age => {_content => 36}}], "\n", [student => {name => 'Martin', age => {_content => 33}}], "\n", ], # the newlines are not required } );
Write a response