SimpleXML in PHP

Source: Internet
Author: User
Tags foreach php language

  This article mainly introduces the use of simplexml_load_string in PHP, the need for friends can refer to the following

A piece of code to reproduce the problem first   at first glance, the result is puzzling:     code is as follows: <?php  $string = <<<EOF  <data>  &l t;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 result is puzzling: The   code is as follows: SimpleXMLElement object  (  [foo] => array  (  [0] => simplexmlelement-object  (  [Bar ] => hello )   [1] => simplexmlelement object  (  [bar] => world )  )  ) &nbsp ; SimpleXMLElement object  (  [bar] => hello )     mingming Print_r show Foo is an array of two bar elements, But in the end it only shows a bar element!   The reason is actually very simple, as shown in the results of the simplexml_load_string, Foo is not an array, but an iterative object!   can confirm:     code as follows: foreach ($data->foo as $v) Print_r ($v);  foreach ($data->children () as $v) PR Int_r ($v);     It seems that the appearance of Print_r or var_dump is not entirely believable, and pay more attention to it.   If we get the XML data as follows: (can be obtained using curl, fsockopen, etc.)     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;array;array;</acceptation>  <sent>   <orig>haven ' t seen for a Long time. How are you?</orig>   <trans> have been missing for days, 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>   & Lt;trans> Hey, Barbara, how are you? </trans>  </sent>  <sent>   <orig>how are you? -quite, thank you.</orig>   <trans>, how are you? -Very well, thank you. </trans>  </sent> </dict>      via simplexml_load_string: Code as follows: SimpleXMLElement Object (    [@attributes] => Array         (      &NBSP ;     [num] => 219             [ID] => 219             [name] => 219        )       [key] => Hello     [POS] => Simple XmlElement Object         (       )       [acceptation] => ARR Ay;array;array     [sent] => Array         (            [ 0] => simplexmlelement Object                 (        &NB Sp           [orig] => Haven ' t seen for a long time. How are?                    [Trans] => been gone for days, how are you?                -              [1] => Simpl Exmlelement Object                 (            &NB Sp       [orig] => hello! How are?                     [Trans] => Hey, how are you?                -              [2] => Simpl Exmlelement Object                 (            &NB Sp       [orig] => Hello, brooks! How are?                     [Trans] => Hey, Brooks! How are you?                -              [3] => Simpl Exmlelement Object   &NBsp             (                    [orig] = > Hi, Barbara, how are?                     [Trans] => Hey, Barbara, how are you?                -              [4] => Simpl Exmlelement Object                 (            &NB Sp       [orig] => how are? -quite, thank you.                     [Trans] => how are you? -Very well, thank you.                 (         )       &NBS P We can get the value we want in the PHP language in the following ways: The   code is as follows: <?php $data = <<<xml <?xml version= "1.0" encoding= "UTF-8"?> &L T;dict num= "219" id= "219" name= "219" >  <key> Hello </key>  <poS></pos>  <acceptation>Array;Array;Array;</acceptation>  <sent>   < Orig>haven ' t seen for a long time. How are you?</orig>   <trans> have been missing for days, 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>   & Lt;trans> Hey, Barbara, how are you? </trans>  </sent>  <sent>   <orig>how are you? -quite Well, thank you.</orig>   <trans> how are you? -Very well, 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 for a long time. How are? echo "<br/>". Trim ($xmldata->key); Hello?>  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.