-1

I assume there is a very simple answer to this.

XML looks like:

<ORDERSTATUS>
      <DATE>10/03/17</DATE>
      <INVOICE>abc</INVOICE>
      <INVTOTAL Tax="15.00" Freight="18" SubTotal="100.00">133.0</INVTOTAL>
<ORDERSTATUS>

Using simple_xml_load_string does not return the Tax, Freight or SubTotalelements. It just shows Invoice

[DATE] => 10/03/17 
[INVOICE] => abc 
[INVTOTAL] => 133.0

How do I retrieve those inner values?

Thank you

user2029890
  • 2,493
  • 6
  • 34
  • 65
  • 1
    Possible duplicate of [Accessing @attribute from SimpleXML](https://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) – Progman Jan 01 '18 at 19:53

2 Answers2

0

Assuming your xml should end with a closing </ORDERSTATUS>

simplexml_load_string returns a SimpleXMLElement which has a method attributes from which you can get "Tax" etc..

For example:

$string = <<<XML
<ORDERSTATUS>
    <DATE>10/03/17</DATE>
    <INVOICE>abc</INVOICE>
    <INVTOTAL Tax="15.00" Freight="18" SubTotal="100.00">133.0</INVTOTAL>
</ORDERSTATUS>
XML;

$xml = simplexml_load_string($string);

echo $xml->INVTOTAL->attributes()->Tax;

Which will give you:

15.00

Output

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

You can access this using attributes .First select INVTOTAL then select this attributes like tax so this is normal process

<?php
$string = <<<XML
<ORDERSTATUS>
    <DATE>10/03/17</DATE>
    <INVOICE>abc</INVOICE>
    <INVTOTAL Tax="15.00" Freight="18" SubTotal="100.00">133.0</INVTOTAL>
</ORDERSTATUS>
XML;

$xml = simplexml_load_string($string);

echo "DATE:".$xml->DATE."<br>";
echo "INVOICE:".$xml->INVOICE ."<br>";
echo "INVTOTAL:".$xml->INVTOTAL ."<br>";
echo  "Tax:".$xml->INVTOTAL->attributes()->Tax."<br>";
echo  "Freight:".$xml->INVTOTAL->attributes()->Freight."<br>";
echo  "SubTotal:".$xml->INVTOTAL->attributes()->SubTotal."<br>";

then this is output :

DATE:10/03/17
INVOICE:abc
INVTOTAL:133.0
Tax:15.00
Freight:18
SubTotal:100.00

for more information

http://php.net/manual/en/simplexml.examples-basic.php

Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43