Use Xpath to simplify your life! As you haven't posted the full xml file you are after, I assume that it should look like :
<root> <predicted_properties> <property> <kind>water_solubility</kind> <value>0.00169 mg/mL at 25 C</value> <source/> </property> </predicted_properties> <predicted_properties> <property> <kind>formal_charge</kind> <value>-1</value> <source>ChemAxon</source> </property> </predicted_properties> </root>

The code looks for the 'kind' node and then for its sibling node called 'value'

use XML::Twig::XPath; my $twig= XML::Twig::XPath->new(); $twig->parsefile('myfile.xml'); print $twig->findvalue ('/root/predicted_properties/property/kind[text()="formal_charge"]/ following-sibling::value');

prints -1

One suggestion to simplify your xml structure would be to add it as an attribute to kind. ie :

<kind name='formal_charge'>-1</kind>


In reply to Re: Getting the value from XML file by nikosv
in thread Getting the value from XML file by hellohello1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.