0

I have XML content using SimpleXMLElement and var_dump results are appeared.

Sample Code:

$data = new SimpleXMLElement($xml);
$data->registerXPathNamespace('ns','http://endpoint.websitecom/');
$part = $data->xpath("//ns:return");
var_dump($part[0]->children("ns",true));

My Results:

object(SimpleXMLElement)[4]
  public 'result' => 
    object(SimpleXMLElement)[6]
      public 'result' => 
      object(SimpleXMLElement)[10]
      public 'Code' => string '0' (length=1)

  public 'amount' => string '2020.03' (length=7)
  public 'code2' => string '3934.8' (length=6)
object(SimpleXMLElement)[8]

How Can I pass amount, code2 variables into PHP variables or array?

$amount = ?????

$code2 = ????
Sathiska
  • 495
  • 1
  • 6
  • 22
  • Could you add the xml structure? I'm not sure if I got it correctly. – rekire Jun 16 '13 at 08:32
  • 1
    Never ever duplicate your own question only because you don't got any answer so far. It only will get you downvotes and you will loose potential friends. – hakre Jun 16 '13 at 09:30
  • Quiet many dubicates I did not note it. I found two yet. @hakre – rekire Jun 16 '13 at 09:33
  • duplicate material: http://stackoverflow.com/questions/12865858/simplexmlelement-access-elements-with-namespace - also self-duplicator. – hakre Jun 22 '13 at 09:09

1 Answers1

1

I wrote a test case with the flowing xml code (reverse engineered on your var_dump):

<unknown xmlns:ns="http://endpoint.websitecom/">
 <ns:return>
  <ns:result>
   <ns:result>
    <ns:Code>0</ns:Code>
   </ns:result>
  </ns:result>
  <ns:amount>2020.03</ns:amount>
  <ns:code2>3934.8</ns:code2>
 </ns:return>
</unknown>

I wrote this code here for getting $amount and $code2:

$data = new SimpleXMLElement($xml);
$data->registerXPathNamespace('ns','http://endpoint.websitecom/');
$part = $data->xpath("//ns:return");
$branch=$part[0]->children("ns",true);
var_dump($branch); // your current output

$amount=$branch->amount;
$code2 =$branch->code2;
rekire
  • 47,260
  • 30
  • 167
  • 264