XML : simpleXmlElement losses attributes on PHP

I'm trying to convert some xml data to json with a code like this:

  $xml = file_get_contents($query);  $GSP = new SimpleXMLElement($xml);  echo json_encode($GSP);    

Problem is, during SimpleXMLElement, I lost attributes within xml files.

Here is an example from the XML;

  <ENTOBRESULTS>  <OBRES module_name="ModName">    <provider>ModName</provider>    <title>...</title>    <MODULE_RESULT>      <U>    http://someURL.aspx    </U>      <Title>    AnotherTitle   </Title>      <Field name="Main">ASD</Field>      <Field name="ProductType">type</Field>      <Field name="buttonText">Press it</Field>      <Field name="buttonUrl">https://anotherURL.aspx</Field>    </MODULE_RESULT>  </OBRES>  </ENTOBRESULTS>    

But when I log the $GSP to chrome console it shows something like this:

  ENTOBRESULTS: Object  OBRES: Array[3]    0: Object      @attributes: Object      MODULE_RESULT: Array[3]        0: Object          Field: Array[22]            0: "ASD"            1: "type"            2: "Press it"            3: "https://anotherURL.aspx"    

As you can see, I'm losing 'name' attributes during $GSP = new SimpleXMLElement($xml) action. Is there a way to prevent this? OR how can I bypass this problem?

No comments:

Post a Comment