Re: xml question for xml::twig

2008-11-19 Thread Rob Dixon
Richard Lee wrote:
> Richard Lee wrote:
>>> I think I made a mistake .. this is now working...
>>>
>>> >> V="baz">1000yes50no>>  
>>> value="disabled"/>>> country="Russia" id="kingtony">>> value="none"/>>> value="ohio_usa">>>  
>>> active="true" country="japan" id="queensarah">>> type="dictator"/>>> value="none"/>>> value="ca_usa"/>>> marriage="no"/>
>>>
>>> Now, I just need good way to put this into hash of hash referernce.
>>
>> ok so I gave up putting them into has of hash reference because of 
>> unpredictableness of items..
>> I can get to all values by doing manual $_->first_child method... but 
>> my problem is, I don't know how to extract all information on
>> .  how do I get that?  $_->parent?
>
> After I extracted above xml, what is the proper way to loop through them 
> and extract all information?
> I like my final output to look like,
> 
> yahoo V: baz
> bay_id value: 1000 fact: yes
> bay_seen value: 50 fact: no
> .. and so on

This code may help you.

my $t = XML::Twig->new(
  twig_handlers => {
'/foo/yahoo' => sub {

  printf "%s %s:%s\n", $_->tag, 'V', $_->att('V');

  foreach my $elem ($_->children) {

next if $elem->tag eq 'bayqueen_list';

print $elem->tag;
foreach my $subelem ($elem->children) {
  printf " %s: %s", $subelem->tag, $subelem->text;
}
print "\n";
  }
}
  }
);


But you need to decide what you are trying to do. A generalized program to dump
all the text and attribute values from an arbitrary piece of XML data has to be
recursive and is probably not what you want.

First you talked about putting the data into a hash, and now you say you want to
print it. What is your ultimate goal?

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-16 Thread Richard Lee

Richard Lee wrote:


I think I made a mistake .. this is now working...

V="baz">1000yes50novalue="disabled"/>country="Russia" id="kingtony">value="none"/>value="ohio_usa">active="true" country="japan" id="queensarah">type="dictator"/>value="none"/>value="ca_usa"/>marriage="no"/>


Now, I just need good way to put this into hash of hash referernce.
ok so I gave up putting them into has of hash reference because of 
unpredictableness of items..
I can get to all values by doing manual $_->first_child method... but 
my problem is, I don't know how to extract all information on

.  how do I get that?  $_->parent?
After I extracted above xml, what is the proper way to loop through them 
and extract all information?

I like my final output to look like,

yahoo V: baz
bay_id value: 1000 fact: yes
bay_seen value: 50 fact: no
.. and so on



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-16 Thread Richard Lee

Richard Lee wrote:

Richard Lee wrote:

Chas. Owens wrote:

my $sabal = new XML::Twig(
 twig_roots  => {
'foo/yahoo'  =>
#'[EMAIL PROTECTED]"kingtony"]'   =>
sub {
   my ($yabal, $element ) = @_;
   if ( 
$element->first_child('bayking_list')->first_child('bayking')->att('id') 
eq 'kingtony' ) {

  $element->print;
}
 }
 }
);

I think I made a mistake .. this is now working...

V="baz">1000yes50novalue="disabled"/>country="Russia" id="kingtony">value="none"/>value="ohio_usa">active="true" country="japan" id="queensarah">type="dictator"/>value="none"/>value="ca_usa"/>marriage="no"/>


Now, I just need good way to put this into hash of hash referernce.
ok so I gave up putting them into has of hash reference because of 
unpredictableness of items..
I can get to all values by doing manual $_->first_child method... but my 
problem is, I don't know how to extract all information on

.  how do I get that?  $_->parent?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Richard Lee wrote:

Chas. Owens wrote:

my $sabal = new XML::Twig(
 twig_roots  => {
'foo/yahoo'  =>
#'[EMAIL PROTECTED]"kingtony"]'   =>
sub {
   my ($yabal, $element ) = @_;
   if ( 
$element->first_child('bayking_list')->first_child('bayking')->att('id') 
eq 'kingtony' ) {

  $element->print;
}
 }
 }
);

I think I made a mistake .. this is now working...

V="baz">1000yes50novalue="disabled"/>id="kingtony">value="none"/>value="ohio_usa">active="true" country="japan" id="queensarah">type="dictator"/>value="none"/>value="ca_usa"/>marriage="no"/>


Now, I just need good way to put this into hash of hash referernce.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Chas. Owens wrote:




Perhaps I am dense, but what is the desired output from the given XML?

  

Hello Chas,

From xml file, based on attribute value for bayking id, I want to find 
kingtony and then I want to traverse back up to yahoo and print 
everything from

 to 

I have tried to use xpath //../../bayking but doesn't seem to work.

I am trying to ultimately push below find value into hash of hash 
reference such as


  $list{'yahoo'} = { 'V' => 'baz'}

   
$list{'bayking'} = { 'id' => 'kingtony' , 'country' => 'Russia', 
'active' => 'true' }


I am able to find what I am looking for 'kingtony' but having tough time 
traversing back to right yahoo element and accessing all data.





