Introduction to simplexml_load_string instance in php

Source: Internet
Author: User

Introduction to simplexml_load_string instance in php

This article describes how to use simplexml_load_string in php. For more information, see

Use a piece of code to reproduce the problem.

 

At first glance, the results are confusing:

 

The Code is as follows:

<? Php

$ String = <EOF

<Data>

<Foo> <bar> hello </bar> </foo>

<Foo> <bar> world </bar> </foo>

</Data>

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:

<? Xml version = "1.0" encoding = "UTF-8"?>

<Dict num = "219" id = "219" name = "219">

<Key> Hello </key>

<Pos> </pos>

<Acceptation> Array; </acceptation>

<Sent>

<Orig> Haven't seen you for a long time. How are you? </Orig>

<Trans> the system disappeared several days ago. How are you? </Trans>

</Sent>

<Sent>

<Orig> Hello! How are you? </Orig>

<Trans> Hey, how are you? </Trans>

</Sent>

<Sent>

<Orig> Hello, Brooks! How are you? </Orig>

<Trans> hello, Brooks! How are you? </Trans>

</Sent>

<Sent>

<Orig> Hi, Barbara, how are you? </Orig>

<Trans> Hey, Barbara, how are you? </Trans>

</Sent>

<Sent>

<Orig> How are you? -Quite well, thank you. </orig>

<Trans> how are you? -Good. Thank you. </Trans>

</Sent>

</Dict>

 

 

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:

<? Php

$ Data = <XML

<? Xml version = "1.0" encoding = "UTF-8"?>

<Dict num = "219" id = "219" name = "219">

<Key> Hello </key>

<Pos> </pos>

<Acceptation> Array; </acceptation>

<Sent>

<Orig> Haven't seen you for a long time. How are you? </Orig>

<Trans> the system disappeared several days ago. How are you? </Trans>

</Sent>

<Sent>

<Orig> Hello! How are you? </Orig>

<Trans> Hey, how are you? </Trans>

</Sent>

<Sent>

<Orig> Hello, Brooks! How are you? </Orig>

<Trans> hello, Brooks! How are you? </Trans>

</Sent>

<Sent>

<Orig> Hi, Barbara, how are you? </Orig>

<Trans> Hey, Barbara, how are you? </Trans>

</Sent>

<Sent>

<Orig> How are you? -Quite well, thank you. </orig>

<Trans> how are you? -Good. Thank you. </Trans>

</Sent>

</Dict>

XML;

$ Xmldata = simplexml_load_string ($ data );

Header ("Content-Type: text/html; charset = UTF-8 ");

Print_r ($ xmldata );

Echo "<br/>". trim ($ xmldata-> sent [0]-> orig); // haven' t seen you for a long time. How are you?

Echo "<br/>". trim ($ xmldata-> key); // hello

?>

 

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.