国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php中simplexml_load_string使用實(shí)例

2024-05-04 21:48:11
字體:
供稿:網(wǎng)友

在php中simplexml_load_string() 函數(shù)把 XML 字符串載入對象中,下面我來給大家介紹幾個簡單實(shí)例的同時也介紹在使用simplexml_load_string的一些需要注意的事項(xiàng).

先用一段代碼重現(xiàn)一下問題,乍一看,結(jié)果很讓人費(fèi)解,代碼如下:

  1. <?php  
  2. $string = <<<EOF  
  3. <data>  
  4. <foo><bar>hello</bar></foo>  
  5. <foo><bar>world</bar></foo>  
  6. </data>  
  7. EOF; 
  8.  
  9. $data = simplexml_load_string($string); 
  10.  
  11. print_r($data);  
  12. print_r($data->foo);  
  13. ?> 
  14.  
  15. SimpleXMLElement Object  
  16. (  
  17. [foo] => Array  
  18. (  
  19. [0] => SimpleXMLElement Object  
  20. (  
  21. [bar] => hello  
  22. )  
  23. [1] => SimpleXMLElement Object  
  24. (  
  25. [bar] => world  
  26. )  
  27. //開源代碼Vevb.com 
  28. )  
  29. SimpleXMLElement Object  
  30. (  
  31. [bar] => hello  

明明print_r顯示foo是一個有兩個bar元素的數(shù)組,但是最后卻僅僅顯示了一個bar元素,原因其實(shí)很簡單,在如上所示simplexml_load_string的結(jié)果里,foo并不是數(shù)組,而是一個迭代對象.

可以這樣確認(rèn),代碼如下:

foreach ($data->foo as $v) print_r($v); 

foreach ($data->children() as $v) print_r($v);

看來,print_r或者var_dump之類的表象并不完全可信,假如我們獲取的XML數(shù)據(jù)如下,可以使用curl、fsockopen等方式獲取,代碼如下:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <dict num="219" id="219" name="219"> 
  3.  <key>你好</key> 
  4.  <pos></pos> 
  5.  <acceptation>Array;Array;Array;</acceptation> 
  6.  <sent> 
  7.   <orig>Haven't seen you for a long time. How are you?</orig> 
  8.   <trans>多日不見了,你好嗎?</trans> 
  9.  </sent> 
  10.  <sent> 
  11.   <orig>Hello! How are you?</orig> 
  12.   <trans>嘿,你好?</trans> 
  13.  </sent> 
  14.  <sent> 
  15.   <orig>Hello, Brooks!How are you?</orig> 
  16.   <trans>喂,布魯克斯!你好嗎?</trans> 
  17.  </sent> 
  18.  <sent> 
  19.   <orig>Hi, Barbara, how are you?</orig> 
  20.   <trans>嘿,芭芭拉,你好嗎?</trans> 
  21.  </sent> 
  22.  <sent> 
  23.   <orig>How are you? -Quite well, thank you.</orig> 
  24.   <trans>你好嗎?-很好,謝謝你。</trans> 
  25.  </sent> 
  26. </dict>  

經(jīng)過simplexml_load_string得到如下代碼:

  1. SimpleXMLElement Object 
  2.     [@attributes] => Array 
  3.         ( 
  4.             [num] => 219 
  5.             [id] => 219 
  6.             [name] => 219 
  7.         ) 
  8.  
  9.     [key] => 你好m.survivalescaperooms.com 
  10.     [pos] => SimpleXMLElement Object 
  11.         ( 
  12.         ) 
  13.  
  14.     [acceptation] => Array;Array;Array; 
  15.     [sent] => Array 
  16.         ( 
  17.             [0] => SimpleXMLElement Object 
  18.                 ( 
  19.                     [orig] => Haven't seen you for a long time. How are you? 
  20.                     [trans] => 多日不見了,你好嗎? 
  21.                 ) 
  22.  
  23.             [1] => SimpleXMLElement Object 
  24.                 ( 
  25.                     [orig] => Hello! How are you? 
  26.                     [trans] => 嘿,你好? 
  27.                 ) 
  28.  
  29.             [2] => SimpleXMLElement Object 
  30.                 ( 
  31.                     [orig] => Hello, Brooks!How are you? 
  32.                     [trans] => 喂,布魯克斯!你好嗎? 
  33.                 ) 
  34.  
  35.             [3] => SimpleXMLElement Object 
  36.                 ( 
  37.                     [orig] => Hi, Barbara, how are you? 
  38.                     [trans] => 嘿,芭芭拉,你好嗎? 
  39.                 ) 
  40.  
  41.             [4] => SimpleXMLElement Object 
  42.                 ( 
  43.                     [orig] => How are you? -Quite well, thank you. 
  44.                     [trans] => 你好嗎?-很好,謝謝你。 
  45.                 ) 
  46.  
  47.         ) 
  48.  

我們在PHP語言中可以用以下方法取得我們想要的值,代碼如下:

  1. <?php 
  2. $data = <<<XML 
  3. <?xml version="1.0" encoding="UTF-8"?> 
  4. <dict num="219" id="219" name="219"
  5.  <key>你好</key> 
  6.  <pos></pos> 
  7.  <acceptation>Array;Array;Array;</acceptation> 
  8.  <sent> 
  9.   <orig>Haven't seen you for a long time. How are you?</orig> 
  10.   <trans>多日不見了,你好嗎?</trans> 
  11.  </sent> 
  12.  <sent> 
  13.   <orig>Hello! How are you?</orig> 
  14.   <trans>嘿,你好?</trans> 
  15.  </sent> 
  16.  <sent> 
  17.   <orig>Hello, Brooks!How are you?</orig> 
  18.   <trans>喂,布魯克斯!你好嗎?</trans> 
  19.  </sent> 
  20.  <sent> 
  21.   <orig>Hi, Barbara, how are you?</orig> 
  22.   <trans>嘿,芭芭拉,你好嗎?</trans> 
  23.  </sent> 
  24.  <sent> 
  25.   <orig>How are you? -Quite well, thank you.</orig> 
  26.   <trans>你好嗎?-很好,謝謝你。</trans> 
  27.  </sent> 
  28. </dict> 
  29. XML; 
  30. $xmldata = simplexml_load_string($data); 
  31. header("Content-Type: text/html; charset=UTF-8"); 
  32. print_r($xmldata); 
  33. echo "<br />".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you? 
  34. echo "<br />".trim($xmldata->key); //你好 
  35. ?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 衡山县| 辽中县| 阿克陶县| 上栗县| 蓝山县| 安平县| 五台县| 楚雄市| 玛多县| 龙井市| 岐山县| 平泉县| 漳平市| 阆中市| 宁强县| 达州市| 屯昌县| 措勤县| 双峰县| 海丰县| 宁远县| 天祝| 贞丰县| 安徽省| 祥云县| 徐水县| 南丰县| 濮阳市| 庄河市| 高淳县| 玉龙| 泰宁县| 龙海市| 四平市| 东光县| 六枝特区| 江西省| 东山县| 天台县| 隆回县| 安化县|