使用 PHP 在一个文档中处理多个 XML 标记


handle multiple xml tags in one document with php

我今天尝试用php导出一个大的XML文件,以便稍后将内容添加到MySQL数据库中。我今天与 PHP SimpleXML 取得了联系,它仅适用于 xml 标签的一个大图,但是当我添加更多类似内容时:

<features>
<name>Holy moly</name>
 ....
 </features>
<features>
<name>what the...</name>
 ...
 </features>

我的脚本无法处理多个大的 XML"all over"标签。这是我的PHP解析脚本:

<?php
include 'example.php';//heres my xml content
$features = new SimpleXMLElement($xmlstr);
/* For each <character> node, we echo a separate <name>. */
foreach ($features->properties as $properties) {
   echo "<br />".$properties->name, ' played by ', $properties->website, PHP_EOL;
}
?>

谢谢你的帮助

格式正确的 XML 文档应该只有一个根元素,封装所有其他元素。因此,我建议您使用更高的根元素封装功能元素。

解决方案:

foreach ($root->features as $features) { 
foreach ($features->properties as $properties) {
echo "<br />".$properties->name, ' played by ', $properties->website, PHP_EOL; 
 } }