title: How do I get the inner text of a xml tag when there is already content in it?
body: <p>Here is the example xml I am using :</p>
  
  <pre><code>&lt;contact id="43956"&gt;
   &lt;personal&gt;
        &lt;name&gt;
             &lt;first&gt;J&lt;/first&gt;
             &lt;middle&gt;J&lt;/middle&gt;
             &lt;last&gt;J&lt;/last&gt;
             Some text...
        &lt;/name&gt;
        &lt;title&gt;Manager&lt;/title&gt;
        &lt;employer&gt;National&lt;/employer&gt;
        &lt;dob&gt;1971-12-22&lt;/dob&gt;
   &lt;/personal&gt;
  &lt;/contact&gt;
  </code></pre>
  
  <p>I got the <code>Some text...</code> but now i need my code to read the entire xml document. It also isn't reading the values inside the xml... as you can tell I have never used <code>XMLReader</code> before.</p>
  
  <p>This is what I get:</p>
  
  <p><code>Array ( [contact] =&gt; Array ( [id] =&gt; 43956 [value] =&gt; some sample value ) [first] =&gt; [middle] =&gt; [last] =&gt; [#text] =&gt; Some text... [name] =&gt; [title] =&gt; [employer] =&gt; [dob] =&gt; [personal] =&gt; )</code></p>
  
  <p>Here is the code I have now:</p>
  
  <pre><code>function xml2array($file, array $result = array()) {
  $lastElementNodeType = '';
  $xml = new XMLReader();
  if(!$xml-&gt;open($file)) {
      die("Failed to open input file");
  }
  while($xml-&gt;read()) {
      switch ($xml-&gt;nodeType) {
          case $xml::END_ELEMENT:
              $lastElementNodeType = $xml-&gt;nodeType;
          case $xml::TEXT:
              $tag = $xml-&gt;name;
              if($lastElementNodeType == 15) {
                  $result[$tag] = $xml-&gt;readString();                 
              }
          case $xml::ELEMENT:
              $lastElementNodeType = $xml-&gt;nodeType;
              $tag = $xml-&gt;name;
              if($xml-&gt;hasAttributes) {
                  while($xml-&gt;moveToNextAttribute()) {
                      $result[$tag][$xml-&gt;name] = $xml-&gt;value;
                  }
              }
      }
  }
  print_r($result);
  }
  </code></pre>
  
  <p>I thought about making this function recursive, but when I tried that it made the array really messy.</p>
  
  <p>I had a version of this, but it still didn't output the <code>J</code> in the <code>first</code>, etc. :</p>
  
  <pre><code>function xml2assoc($xml) { 
  $tree = null; 
  while($xml-&gt;read()) 
      switch ($xml-&gt;nodeType) { 
          case XMLReader::END_ELEMENT: return $tree; 
          case XMLReader::ELEMENT: 
              $node = array('tag' =&gt; $xml-&gt;name, 'value' =&gt; $xml-&gt;isEmptyElement ? '' : xml2assoc($xml)); 
              if($xml-&gt;hasAttributes) 
                  while($xml-&gt;moveToNextAttribute()) 
                      $node['attributes'][$xml-&gt;name] = $xml-&gt;value; 
              $tree[] = $node; 
          break; 
          case XMLReader::TEXT: 
          case XMLReader::CDATA: 
              $tree .= $xml-&gt;value; 
      } 
  return $tree; 
  }
  </code></pre>