tags:

views:

261

answers:

1

I use a Perl script with the module XML:Simple to parse an XML feed:

my $xml=XMLin($resp->content);
my $xml2 = $xml->{params}{param}{value}{array}{data}{value}{struct}{member};


print $xml2->{prod_id}{value}{int}; #works fine

print $xml2->{product_text}{value}{string}; #returns a hash reference (hash(0x...)

What have I to modify to get the text:

<div class="text"> <div class="text_products">sdfsdf</div> </div>

instead of a hash reference?

This is the XML data:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <methodResponse>
- <params>
- <param>
- <value>
- <array>
- <data>
- <value>
- <struct>
- <member>
  <name>product_text</name> 
- <value>
  <string><![CDATA[ <div class="text"> <div class="text_products">sdfsdf</div> </div> ]]></string> 
  </value>
  </member>
- <member>
  <name>prod_id</name> 
- <value>
  <int>1290</int> 
  </value>
  </member>
- <member>.......
A: 

Your code appears to work for me. I copied your XML and your perl code and it prints:

1290 <div class="text"> <div class="text_products">sdfsdf</div> </div> 

Using Data::Dumper will show the data structure that exists. For example, when I run your sample code, an extract of the relevant section is:

'value' => {
  'string' => ' <div class="text"> <div class="text_products">sdfsdf</div> </div> '
}

The only reason I can think that you might be getting something different is if there's another element called product_text at the same level.

EDIT: If you are sometimes getting empty elements and they are coming out as a hashref, you can set the SuppressEmpty option to undef when you call XMLin. See the docs for more details ( link : http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm#SuppressEmpty_=>1|_''_|undef#_in+out_-_handy )

mopoke
I use my $xml=XMLin($resp->content);print Dumper($xml);to show all the xml data.How can I show the key value pairs with data dumper?
mypatek
Edited to explain a little more about Data::Dumper
mopoke
sorry my fault.. there was an error at the xml senders site.Thank you guys
mypatek
mopoke, you were absolutely correct. There was a configuration error of the xml server. element "product_text" has been submitted empty to the script with xml::simple. Another script I have used with Data Dumper received the data because the call was done with a different procedure (xml::rpc)
mypatek
Glad to be of help. I've edited the answer to give you hints on XML::Simple options which may let you deal with both cases more elegantly.
mopoke