我正在尝试使用PHP读取一个XML文件。XML文件是一个来自其他地方的报告。除了XML文件最终在"ReportItem"标记中包含多个同名标记外,其他一切都很正常。
例如,当只有一个标记时,该标记的内容将正确显示。但当有多个标记时,它会显示"ARRAY(0x996c840)"。
我的XML如下所示:
<Scan>
<Report>
<ReportHost>
<ReportItem>
...
<see_also>http://google.com</see_also>
...
</ReportItem>
<ReportItem>
...
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
...
</ReportItem>
...
</ReportHost>
</Report>
</Scan>
我的代码如下:
//Load the XML File
$xml = simplexml_load_file($path);
foreach ($xml->ReportHost as $reportHost)
{
$x = 1;
foreach ($reportHost->ReportItem as $reportItem)
{
...
$ARCHIVES['SEE_ALSO'][$x] = str_replace( "\n", '<br />', $reportItem->see_also);
...
}
}
我尝试了很多不同的方法,但似乎都不起作用。即使只是试图展示它也无济于事。
我试过:
print_r($xml->xpath("//see_also"));
但如果只有1个标记,这仍然只会给我内容,否则我会得到相同的"ARRAY(0x996c840)"结果。
我也试过:
$z = 0;
while (isset($reportItem->see_also[$z]) AND ($reportItem->see_also[$z] != ""))
{
echo "See Also: " . $reportItem->see_also[$z] . "n";
$z++;
}
但我总是得到同样的东西。
我甚至尝试使用xmlutils.php中的"xml_to_array"函数,这是我从以下位置下载的脚本:
http://pynej.blogspot.com/2010/02/convert-xml-to-array-and-array-to-xml.html
但它仍然总是显示"ARRAY(0x996c840)"而不是内容。
我真的需要别人帮我看看我做错了什么。提前感谢您的帮助!
必须是:
foreach ($xml->Report->ReportHost as $reportHost)
...
您忘记了<Report>
节点。
看到它工作:https://eval.in/106449