php中simplexml_load_file函数使用方法

在php中simplexml_load_file() 函数把 XML 文档载入对象中之后我们就可以利用由此函数返回回的对象进行相关的操作了,下面我们看几个测试实例.

例子,XML文件,代码如下:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <note>
  3. <to>George</to>
  4. <from>John</from>
  5. <heading>Reminder</heading>
  6. <body>Don't forget the meeting!</body>
  7. </note>

PHP 代码如下:

  1. <?php
  2. if (file_exists('test.xml'))
  3. {
  4. $xml = simplexml_load_file('test.xml');
  5. var_dump($xml);
  6. }
  7. else
  8. {
  9. exit('Error.');
  10. }
  11. ?>
  12. //输出:
  13. object(SimpleXMLElement)#1 (4)
  14. {
  15. ["to"]=> string(4) "George"
  16. ["from"]=> string(4) "John"
  17. ["heading"]=> string(8) "Reminder"
  18. ["body"]=> string(29) "Don't forget the meeting!"
  19. }

假如有一个“iciba.xml”文件,其内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <dict num="219" name="219">
  3. <key>天空</key>
  4. <pos></pos>
  5. <acceptation>Array;Array;</acceptation>
  6. <sent>
  7. <orig>The church tower stood against the sky like a finger pointing towards heaven.</orig>
  8. <trans>教堂的尖塔在天空的映衬下宛如指向天空的手指。</trans>
  9. </sent>
  10. <sent>
  11. <orig>A balloon floated across the sky.</orig>
  12. <trans>气球飘过天空。</trans>
  13. </sent>
  14. <sent>
  15. <orig>A bolt of lightning lit up the sky.</orig>
  16. <trans>(一道)闪电照亮了天空。</trans>
  17. </sent>
  18. <sent>
  19. <orig>A bright moving object appeared in the sky at sunset.</orig>
  20. <trans>日落西山时,天空出现了一个移动的发亮物体。</trans>
  21. </sent>
  22. <sent>
  23. <orig>A bright rainbow arched above.</orig>
  24. <trans>一弯明亮的彩虹悬挂在天空。</trans>
  25. </sent>
  26. </dict>在PHP语言中我们可以用以下方法取得我们想要的值:
  27. <?php
  28. $xmldata = simplexml_load_file("iciba.xml");
  29. header("Content-Type: text/html; charset=UTF-8");
  30. print_r($xmldata); //第一部分www.phpfensi.com
  31. $listcount = count($xmldata->sent);
  32. for($i=0;$i<$listcount;$i++){ //第二部分
  33. $dictlist = $xmldata->sent[$i];
  34. echo "<br />例句:".$dictlist->orig;
  35. echo "<br />翻译:".$dictlist->trans;
  36. }
  37. ?>“第一部分”将输出:
  38. SimpleXMLElement Object
  39. (
  40. [@attributes] => Array
  41. (
  42. [num] => 219
  43. [id] => 219
  44. [name] => 219
  45. )
  46. [key] => 天空
  47. [pos] => SimpleXMLElement Object
  48. (
  49. )
  50. [acceptation] => Array;Array;
  51. [sent] => Array
  52. (
  53. [0] => SimpleXMLElement Object
  54. (
  55. [orig] => The church tower stood against the sky like a finger pointing towards heaven.
  56. [trans] => 教堂的尖塔在天空的映衬下宛如指向天空的手指。
  57. )
  58. [1] => SimpleXMLElement Object
  59. (
  60. [orig] => A balloon floated across the sky.
  61. [trans] => 气球飘过天空。
  62. )
  63. [2] => SimpleXMLElement Object
  64. (
  65. [orig] => A bolt of lightning lit up the sky.
  66. [trans] => (一道)闪电照亮了天空。
  67. )
  68. [3] => SimpleXMLElement Object
  69. (
  70. [orig] => A bright moving object appeared in the sky at sunset.
  71. [trans] => 日落西山时,天空出现了一个移动的发亮物体。
  72. )
  73. [4] => SimpleXMLElement Object
  74. (
  75. [orig] => A bright rainbow arched above.
  76. [trans] => 一弯明亮的彩虹悬挂在天空。
  77. )
  78. )
  79. )“第二部分”将输出:
  80. 例句:The church tower stood against the sky like a finger pointing towards heaven.
  81. 翻译:教堂的尖塔在天空的映衬下宛如指向天空的手指。
  82. 例句:A balloon floated across the sky.
  83. 翻译:气球飘过天空。
  84. 例句:A bolt of lightning lit up the sky.
  85. 翻译:(一道)闪电照亮了天空。
  86. 例句:A bright moving object appeared in the sky at sunset.
  87. 翻译:日落西山时,天空出现了一个移动的发亮物体。
  88. 例句:A bright rainbow arched above.
  89. 翻译:一弯明亮的彩虹悬挂在天空。

例子,更深入的一个遍历输出生成表格,代码如下:

  1. eader("content-type:text/html; charset=utf-8"); //设置编码
  2. $xml = simplexml_load_file('a.xml'); //载入xml文件 $lists和xml文件的根节点是一样的
  3. echo $xml->company."<br>";
  4. echo $xml->town."<br>id:";
  5. echo $xml->town['id']."<br>parent:";
  6. echo $xml->town['parent']."<br>";
  7. echo "<br>循环读取:<br>";
  8. foreach($xml->user as $users){ //有多个user,取得的是数组,循环输出
  9. echo "-------------------<br>";
  10. echo "姓名:".$users->name."<br>";
  11. echo "编号:".$users->age."<br>";
  12. echo "性别:".$users->age['sex']."<br>";
  13. echo "序号:".$users->height."<br>";
  14. }//开源代码phpfensi.com
  15. echo "<br>循环读取:<br>";
  16. foreach($xml->town as $towns){ //有多个user,取得的是数组,循环输出
  17. echo "-------------------<br>";
  18. echo "id:".$towns['id']."<br>";
  19. echo "归属:".$towns['parent']."<br>";
  20. echo "地区:".$towns."<br>";
  21. }