Richard Lee wrote:
> Richard Lee wrote:
>>> I think I made a mistake .. this is now working...
>>>
>>> <yahoo 
>>> V="baz"><bay_id><value>1000</value><fact>yes</fact></bay_id><bay_seen><value>50</value><fact>no</fact></bay_seen><bay_overall
>>>  
>>> value="disabled"/><bayking_list><bayking active="true" 
>>> country="Russia" id="kingtony"><bayking type="dictator"/><bay_usage 
>>> value="none"/><bayking_origin><bayking_origin_name emmigrate="no" 
>>> value="ohio_usa"><economy_status_previous value="very 
>>> poor"/></bayking_origin_name></bayking_origin></bayking></bayking_list><bayqueen_list><bayqueen
>>>  
>>> active="true" country="japan" id="queensarah"><bayqueen 
>>> type="dictator"/><bay_usage 
>>> value="none"/><bayqueen_origin><bayqueen_origin_name emmigrate="no" 
>>> value="ca_usa"/><economy_status_previous value="very poor"/><previous 
>>> marriage="no"/></bayqueen_origin></bayqueen></bayqueen_list></yahoo>
>>>
>>> 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
>> <yahoo V="baz">.....  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/


Reply via email to