Note that there are some explanatory texts on larger screens.

plurals
  1. POsimple XML question for perl - how to retrieve specific elements
    primarykey
    data
    text
    <p>I'm trying to figure out how to loop through XML but I've read a lot and I'm still getting stuck. Here's the info:</p> <p>I'm using the wordnik api to retrieve XML with XML::Simple:</p> <pre><code> $content = get($url); $r = $xml-&gt;XMLin("$content"); </code></pre> <p>The actual XML looks like this:</p> <pre><code>&lt;definitions&gt; − &lt;definition sequence="0" id="0"&gt; − &lt;text&gt; To withdraw one's support or help from, especially in spite of duty, allegiance, or responsibility; desert: abandon a friend in trouble. &lt;/text&gt; &lt;headword&gt;abandon&lt;/headword&gt; &lt;partOfSpeech&gt;verb-transitive&lt;/partOfSpeech&gt; &lt;/definition&gt; − &lt;definition sequence="1" id="0"&gt; − &lt;text&gt; To give up by leaving or ceasing to operate or inhabit, especially as a result of danger or other impending threat: abandoned the ship. &lt;/text&gt; &lt;headword&gt;abandon&lt;/headword&gt; &lt;partOfSpeech&gt;verb-transitive&lt;/partOfSpeech&gt; &lt;/definition&gt; − &lt;definition sequence="2" id="0"&gt; − &lt;text&gt; To surrender one's claim to, right to, or interest in; give up entirely. See Synonyms at relinquish. &lt;/text&gt; &lt;headword&gt;abandon&lt;/headword&gt; &lt;partOfSpeech&gt;verb-transitive&lt;/partOfSpeech&gt; &lt;/definition&gt; − &lt;definition sequence="3" id="0"&gt; </code></pre> <p>...</p> <p>What I want is simply the FIRST definition's part of speech. I'm using this code but it's getting the LAST definition's POS:</p> <pre><code> if($r-&gt;{definition}-&gt;{0}-&gt;{partOfSpeech}) { $pos = $r-&gt;{definition}-&gt;{0}-&gt;{partOfSpeech}; } else { $pos = $r-&gt;{definition}-&gt;{partOfSpeech}; } </code></pre> <p>I am pretty embarrassed by this since I know there's an obviously better way to do it. I would love to get something as simple as this working so I could more generally loop through the elements. BUt it just isn't working for me (no idea what to reference). I've tried many variations of the following - this is just my last attempt:</p> <pre><code> while (my ($k, $v) = each %{$r-&gt;{definitions}-&gt;{definition}[0]-&gt;{sequence}-&gt;{partOfSpeech}}) { $v =~ s/'/'"'"'/g; $v = "'$v'"; print "export $k=$v\n"; } </code></pre> <p>Lastly, when I do "print Dumper($r)" it gives me this:</p> <pre><code>$VAR1 = { 'definition' =&gt; { '0' =&gt; { 'partOfSpeech' =&gt; 'noun', 'sequence' =&gt; '6', 'text' =&gt; 'A complete surrender of inhibitions.', 'headword' =&gt; 'abandon' } } }; </code></pre> <p>(And that "noun" you see is the last (6th) definition/partofspeech element).</p> <hr> <p>Based on RC's answer below, my new code looks like this:</p> <pre><code>$content = get($url); $r = $xml-&gt;XMLin("$content", KeyAttr =&gt; { definition =&gt; 'sequence'}); while (my ($k, $v) = each %{$r-&gt;{definition}}) { $v=$r-&gt;{definition}-&gt;{$k}-&gt;{partOfSpeech}; print "export $k=$v\n"; } </code></pre> <p>This prints out the following:</p> <pre><code>export 6='noun' export 4='verb-transitive' export 1='verb-transitive' export 3='verb-transitive' export 0='verb-transitive' export 2='verb-transitive' export 5='noun' </code></pre> <p>So this is good and it is exporting the correct pairs. But now the issue is that the order is off (which seems very likely to be Wordnik's problem and not a programming issue). How do I sort this by a key? Something like this?</p> <pre><code>sort($r-&gt;{definition}); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload