Use simplexml_load_string in php

Source: Internet
Author: User
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

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.