title: xml to json with attributes for php or python
body: <p>I'm trying to convert some XML to JSON, which is easy enough with PHP</p>
  
  <pre><code>$file = file_get_contents('data.xml' );
  $a = json_decode(json_encode((array) simplexml_load_string($file)),1);
  print_r($a);
  </code></pre>
  
  <p>Taking the following XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;foo&gt;
      &lt;bar&gt;
          &lt;one lang="fr" type="bar"&gt;Test&lt;/one&gt;
          &lt;one lang="fr" type="foo"&gt;Test&lt;/one&gt;
          &lt;one lang="fr" type="baz"&gt;Test&lt;/one&gt;
      &lt;/bar&gt;
  
      &lt;thunk&gt;
          &lt;thud&gt;
              &lt;bar lang="fr" name="bob"&gt;test&lt;/bar&gt;
              &lt;bar lang="bz" name="frank"&gt;test&lt;/bar&gt;
              &lt;bar lang="ar" name="alive"&gt;test&lt;/bar&gt;
              &lt;bar lang="fr" name="bob"&gt;test&lt;/bar&gt;
          &lt;/thud&gt;
      &lt;/thunk&gt;
  
  &lt;/foo&gt;
  </code></pre>
  
  <p>And paring it through simplexml produces</p>
  
  <pre><code>Array
  (
      [bar] =&gt; Array
          (
              [one] =&gt; Array
                  (
                      [0] =&gt; Test
                      [1] =&gt; Test
                      [2] =&gt; Test
                  )
  
          )
  
      [thunk] =&gt; Array
          (
              [thud] =&gt; Array
                  (
                      [bar] =&gt; Array
                          (
                              [0] =&gt; test
                              [1] =&gt; test
                              [2] =&gt; test
                              [3] =&gt; test
                          )
  
                  )
  
          )
  
  )
  </code></pre>
  
  <p>Where ideally the output would look like this</p>
  
  <pre><code>{
      "foo": {
          "bar": {
              "one": [
                  {
                      "_lang": "fr",
                      "_type": "bar",
                      "__text": "Test"
                  },
                  {
                      "_lang": "fr",
                      "_type": "foo",
                      "__text": "Test"
                  },
                  {
                      "_lang": "fr",
                      "_type": "baz",
                      "__text": "Test"
                  }
              ]
          },
          "thunk": {
              "thud": {
                  "bar": [
                      {
                          "_lang": "fr",
                          "_name": "bob",
                          "__text": "test"
                      },
                      {
                          "_lang": "bz",
                          "_name": "frank",
                          "__text": "test"
                      },
                      {
                          "_lang": "ar",
                          "_name": "alive",
                          "__text": "test"
                      },
                      {
                          "_lang": "fr",
                          "_name": "bob",
                          "__text": "test"
                      }
                  ]
              }
          }
      }
  }
  </code></pre>
  
  <p>Trouble is that the output doesn't contain all the attributes for the child elements, some of these elements contain two or more attributes, is there a way to transform the xml with PHP or Python and include all the attributes found in all the children?</p>
  
  <p>Thanks</p>