0

Does anybody know why SimpleXMLElement is removing the attributes in my XML??

I have XML data that looks like this (note the translation "language" attribute):

<events> 
 <event id="d8f17143-0c67-48aa-a7f1-003a5ddbd28f"> 
    <details> 
        <names> 
            <translation language="en">English title</translation> 
            <translation language="de">German title</translation> 
        </names> 
    </details> 
 </event>
</events> 

I run it through SimpleXmlElement like so:

$xmlConvertedData = new \SimpleXMLElement($xml);

I dump out the data and it looks like so:

object(SimpleXMLElement)#958 (2) {
    ["@attributes"]=>
    array(1) {
        ["Index"]=>
        string(1) "1"
    }
    ["Events"]=>
    object(SimpleXMLElement)#956 (1) {
        ["Event"]=>
        array(1) {
            [0]=>
            object(SimpleXMLElement)#959 (1) {
                ["Details"]=>
                object(SimpleXMLElement)#826 (13) {
                    ["Names"]=>
                    object(SimpleXMLElement)#834 (1) {
                        ["Translation"]=>
                        array(2) {
                            [0]=>
                            string(32) "English title"
                            [1]=>
                            string(33) "German title"
                        }
                    }
                }
            }
        }
    }
}

...notice "translation" no longer has a "language" attribute, just an ID number 0 and 1. I need to know the attribute value because the XML does not always show the same language first.

(I edited the shortened the sample code to one record, so please ignore the #958 part)

daedsidog
  • 1,732
  • 2
  • 17
  • 36
Delmontee
  • 1,898
  • 2
  • 26
  • 44
  • Possible duplicate of [php SimpleXML attributes are missing](https://stackoverflow.com/questions/12432739/php-simplexml-attributes-are-missing) – miken32 Dec 06 '18 at 20:48

1 Answers1

0

Do not use any of the print_r() or var_dump() on a SimpleXML object, this will abbreviate the output as there is potentially a lot of it. If you want to check the document loaded use asXML()...

echo $xmlConvertedData->asXML();

or to output the one elements language...

echo $xmlConvertedData->event[0]->details->names->translation['language'];

( You also need to correct the last element of the sample - </events>)

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks for the advice. I can see the attributes via echo asXML() however the second output example doesn't return anything. I had simplified my XML too much, so in reality, the structure is ferateldsirs > result > events > event..... I tried $xmlConvertedData->result->events->event[0]->details->names->translation['language'] but it returns empty. – Delmontee Dec 05 '18 at 14:10
  • It was just an example, you will need to work out for your structure how your accessing the data and this may be XPath or just using `foreach()` you will need to define this more specifically and with a full code for the XML. – Nigel Ren Dec 05 '18 at 14:22