SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2000
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML::Twig - Getting a value?

    I am trying to get the rate. I actually need the MailService and Rate with the possibility of having more than one package. I get nothing. I can't get text from anything. I can print out hash refs, but that's it.

    What am I doing wrong?

    Code:
    my $xml_response =  XML::Twig->new();
    
    $xml_response->parse("<RateV3Response><Package ID=\"1ST\"><ZipOrigination>44106</ZipOrigination><ZipDestination>20770</ZipDestination><Pounds>0</Pounds><Ounces>3.5</Ounces><FirstClassMailType>LETTER</FirstClassMailType><Size/><Machinable>TRUE</Machinable><Zone>3</Zone><Postage CLASSID=\"0\"><MailService>First-Class Mail</MailService><Rate>0.93</Rate></Postage></Package></RateV3Response>") ;
    my $root = $xml_response->root;
    
    
    print $root->first_child('Package')->first_child_text('Rate');

  2. #2
    SitePoint Member
    Join Date
    May 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Rate element is not a direct child of Package, but rather a child of Postage:
    Code:
    #!/usr/bin/perl -l
    
    use strict;
    use warnings;
     
    use XML::Twig;
    
    my $xml_response =  XML::Twig->new();
    
    $xml_response->parse(
    q{<RateV3Response>
        <Package ID="1ST">
          <ZipOrigination>44106</ZipOrigination>
          <ZipDestination>20770</ZipDestination>
          <Pounds>0</Pounds>
          <Ounces>3.5</Ounces>
          <FirstClassMailType>LETTER</FirstClassMailType>
          <Size/>
          <Machinable>TRUE</Machinable>
          <Zone>3</Zone>
          <Postage CLASSID="0">
            <MailService>First-Class Mail</MailService>
            <Rate>0.93</Rate>
          </Postage>
        </Package>
      </RateV3Response>}) ;
    my $root = $xml_response->root;
    
    print $root->first_child('Package')->first_child( 'Postage')->field('Rate');


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •