First use a piece of code to reproduce the problem
At first glance, the result is puzzling:
The code is as follows |
|
$string = <<
hello
world
EOF;
代码如下 |
|
$string = <<
hello
world
EOF;
$data = simplexml_load_string($string);
print_r($data);
print_r($data->foo);
?>
|
$data = simplexml_load_string($string);
print_r($data);
print_r($data->foo);
?>
|
代码如下 |
|
SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[bar] => world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)
|
At first glance, the result is puzzling:
The code is as follows |
|
SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[bar] => world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)
|
Obviously print_r shows that foo is an array with two bar elements, but in the end only one bar element is displayed!
The reason is actually very simple. In the result of simplexml_load_string shown above, foo is not an array, but an iterable object!
代码如下 |
|
foreach ($data->foo as $v) print_r($v);
foreach ($data->children() as $v) print_r($v);
|
You can confirm like this:
The code is as follows |
|
foreach ($data->foo as $ v) print_r($v);
foreach ($data->children() as $v) print_r($v);
|
It seems that the appearance of print_r or var_dump is not completely credible, so please pay more attention to it.
Suppose the XML data we obtain is as follows: (can be obtained using curl, fsockopen, etc.)
代码如下 |
|
你好
Array;Array;Array;
Haven't seen you for a long time. How are you?
多日不见了,你好吗?
Hello! How are you?
嘿,你好?
Hello, Brooks!How are you?
喂,布鲁克斯!你好吗?
Hi, Barbara, how are you?
嘿,芭芭拉,你好吗?
How are you? -Quite well, thank you.
你好吗?-很好,谢谢你。
|
Get through simplexml_load_string:
The code is as follows |
|
SimpleXMLElement Object
(
[@attributes] => Array
(
[num] => 219
[id] => 219
[name] => 219
)
[key] => Hello www.111cn.Net
[pos] => SimpleXMLElement Object
(
)
[acceptation] => Array; Array; Array;
[sent] => Array
(
[0] => SimpleXMLElement Object
(
[orig] => Haven't seen you for a long time. How are you?
[Trans] = & GT; I have gone to see it for many days, how are you?
)
[1] => SimpleXMLElement Object
(
[orig] => Hello! How are you?
[Trans] = & GT; Hey, Hello?
)
[2] => SimpleXMLElement Object
(
[orig] => Hello, Brooks!How are you?
[Trans] = & GT; Hey, Brooks! Are you OK?
)
[3] => SimpleXMLElement Object
(
[orig] => Hi, Barbara, how are you?
[Trans] = & GT; Hey, Barbara, how are you?
)
[4] => SimpleXMLElement Object
(
[orig] =>
[Trans] = & GT; How are you? -Very well, thank you.
)
)
)
|
We can use the following method to get the value we want in PHP language:
The code is as follows |
|
$data = <<
Hello
Array;Array;Array;
Haven't seen you for a long time. How are you?
I haven’t seen you for a few days. How are you?
Hello! How are you?
Hey, how are you?
Hello, Brooks!How are you?
Hey, Brooks! Are you OK?
Hi, Barbara, how are you?
Hey, Barbara, how are you?
How are you? -Quite well, thank you.
How are you? -Very well, thank you.
XML;
$xmldata = simplexml_load_string($data);
header("Content-Type: text/html; charset=UTF-8");
print_r($xmldata);
echo " ".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you?
echo " ".trim($xmldata->key); //Hello
?>
代码如下 |
|
$data = <<
你好
Array;Array;Array;
Haven't seen you for a long time. How are you?
多日不见了,你好吗?
Hello! How are you?
嘿,你好?
Hello, Brooks!How are you?
喂,布鲁克斯!你好吗?
Hi, Barbara, how are you?
嘿,芭芭拉,你好吗?
How are you? -Quite well, thank you.
你好吗?-很好,谢谢你。
XML;
$xmldata = simplexml_load_string($data);
header("Content-Type: text/html; charset=UTF-8");
print_r($xmldata);
echo " ".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you?
echo " ".trim($xmldata->key); //你好
?>
|
|
http://www.bkjia.com/PHPjc/733191.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/733191.htmlTechArticleFirst use a piece of code to reproduce the problem. At first glance, the result is very confusing: The code is as follows?php $string = EOF data foobarhello/bar/foo foobarworld/bar/foo /data EOF; $data = simplexml_...