I'm trying to use xpath
to fetch data from a loaded SimpleXMLElement
. Hoever, the same code yields different results on local vs dev servers. Could someone point me in the right direction of what to look for?
Here is the code as ran on both servers:
<?php
$xml = simplexml_load_string('<?xml version="1.0"?>
<document>
<ADDDATA>
<PAGEN>1</PAGEN>
<DOCN>123456789</DOCN>
<DATE>06.07.2017</DATE>
</ADDDATA>
</document>
');
$nodes = $xml->xpath('//DOCN');
var_dump($nodes);
Results on local (as expected):
array (size=1)
0 =>
object(SimpleXMLElement)[3]
public 0 => string '123456789' (length=9)
Results on dev (wtf):
array(1) { [0]=> object(SimpleXMLElement)#2 (0) { } }
My local env is:
OSX10.12.6
homebrew/php/php56: stable 5.6.31 (bottled), HEAD
libxml2: stable 2.9.5 (bottled), HEAD [keg-only]
My server is:
Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-57-generic x86_64)
PHP 5.5.9-1ubuntu4.16 (cli) (built: Apr 20 2016 14:31:27)
/usr/lib/x86_64-linux-gnu/libxml2.so.2.9.1
Could the difference in versions be producing the different results?
Is there some global configuration I might be missing?
Thanks!
Don't rely on var_dump()
output for inspecting SimpleXMLElement
s. I think this specific issue you're experiencing is related to bug #66084 . See in this demo that the results change in PHP 5.6.10, which is consistent with what you're experiencing. Bug #66084 was fixed in PHP 5.6.11 .
You probably already know this, but just in case: $nodes
contains a list of SimpleXMLElement
s, not their values. So, to get the value of the node, simply cast each instance as a string, either implicitly or explicitly:
echo $nodes[0]; // 123456789 , implicitly
var_dump($nodes[0]->__toString()); // string(9) "123456789", explicitly
var_dump((string)$nodes[0]); // string(9) "123456789", explicitly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.