In php, the simplexml_load_string () function loads the XML string into the object. next I will introduce some simple examples and some notes for using simplexml_load_string... in php, the simplexml_load_string () function loads XML strings into objects. next I will introduce some simple examples and some precautions for using simplexml_load_string.
First use a piece of code to reproduce the problem. at first glance, the results are confusing. the code is as follows:
hello
world
EOF; $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 )
Obviously, print_r shows that foo is an array with two bar elements, but only shows one bar element. The reason is actually very simple. in the result of simplexml_load_string shown above, foo is not an array, it is an iteration object.
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. if we obtain the XML data as follows, we can use curl, fsockopen, and other methods to obtain the data. 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.
The following code is obtained after simplexml_load_string:
SimpleXMLElement Object ([@ attributes] => Array ([num] => 219 [id] => 219 [name] => 219) [key] => Hello www.phprm.com [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. )))
In PHP, we can use the following method to obtain the desired value. 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.
XML; $ xmldata = simplexml_load_string ($ data); head( "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