1000
yes


50
no







  emmigrate="no">

 
  




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-15 Thread Chas. Owens
On Sat, Nov 15, 2008 at 23:18, Richard Lee <[EMAIL PROTECTED]> wrote:
> Richard Lee wrote:
>>
>> Chas. Owens wrote:
>>>
>>> On Sat, Nov 15, 2008 at 16:27, Richard Lee <[EMAIL PROTECTED]> wrote:
>>> snip
>>>

   if ( $bay1->att('id' eq 'kingtony' ) ) {

>>>
>>> snip
>>>
>>> I think you mean to say
>>>
>>> if ($bay1->att("id") eq "kingtony") {
>>>
>>>
>>
>> yes, that was a typo...
>
> I changed to
>
> my $sabal = new XML::Twig(
> twig_roots  => {
>'foo/yahoo'  =>
>#'[EMAIL PROTECTED]"kingtony"]'   =>
>sub {
>my ($yabal, $element ) = @_;
>if (
> $yabal->first_child('bayking_list')->first_child('bayking')->att('id') eq
> 'kingtony' ) {
>$element->print;
>}
> }
> }
> );
>
> $sabal->parse($xml);
>
> but still no luck..
>


Perhaps I am dense, but what is the desired output from the given XML?

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Richard Lee wrote:

Chas. Owens wrote:

On Sat, Nov 15, 2008 at 16:27, Richard Lee <[EMAIL PROTECTED]> wrote:
snip
 

   if ( $bay1->att('id' eq 'kingtony' ) ) {


snip

I think you mean to say

if ($bay1->att("id") eq "kingtony") {

  

yes, that was a typo...

I changed to

my $sabal = new XML::Twig(
 twig_roots  => {
'foo/yahoo'  =>
#'[EMAIL PROTECTED]"kingtony"]'   =>
sub {
my ($yabal, $element ) = @_;
if ( 
$yabal->first_child('bayking_list')->first_child('bayking')->att('id') 
eq 'kingtony' ) {

$element->print;
}
 }
 }
);

$sabal->parse($xml);

but still no luck..

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee

Chas. Owens wrote:

On Sat, Nov 15, 2008 at 16:27, Richard Lee <[EMAIL PROTECTED]> wrote:
snip
  

   if ( $bay1->att('id' eq 'kingtony' ) ) {


snip

I think you mean to say

if ($bay1->att("id") eq "kingtony") {

  

yes, that was a typo...

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-15 Thread Chas. Owens
On Sat, Nov 15, 2008 at 16:27, Richard Lee <[EMAIL PROTECTED]> wrote:
snip
>if ( $bay1->att('id' eq 'kingtony' ) ) {
snip

I think you mean to say

if ($bay1->att("id") eq "kingtony") {

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: xml question for xml::twig

2008-11-15 Thread Richard Lee



use strict;
use warnings;

use XML::Twig;

my $xml = <
 
 
 1
 yes
 
 
 10
 no
 
 
 
 
 
 
 
   emmigrate="no">

  
   
 
 
 
 
 
 
 
 
   emmigrate="no"/>

 
 
 
 
 
 
 1000
 yes
 
 
 50
 no
 
 
 
 
 
 
 
   emmigrate="no">

  
   
 
 
 
 
 
 
 
 
   emmigrate="no"/>

  
  
 
 
 
 

XML

my $sabal = new XML::Twig(
 twig_roots  => {
'[EMAIL PROTECTED]"kingtony"]'   =>
sub {
my ($yabal, $element ) = @_;
 $element->print;
 }
 }
);

above produces,

type="dictator"/>value="none"/>value="ohio_usa">


which is very close to what i need but I really need it to traverse all 
the way top to  and traverse down back all the way to ..


I thought sub'ing $element->print to //../../$element->print would work 
but does not


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




xml question for xml::twig

2008-11-15 Thread Richard Lee


Hello, I am praticing below XML file.
Based on where I find att id for bayking id 'kingtony' , I wanted to 
print out the entire element/att(and ID) and any text found from

 to .(exception of bayqueen_list and its descendatns)...

I am having problem just print out one value... can someone take a look 
please?


use strict;
use warnings;

use XML::Twig;

my $xml = <
 
 
 1
 yes
 
 
 10
 no
 
 
 
 
 
 
 
   emmigrate="no">

  
   
 
 
 
 
 
 
 
 
   emmigrate="no"/>

 
 
 
 
 
 
 1000
 yes
 
 
 50
 no
 
 
 
 
 
 
 
   emmigrate="no">

  
   
 
 
 
 
 
 
 
 
   emmigrate="no"/>

  
  
 
 
 
 

XML

my $t = XML::Twig->new
(
 twig_handlers => { '/foo/yahoo/bayking_list' => sub
 {
my @bay = $_->children('bayking');
foreach my $bay1 ( @bay ) {
if ( $bay1->att('id' eq 'kingtony' ) ) {
  # print all elements/att/value and text from  to 
 which contains kingtony but do NOT print out bayqueen

print $bay1->att('id') . "\n";
}
}
  }
  }
);

$t->parse ($xml);

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/