Note that there are some explanatory texts on larger screens.

plurals
  1. POparsing xml to list in R: how to consistently access nodes when xml structure varies?
    primarykey
    data
    text
    <h2>Background</h2> <p>I have a xml settings file that can look like this:</p> <pre><code>&lt;level1&gt; &lt;level2&gt; &lt;level3&gt; &lt;level4name&gt;bob&lt;/level4name&gt; &lt;/level3&gt; &lt;/level2&gt; &lt;/level1&gt; </code></pre> <p>but there can be multiple instances of <code>level3</code></p> <pre><code>&lt;level1&gt; &lt;level2&gt; &lt;level3&gt; &lt;level4name&gt;bob&lt;/level4name&gt; &lt;/level3&gt; &lt;level3&gt; &lt;level4name&gt;jack&lt;/level4name&gt; &lt;/level3&gt; &lt;level3&gt; &lt;level4name&gt;jill&lt;/level4name&gt; &lt;/level3&gt; &lt;/level2&gt; &lt;/level1&gt; </code></pre> <p>there can also be multiple types of <code>level4</code> nodes for each <code>level3</code>:</p> <pre><code> &lt;level3&gt; &lt;level4name&gt;bob&lt;/level4name&gt; &lt;level4dir&gt;/home/bob/ &lt;/level4dir&gt; &lt;level4logical&gt;TRUE&lt;/level4logical&gt; &lt;/level3&gt; </code></pre> <p>In R, I load this file using</p> <pre><code>settings.xml &lt;- xmlTreeParse(settings.file) settings &lt;- xmlToList(settings.xml) </code></pre> <p>I want to write a script that converts all of the values contained in <code>level4type1</code> to a vector of the unique values at this level, but I am stumped trying to do this in a way that works for all of the above cases.</p> <p>One of the problems is that the <code>class(settings[['level2']])</code> is a list for the first two cases and a matrix for the third case.</p> <pre><code>&gt; xmlToList(xmlTreeParse('case1.xml')) $level2.level3.level4name [1] "bob" &gt; xmlToList(xmlTreeParse('case2.xml')) level2 level3.level4name "bob" level3.level4name "jack" level3.level4name "jill" &gt; xmlToList(xmlTreeParse('case3.xml')) level2 level3 List,3 level3 List,1 level3 List,1 </code></pre> <h2>Questions</h2> <p>I have two questions:</p> <ol> <li><p>how can I extract a vector of the unique values of 'level4type1`</p></li> <li><p>is there a better way to do this? </p></li> </ol>
    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.
 

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