This article describes how to use simplexml_load_string in php. For more information, see the following code.
At first glance, the results are confusing:
The Code is as follows:
$ String = <
Hello
World
EOF;
$ Data = simplexml_load_string ($ string );
Print_r ($ data );
Print_r ($ data-> foo );
?>
At first glance, the results are confusing:
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 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 iteration object!
You can confirm as follows:
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 representation of print_r or var_dump is not completely credible. Please pay attention to it.
Assume that the obtained XML data is as follows: (you can use curl, fsockopen, etc)
The Code is as follows:
Hi!
Array;
Haven't seen you for a long time. How are you?
It's been several days away. How are you?
Hello! How are you?
Hey, how are you?
Hello, Brooks! How are you?
Hello, Brooks! How are you?
Hi, Barbara, how are you?
Hey, Barbara, how are you?
How are you? -Quite well, thank you.
How are you? -Good. Thank you.
After simplexml_load_string:
The Code is as follows:
SimpleXMLElement Object
(
[@ Attributes] => Array
(
[Num] = & gt; 219
[Id] = & gt; 219
[Name] = & gt; 219
)
[Key] => hello
[Pos] => SimpleXMLElement Object
(
)
[Acceptation] => Array;
[Sent] => Array
(
[0] => SimpleXMLElement Object
(
[Orig] => Haven't seen you for a long time. How are you?
[Trans] => it's been several days away. How are you?
)
[1] => SimpleXMLElement Object
(
[Orig] => Hello! How are you?
[Trans] => Hey, hello?
)
[2] => SimpleXMLElement Object
(
[Orig] => Hello, Brooks! How are you?
[Trans] => hello, Brooks! How are you?
)
[3] => SimpleXMLElement Object
(
[Orig] => Hi, Barbara, how are you?
[Trans] => Hey, Barbara, how are you?
)
[4] => SimpleXMLElement Object
(
[Orig] => How are you? -Quite well, thank you.
[Trans] => how are you? -Good. Thank you.
)
)
)
We can use the following methods in PHP to obtain the desired value:
The Code is as follows:
$ Data = <
Hi!
Array;
Haven't seen you for a long time. How are you?
It's been several days away. How are you?
Hello! How are you?
Hey, how are you?
Hello, Brooks! How are you?
Hello, Brooks! How are you?
Hi, Barbara, how are you?
Hey, Barbara, how are you?
How are you? -Quite well, thank you.
How are you? -Good. 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
?>