用PHP解析XML文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用PHP解析XML文件相关的知识,希望对你有一定的参考价值。

This code shows how to parse XML file in easy way using php.
  1. <?php
  2. // this is a sample xml string
  3. $xml_string="<?xml version='1.0'?>
  4. <text>
  5. <article id='Article1'>
  6. <title>Title 1</title>
  7. <content>..text here..</content>
  8. </article>
  9. <article id='Article2'>
  10. <title>Title 2</title>
  11. <content>..text here..</content>
  12. </article>
  13. </text>";
  14.  
  15. // load this xml string using simplexml function
  16. $xml = simplexml_load_string($xml_string);
  17.  
  18. // loop through the each node
  19. foreach($xml->article as $key){
  20. // attribute are accessted by
  21. echo $key['id'].' ';
  22. // node are accessted by -> operator
  23. echo $key->title.' ';
  24. echo $key->content.'<br />';
  25. }
  26. ?>

以上是关于用PHP解析XML文件的主要内容,如果未能解决你的问题,请参考以下文章