XML::Twig Doubt
Hi List, I have a XML file which looks like as follows 10 1,3,4,7 12,16,21 2,3,3 1,3,6,8 12,17,25 50 AMI_BRANLY_B_1 . . Now my question is how to extract the value of id in a variable using XML::Twig. Since the xml file is quite large I like to print the value of id using a loop. Best Regards Anirban Adhikary.
Re: XML::Twig Doubt
On 19/03/2012 13:10, Anirban Adhikary wrote: Hi List, I have a XML file which looks like as follows 10 1,3,4,7 12,16,21 2,3,3 1,3,6,8 12,17,25 50 AMI_BRANLY_B_1 . . Now my question is how to extract the value of id in a variable using XML::Twig. Since the xml file is quite large I like to print the value of id using a loop. XML::Twig uses callbacks to process pieces of the XML that you have defined. In this case you are interested only in the start tag so you can define a "start tag handler". Using $twig->purge empties the data read so far. If you use this once you have accessed all the information you need from a given element there is no need to store the entire XML data in memory. The program below doeas what I think you want. HTH, Rob use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(start_tag_handlers => { BSC => \&on_BSC }); sub on_BSC { my($twig, $bsc)= @_; print $bsc->id, "\n"; $twig->purge; } $twig->parsefile('ISProducts.xml'); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML::Twig Doubt
Hi Rob, Thanks for your support.The code does exactly what I want . Can you please suggest me a tutorials with good examples on XML::Twig. Another thing can you please explain me this line in the code my($twig, $bsc)= @_; Best Regards Anirban Adhikary. On Mon, Mar 19, 2012 at 7:41 PM, Rob Dixon wrote: > On 19/03/2012 13:10, Anirban Adhikary wrote: > >> Hi List, >> I have a XML file which looks like as follows >> >> >> >> >> 10 >> 1,3,4,7 >> 12,16,21 >> 2,3,3 >> 1,3,6,8 >> 12,17,25 >> 50 >> AMI_BRANLY_B_1 >> >> . >> . >> >> Now my question is how to extract the value of id in a variable using >> XML::Twig. >> Since the xml file is quite large I like to print the value of id using a >> loop. >> > > XML::Twig uses callbacks to process pieces of the XML that you have > defined. In this case you are interested only in the start tag so > you can define a "start tag handler". Using $twig->purge empties the > data read so far. If you use this once you have accessed all the > information you need from a given element there is no need to store the > entire XML data in memory. > > The program below doeas what I think you want. > > HTH, > > Rob > > > use strict; > use warnings; > > use XML::Twig; > > my $twig = XML::Twig->new(start_tag_**handlers => { >BSC => \&on_BSC > }); > > sub on_BSC { > my($twig, $bsc)= @_; > print $bsc->id, "\n"; > $twig->purge; > } > > $twig->parsefile('ISProducts.**xml'); > > > >
Re: XML::Twig Doubt
Hi, When I am trying to print the value against the tag ALPHA it is not prints anything,though it is not showing any warnings. use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(start_tag_handlers => { BSC => \&on_BSC }); sub on_BSC { my($twig, $bsc)= @_; print $bsc->id, "\n"; my $alpha = $bsc->field('ALPHA'); print $alpha, "\n"; $twig->purge; } $twig->parsefile(ISProducts.xml'); On Mon, Mar 19, 2012 at 7:54 PM, Anirban Adhikary < anirban.adhik...@gmail.com> wrote: > Hi Rob, > Thanks for your support.The code does exactly what I want . > Can you please suggest me a tutorials with good examples on XML::Twig. > > Another thing can you please explain me this line in the code my($twig, > $bsc)= @_; > > Best Regards > Anirban Adhikary. > > > On Mon, Mar 19, 2012 at 7:41 PM, Rob Dixon wrote: > >> On 19/03/2012 13:10, Anirban Adhikary wrote: >> >>> Hi List, >>> I have a XML file which looks like as follows >>> >>> >>> >>> >>> 10 >>> 1,3,4,7 >>> 12,16,21 >>> 2,3,3 >>> 1,3,6,8 >>> 12,17,25 >>> 50 >>> AMI_BRANLY_B_1 >>> >>> . >>> . >>> >>> Now my question is how to extract the value of id in a variable using >>> XML::Twig. >>> Since the xml file is quite large I like to print the value of id using a >>> loop. >>> >> >> XML::Twig uses callbacks to process pieces of the XML that you have >> defined. In this case you are interested only in the start tag so >> you can define a "start tag handler". Using $twig->purge empties the >> data read so far. If you use this once you have accessed all the >> information you need from a given element there is no need to store the >> entire XML data in memory. >> >> The program below doeas what I think you want. >> >> HTH, >> >> Rob >> >> >> use strict; >> use warnings; >> >> use XML::Twig; >> >> my $twig = XML::Twig->new(start_tag_**handlers => { >>BSC => \&on_BSC >> }); >> >> sub on_BSC { >> my($twig, $bsc)= @_; >> print $bsc->id, "\n"; >> $twig->purge; >> } >> >> $twig->parsefile('ISProducts.**xml'); >> >> >> >> >
Re: XML::Twig Doubt
Hi List, When I have changed the method from my $twig = XML::Twig->new(start_tag_ handlers => { BSC => \&on_BSC }); to my $twig = XML::Twig->new(TwigHandlers => { BSC => \&on_BSC }); I am able to print the value against the ALPHA tag. Thanks to you for your support. Best Regards Anirban Adhikary. On Mon, Mar 19, 2012 at 8:15 PM, Anirban Adhikary < anirban.adhik...@gmail.com> wrote: > Hi, > When I am trying to print the value against the tag ALPHA it is not prints > anything,though it is not showing any warnings. > > > use strict; > use warnings; > > use XML::Twig; > > my $twig = XML::Twig->new(start_tag_handlers => { >BSC => \&on_BSC > }); > > sub on_BSC { > my($twig, $bsc)= @_; > print $bsc->id, "\n"; > my $alpha = $bsc->field('ALPHA'); > print $alpha, "\n"; > $twig->purge; > } > > $twig->parsefile(ISProducts.xml'); > > > On Mon, Mar 19, 2012 at 7:54 PM, Anirban Adhikary < > anirban.adhik...@gmail.com> wrote: > >> Hi Rob, >> Thanks for your support.The code does exactly what I want >> . >> Can you please suggest me a tutorials with good examples on XML::Twig. >> >> Another thing can you please explain me this line in the code my($twig, >> $bsc)= @_; >> >> Best Regards >> Anirban Adhikary. >> >> >> On Mon, Mar 19, 2012 at 7:41 PM, Rob Dixon wrote: >> >>> On 19/03/2012 13:10, Anirban Adhikary wrote: >>> Hi List, I have a XML file which looks like as follows 10 1,3,4,7 12,16,21 2,3,3 1,3,6,8 12,17,25 50 AMI_BRANLY_B_1 . . Now my question is how to extract the value of id in a variable using XML::Twig. Since the xml file is quite large I like to print the value of id using a loop. >>> >>> XML::Twig uses callbacks to process pieces of the XML that you have >>> defined. In this case you are interested only in the start tag so >>> you can define a "start tag handler". Using $twig->purge empties the >>> data read so far. If you use this once you have accessed all the >>> information you need from a given element there is no need to store the >>> entire XML data in memory. >>> >>> The program below doeas what I think you want. >>> >>> HTH, >>> >>> Rob >>> >>> >>> use strict; >>> use warnings; >>> >>> use XML::Twig; >>> >>> my $twig = XML::Twig->new(start_tag_**handlers => { >>>BSC => \&on_BSC >>> }); >>> >>> sub on_BSC { >>> my($twig, $bsc)= @_; >>> print $bsc->id, "\n"; >>> $twig->purge; >>> } >>> >>> $twig->parsefile('ISProducts.**xml'); >>> >>> >>> >>> >> >
Re: XML::Twig Doubt
On 19/03/2012 15:12, Anirban Adhikary wrote: Hi, When I am trying to print the value against the tag ALPHA it is not prints anything,though it is not showing any warnings. use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(start_tag_handlers => { BSC => \&on_BSC }); sub on_BSC { my($twig, $bsc)= @_; print $bsc->id, "\n"; my $alpha = $bsc->field('ALPHA'); print $alpha, "\n"; $twig->purge; } $twig->parsefile(ISProducts.xml'); Hi List, When I have changed the method from my $twig = XML::Twig->new(start_tag_ handlers => { BSC => \&on_BSC }); to my $twig = XML::Twig->new(TwigHandlers => { BSC => \&on_BSC }); I am able to print the value against the ALPHA tag. Thanks to you for your support. Hi Anirban I suggested using start_tag_handlers because what you wanted to do was extract the id attribute from the start tag of all elements, so there was no point in having any more of the element in store that just that start tag. It is also more efficient Changing to twig_handlers makes entirety of each "twig" (the element) available, so you are able to access the child elements as you have found. Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML::Twig Doubt
On 19/03/2012 14:45, Anirban Adhikary wrote: On Mon, Mar 19, 2012 at 7:41 PM, Rob Dixon wrote: XML::Twig uses callbacks to process pieces of the XML that you have defined. In this case you are interested only in the start tag so you can define a "start tag handler". Using $twig->purge empties the data read so far. If you use this once you have accessed all the information you need from a given element there is no need to store the entire XML data in memory. The program below doeas what I think you want. HTH, Rob use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(start_tag_**handlers => { BSC => \&on_BSC }); sub on_BSC { my($twig, $bsc)= @_; print $bsc->id, "\n"; $twig->purge; } $twig->parsefile('ISProducts.**xml'); Thanks for your support.The code does exactly what I want . Can you please suggest me a tutorials with good examples on XML::Twig. Another thing can you please explain me this line in the code my($twig, $bsc)= @_; When I am trying to print the value against the tag ALPHA it is not prints anything,though it is not showing any warnings. use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(start_tag_handlers => { BSC => \&on_BSC }); sub on_BSC { my($twig, $bsc)= @_; print $bsc->id, "\n"; my $alpha = $bsc->field('ALPHA'); print $alpha, "\n"; $twig->purge; } $twig->parsefile(ISProducts.xml'); Hi Anirban I use the POD documentation supplied with the module, but I agree it is a little opaque. XML::Twig has its own website at http://xmltwig.org where you will find some tutorials that may help you. The line my ($twig, $bsc)= @_; copies the values supplied as parameters to the callback into local variables. $twig is the XML::Twig object itself, and $bsc is an XML::Twig::Elt object representing the XML element that has been found. You are unable to access the child elements of each because I have suggested you specify "start_tag_handlers" for which only the start tag is available. That was fine when you just wanted the "id" attribute from the tag, but there is no information about any other part of the XML. You need to use "twig_handlers" instead, when the entire element will be accessible using $bsc->first_child_trimmed_text('ALPHA') HTH, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML::Twig Doubt
Thanks a lot Rob for your nice help. Best Regards Anirban Adhikary. On Tue, Mar 20, 2012 at 4:38 PM, Rob Dixon wrote: > On 19/03/2012 14:45, Anirban Adhikary wrote: > >> On Mon, Mar 19, 2012 at 7:41 PM, Rob Dixon wrote: >>> XML::Twig uses callbacks to process pieces of the XML that you have defined. In this case you are interested only in the start tag so you can define a "start tag handler". Using $twig->purge empties the data read so far. If you use this once you have accessed all the information you need from a given element there is no need to store the entire XML data in memory. The program below doeas what I think you want. HTH, Rob use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(start_tag_handlers => { BSC => \&on_BSC }); sub on_BSC { my($twig, $bsc)= @_; print $bsc->id, "\n"; $twig->purge; } $twig->parsefile('ISProducts.xml'); >>> >>> Thanks for your support.The code does exactly what I want >>> . >>> Can you please suggest me a tutorials with good examples on XML::Twig. >>> >>> Another thing can you please explain me this line in the code my($twig, >>> $bsc)= @_; >>> >>> >> When I am trying to print the value against the tag ALPHA it is not prints >> anything,though it is not showing any warnings. >> >> use strict; >> use warnings; >> >> use XML::Twig; >> >> my $twig = XML::Twig->new(start_tag_**handlers => { >>BSC => \&on_BSC >> }); >> >> sub on_BSC { >> my($twig, $bsc)= @_; >> print $bsc->id, "\n"; >> my $alpha = $bsc->field('ALPHA'); >> print $alpha, "\n"; >> $twig->purge; >> } >> >> $twig->parsefile(ISProducts.**xml'); >> > > Hi Anirban > > I use the POD documentation supplied with the module, but I agree it is > a little opaque. XML::Twig has its own website at http://xmltwig.org > where you will find some tutorials that may help you. > > The line > > my ($twig, $bsc)= @_; > > copies the values supplied as parameters to the callback into local > variables. $twig is the XML::Twig object itself, and $bsc is an > XML::Twig::Elt object representing the XML element that has been > found. > > You are unable to access the child elements of each because I have > suggested you specify "start_tag_handlers" for which only the start tag > is available. That was fine when you just wanted the "id" attribute from > the tag, but there is no information about any other part of the XML. > > You need to use "twig_handlers" instead, when the entire element > will be accessible using > > $bsc->first_child_trimmed_**text('ALPHA') > > HTH, > > Rob >