我是PHP的新手,我正在尝试读取驻留在本地文件夹上的文件夹中的XML文件的输出。在PHP.net/simplexml_load_file教程中,当我执行print_r($ xml)时,以下LIKE代码应该显示SimpleXMLElement对象。但是当我在本地终端上运行脚本时,我得到文件路径:
/Users/msavoy/XMLSourceDir/sfly-6x8.000020513524-7001536_28935-tb.33.xml
我需要能够获取$ xml对象,以便我可以显示对象中的所有元素。
这是我的代码:
// Get the correct count of the values in the $sourceXmlDir after the unset function completes
$sourceXmlArray = array_values($sourceXmlDir);
$count_xml_files = count($sourceXmlArray);
// Check to ensure that there are xml files in the directory before processing parsing the file
// If no files exist create an Exception and log the error.
if ($count_xml_files > 0) {
// Cycle through the XML files in the $sourceXmlArray
for ($i = 0; $i < $count_xml_files; $i++) {
$xml = ($sourceDir . '/' . $sourceXmlArray[$i]);
print_r($xml);
}
} else {
$error_output = date('Y-m-d H:i:s') . 'There are no XML files in the source directory: ' . $sourceDir;
error_log(date('Y-m-d H:i:s') . 'There are no XML files in the source directory: ' . $sourceDir);
throw new Exception($error_output);
}
我错过了什么?任何帮助/方向将不胜感激。问候。
答案 0 :(得分:0)
$xml = ($sourceDir . '/' . $sourceXmlArray[$i]);
print_r($xml);
您实际上并未致电simplexml_load_file
,为什么$xml
会成为SimpleXMLElement
个对象?
试试这个:
$xml = simplexml_load_file($sourceDir . '/' . $sourceXmlArray[$i]);
print_r($xml);