I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2006-09-01 14:14:21-07 by aloknath251
reading and writing into xml
Hi, I have this xml which I want to process.Basically I want to do this two operations 1. Print the students name for a given class. 2. Insert a student in a given class. It just became complicated after I added few levels. Can anybody guide me please ? Thanx, Alok.
#!/usr/bin/perl -w use strict ; use XML::Twig; my $twig = XML::Twig->new(pretty_print => 'indented'); $twig->parsefile('testTwig.xml'); my $root = $twig->root ; my $className ; # display students of a class foreach my $element ($twig->root->children('classes')) { $className = $element->{'att'}->{"name"} ; #print the students of 'primary' class if ($className eq "Primary") { print "students for this class : \n"; } } # insert student in a class my $node = XML::Twig::Elt->new( 'User', {'name' => "Johny"}, XML::Twig::Elt->new( 'age' => 25 )) ; ??? Where to paste this $twig->print;
contents of testTwig.xml
< school >

< classes name="Primary" >
< student name="Junkman" >
< Age > 12 < /Age >
< /student >

< student name="Lotman" >
< Age > 14 < /Age >
< /student >
< /classes >

< classes name="Nursery" >
< student name="Testman" >
< Age > 34 < /Age >
< /student >
< /classes >

< classes name="SomeClass" >
< /classes >
< /school >
Direct Responses: 2889 | Write a response
Posted on 2006-09-01 14:44:01-07 by mirod in response to 2885
Re: reading and writing into xml

You want to paste the element at the end of the proper classes element.

It can be a bit of a pain to write because of the interaction between the quotes in Perl and in XPath (and the fact that @ means something different in both languages), so here is a way to do it:

my $class_name= "Nursery"; my $class= $root->first_child( qq{classes[\@name="$class_name"]}); $node->paste( last_child => $class);

Just in case you don't know this: qq{...} is an alternate way of writing "...", and the @ needs to be backslashed or Perl sees it as an array to be interpolated in the string.

A couple of comments: classes seems weird as an element name, as it looks like it contains only 1 class, so why the plural? Instead of $className = $element->{'att'}-*gt;{"name"}; you should really write $className = $element->att( "name"); . This is the "official" way to access attributes, and even though what you wrote works today there is nothing in the published API for XML::Twig that garantees that it will work in the future (ie I could possibly change the implementation of attributes).

Does this help?

Direct Responses: 2913 | 2914 | Write a response
Posted on 2006-09-05 11:28:33-07 by aloknath251 in response to 2889
Re: reading and writing into xml
Hi Mirod, Thanks for the response. But my question was inserting some students inside a specific class element and printing the students specific to a class.I think the above code just inserts a new class. I am sorry if I have not explained my problem properly. Can you just go thru the thread once again ? Thanks, Alok.
Direct Responses: 2921 | Write a response
Posted on 2006-09-05 11:30:02-07 by aloknath251 in response to 2889
Re: reading and writing into xml
Hi Mirod, Thanks for the response. But my question was inserting some students inside a specific class element and printing the students specific to a class.I think the above code just inserts a new class. I am sorry if I have not explained my problem properly. Can you just go thru the thread once again ? Thanks, Alok.
Direct Responses: Write a response
Posted on 2006-09-05 15:33:47-07 by mirod in response to 2913
Re: reading and writing into xml

Hi,

Did you try using the code I gave you? If you replace the "??? where to paste this" line in your original code with the 3 lines I gave you, it will insert the new User element in the Classes element with a name attribute of Nursery (as the last element in the parent).

Isn't that what you are trying to do?

--
mirod

Direct Responses: 3018 | Write a response
Posted on 2006-09-13 15:51:10-07 by aloknath251 in response to 2921
Re: reading and writing into xml
Thanks Mirod for the solution. Its helping...-:)
Direct Responses: 3487 | Write a response
Posted on 2006-11-11 01:51:34-08 by jenda in response to 3018
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 } );
Direct Responses: Write a response