从simplexml_load_file中提取数据不起作用

时间:2014-08-19 21:10:02

标签: php xml extract

我一直坚持从xml文件中获取一个值,我决定使用simplexml,因为它可以让生活变得更轻松,但它正在做相反的事情。

我的xml文件看起来像这样:

<?xml version="1.0"?>
<fslb server="12345">
<statistics>
    <frontend-connections>145</frontend-connections>
    <global-response-time-average>1899ms</global-response-time-average>
    <ecm-blacklist-size>0</ecm-blacklist-size>
    <backend-connections>0 of 0</backend-connections>
    <global-max-response-time>6888ms</global-max-response-time>     
    <global-ecm-cache-size>2396</global-ecm-cache-size>
    <global-queue-length>0</global-queue-length>
    <global-sidless-poll-average>0.00</global-sidless-poll-average>
    <actual-card-requests>0</actual-card-requests>
    <sid-sidless-requests>75% [109]/24% [36]</sid-sidless-requests>
    <invalidated-clients>0</invalidated-clients>
    <global-queue-average>0.00</global-queue-average>
    <global-max-sidless-polls>0</global-max-sidless-polls>
    <average-requests-per-card>0.00</average-requests-per-card>
    <global-cache-efficiency>0%</global-cache-efficiency>
    <total-requests-received>102934</total-requests-received>
</statistics>
<active-users>
    <user name="RS.DOS" unread-messages="0" ipaddress="xxxx" client="Mgcamd" loggedonsince="2014-08-18 06:01:03p" requests="338" period=" 8.53" sid="0695" service-name="INT" request-handling="Queued in GC (External)" hop-list="[0]: None" hash="DE:26" latency="1900" />
    <user name="S9" unread-messages="0" ipaddress="xxxx" client="Mgcamd" loggedonsince="2014-08-18 06:01:03p" requests="263" period="10.97" sid="0174" service-name=" XD" request-handling="Queued in GC (External)" hop-list="[0]: None" hash="80:C0" latency="748" />
    <user name="M1" unread-messages="0" ipaddress="174.93.86.83" client="Newcamd" loggedonsince="2014-08-18 06:01:03p" requests="12844" period=" 0.22" sid="0688" service-name="INT" request-handling="Global Cached CWs" hop-list="[0]: None" hash="4A:3F" latency="0" />
    <user name="S9" unread-messages="0" ipaddress="xxxx" client="Mgcamd" loggedonsince="2014-08-18 06:01:03p" requests="300" period=" 9.61" sid="0650" service-name="INT" request-handling="Queued in GC (External)" hop-list="[0]: None" hash="8A:4C" latency="909" />
</active-users>

</fslb>

我的最终目标是从一个用户中提取数据&#34; line(或统计行)并将其输出到mysql,以便将其保存在我的db中。输出部分看起来很简单,但我甚至无法在第一时间提取数据 - .-。这是我的PHP代码:

ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
ini_set("max_execution_time", 0);
ini_set("memory_limit", "10000M");
$file_xml = "users.xml";
if(!file_exists($file_xml))
{
    exit('Failed to open');
}

$fslb = simplexml_load_file($file_xml, 'SimpleXMLElement',LIBXML_NOCDATA);
$json_string = json_encode($fslb);  
$result_array = json_decode($json_string, TRUE);
//print_r($fslb);
echo('<p> Total number of connections: ');
echo $result_array->statistics[0]->{'frontend-connections'};
echo('</p>');
echo('<p> Global-response-time-average: ');
echo $result_array->statistics[0]->{'global-response-time-average'};
echo('</p>');

我花了好几个小时试图弄清楚如何提取一些数据,但它不会去!

SimpleXMLElement Object ( [@attributes] => Array ( [server] => 12345 ) [statistics] => SimpleXMLElement Object ( [frontend-connections] => 145 [global-response-time-average] => 1899ms [ecm-blacklist-size] => 0 [backend-connections] => 0 of 0 [global-max-response-time] => 6888ms [global-ecm-cache-size] => 2396 [global-queue-length] => 0 [global-sidless-poll-average] => 0.00 [actual-card-requests] => 0 [sid-sidless-requests] => 75% [109]/24% [36] [invalidated-clients] => 0 [global-queue-average] => 0.00 [global-max-sidless-polls] => 0 [average-requests-per-card] => 0.00 [global-cache-efficiency] => 0% [total-requests-received] => 102934 ) [active-users] => SimpleXMLElement Object ( [user] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => RS.DOS [unread-messages] => 0 [ipaddress] => xxxx [client] => Mgcamd [loggedonsince] => 2014-08-18 06:01:03p [requests] => 338 [period] => 8.53 [sid] => 0695 [service-name] => INT [request-handling] => Queued in GC (External) [hop-list] => [0]: None [hash] => DE:26 [latency] => 1900 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => S9 [unread-messages] => 0 [ipaddress] => xxxx [client] => Mgcamd [loggedonsince] => 2014-08-18 06:01:03p [requests] => 263 [period] => 10.97 [sid] => 0174 [service-name] => XD [request-handling] => Queued in GC (External) [hop-list] => [0]: None [hash] => 80:C0 [latency] => 748 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => M1 [unread-messages] => 0 [ipaddress] => 174.93.86.83 [client] => Newcamd [loggedonsince] => 2014-08-18 06:01:03p [requests] => 12844 [period] => 0.22 [sid] => 0688 [service-name] => INT [request-handling] => Global Cached CWs [hop-list] => [0]: None [hash] => 4A:3F [latency] => 0 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => S9 [unread-messages] => 0 [ipaddress] => xxxx [client] => Mgcamd [loggedonsince] => 2014-08-18 06:01:03p [requests] => 300 [period] => 9.61 [sid] => 0650 [service-name] => INT [request-handling] => Queued in GC (External) [hop-list] => [0]: None [hash] => 8A:4C [latency] => 909 ) ) ) ) )

1 个答案:

答案 0 :(得分:0)

这对我有用

的var_dump((字符串)$ fslb-&GT;统计:&GT; {&#34;前端的连接&#34;});

http://php.net/manual/en/simplexmlelement.tostring.php