In php, simplexml_load_string uses an instance to share _ PHP Tutorial

Source: Internet
Author: User
In php, simplexml_load_string is shared by an instance. First use a piece of code to reproduce the problem. at first glance, the results are confusing: Copy the code as follows :? Php $ stringeofdatafoobarhellobarfoobarfoobarworldbarfoodataeof; $ dat use a piece of code to reproduce the problem

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
?>

At first glance, the results are confusing: the code is as follows :? Php $ string = EOF data foobarhello/bar/foo foobarworld/bar/foo/data EOF; $ dat...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.