如何在php中解析xml子树

你好,我写了一些 php 代码来解析 xml 文件,只要我使用单独的字段,这就像一个魅力,但我如何调用子树?


像例如:


<item>

  <items>

    <date>

      <from>2020-03-23</from>

      <until>2020-03-27</until>

    </date>

  </items>

</item>

```


红颜莎娜
浏览 105回答 2
2回答

蛊毒传说

我假设子树是指元素中的元素等。PHP 为此提供了一个内置解决方案:simplexml_load_string$xml = <<<BLOCK<item>  <items>    <date>      <from>2020-03-23</from>      <until>2020-03-27</until>    </date>  </items></item>BLOCK;var_dump(simplexml_load_string($xml));您也可以使用simplexml_load_file直接读取文件。

慕勒3428872

我已经找到了解决这个问题的方法,我已经执行了以下操作:<?phperror_reporting(E_ALL);$xml_file = simplexml_load_file("converted.xml");echo $xml_file->date[0]->from;echo $xml_file->date[0]->until;?>```
打开App,查看更多内容
随时随地看视频慕课网APP