PHP:如何访问的最后一个元素在一个XML文件 [英] PHP: How to access the last element in a XML file

查看:149
本文介绍了PHP:如何访问的最后一个元素在一个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有希望能够访问的最后一个元素在文件中的XML文件。 (注:该文件被动态更新和最新的值总是在最新的元件

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.

我怎样才能做到这一点使用PHP简单的XML?

How can I do this using Php Simple XML?

感谢

推荐答案

首先您需要XML父节点(在下面的例子中,我插入'跨'元素)
那就试试这个XPath:

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);

?>

这篇关于PHP:如何访问的最后一个元素在一个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