简体   繁体   English

不同服务器上具有相同数据的SimpleXML差异

[英]SimpleXML differences with same data on different servers

I'm trying to use xpath to fetch data from a loaded SimpleXMLElement . 我正在尝试使用xpath从已加载的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): 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. 不要依赖var_dump()输出来检查SimpleXMLElement I think this specific issue you're experiencing is related to bug #66084 . 我认为您遇到的这个特定问题与错误#66084有关 See in this demo that the results change in PHP 5.6.10, which is consistent with what you're experiencing. 此演示中看到,结果在PHP 5.6.10中发生了变化,这与您所遇到的一致。 Bug #66084 was fixed in PHP 5.6.11 . 错误#66084已在PHP 5.6.11中修复

You probably already know this, but just in case: $nodes contains a list of SimpleXMLElement s, not their values. 您可能已经知道这一点,但以防万一: $nodes包含一个SimpleXMLElement列表,而不是它们的值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM