0

I have an XML file that I want to be able to access the last element in the file. (NOTE: the file is dynamically updated and the latest values are always in the latest element:

<?xml version="1.0"?>

<tran>
    <balance>25000</balance>
    <amount>560</amount>

</tran>

<tran>
    <amount>5999</amount>
    <balance>30999</balance>
</tran>
<tran>
    <amount>5000</amount>
    <balance>20000</balance>
</tran>
<tran>
    <amount>8923</amount>
    <balance>25000</balance>
</tran>

I want to be able to access that last node.

How can I do this using Php Simple XML?

Thanks

Roger Williams
  • 159
  • 2
  • 7
  • 15

4 Answers4

3

Firstly your XML needs a parent node (in the example below I've inserted 'trans' element) Then try this xpath:

<?php
$xmlstr=<<<EOXML
<?xml version="1.0"?>
<trans>
<tran>
    <balance>25000</balance>
    <amount>560</amount>

</tran>

<tran>
    <amount>5999</amount>
    <balance>30999</balance>
</tran>
<tran>
    <amount>5000</amount>
    <balance>20000</balance>
</tran>
<tran>
    <amount>8923</amount>
    <balance>25000</balance>
</tran>
</trans>
EOXML;

$xml =new SimpleXMLElement($xmlstr);
$last = $xml->xpath("/trans/tran[last()]");
var_dump($last);

?>
ΣΚΟΤ
  • 354
  • 2
  • 8
  • This worked! But how do I echo out the "balance"? (I am a Novice). I tried $last["balance"] but it didn't work. – Roger Williams Apr 11 '12 at 03:45
  • `$last = $xml->xpath("/trans/tran[last()]/balance"); var_dump((int)$last[0]);` – ΣΚΟΤ Apr 11 '12 at 03:57
  • xpath is the swiss army knife for navigating your way around XML. for more info go to [w3schools](http://www.w3schools.com/xpath/) – ΣΚΟΤ Apr 11 '12 at 04:00
  • Thanks again man but when I use that `$last = $xml->xpath("/trans/tran[last()]/balance"); var_dump((int)$last[0]);` I get this `int(2147483647)` but i only want the values and when I take off the (int) i get this `object(SimpleXMLElement)#5 (1) { [0]=> string(11) "25098976600" } `. – Roger Williams Apr 11 '12 at 04:16
  • the (int) is a byproduct of the var_dump command. It is just var_dump telling you that you are looking at an integer. (http://www.php.net/manual/en/function.var-dump.php) BTW: You need to know what your requirements are for the field. So if the balance could be floating point use: $bal=(float)$last[0]; echo $bal; – ΣΚΟΤ Apr 11 '12 at 06:19
0

Try this :

<?php
    $xmlstr = <<<XML
    <?xml version="1.0"?>
    <transactions>
        <tran>
            <balance>25000</balance>
            <amount>560</amount>
        </tran>
        <tran>
            <amount>5999</amount>
            <balance>30999</balance>
        </tran>
        <tran>
            <amount>5000</amount>
            <balance>20000</balance>
        </tran>
        <tran>
            <amount>8923</amount>
            <balance>25000</balance>
        </tran>
    </transactions>
    XML;

    $elements = new SimpleXMLElement($xmlstr);

    $last_element = $elements->tran[count($elements)-1];

    print_r($last_element);

?>

Output :

SimpleXMLElement Object
(
    [amount] => 8923
    [balance] => 25000
)
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
0

Try

$xmlString = '<?xml version="1.0"?>
<doc>
<tran>
    <balance>25000</balance>
    <amount>560</amount>
</tran>

<tran>
    <amount>5999</amount>
    <balance>30999</balance>
</tran>
<tran>
    <amount>5000</amount>
    <balance>20000</balance>
</tran>
<tran>
    <amount>8923</amount>
    <balance>25000</balance>
</tran>

</doc>';

$xml = simplexml_load_string($xmlString);
$var = end($xml);
$var = end($var);
var_dump($var);

Output

object(SimpleXMLElement)[5]
  public 'amount' => string '8923' (length=4)
  public 'balance' => string '25000' (length=5)
Baba
  • 94,024
  • 28
  • 166
  • 217
0

You can simply count "tran" elements

//value of $i is the total count of tran element in xml
$last_element_amount  = $xml->tran[$i]['amount'];
$last_element_balance  = $xml->tran[$i]['balance'];
John
  • 163
  • 1
  • 11