我遇到了以下问题,我可能找不到我的错误了?
我尝试加载以下XML(超过15k行)结构:
<Config>
<SystemFiles>
<Core>
<Info>
<Network>
<Capture>
<Store>
<Mount01>
<Mount02>
</Store>
</Core>
</Config>
我需要使用它的每个子项访问商店的子结构。我的功能如下:
public static function get_storage_data()
{
if(file_exists('/var/www/content/data/data.xml')) :
$xml = simplexml_load_file('/var/www/content/data/data.xml');
foreach ($xml->Config->Core->Store->children() as $mount) {
echo $mount;
}
else:
write_log(sprintf("data.xml not found"));
endif;
}
会产生以下错误(第8行是foreach行):
Notice
: Trying to get property 'Store' of non-object in
/var/www/inc/storage.inc
on line
8
Fatal error
: Uncaught Error: Call to a member function children() on null in /var/www/inc/storage.inc:8 Stack trace: #0 /var/www/storage.php(30): Storage::get_storage_data() #1 {main} thrown in
/var/www/inc/storage.inc
on line
8
我忘记了什么或者我的错误是什么?谢谢!
答案 0 :(得分:1)
使用SimpleXML解析XML时,您不需要包含根节点名称 - 它已经锚定在该级别。
您应该只能从
更改路径$xml->Config->Core->Store->children()
到
$xml->Core->Store->children()
注意:我假设您的<Mount01>
和<Mount02>
节点只包含文本内容,因为否则您将无法回显它们。
请参阅此处查看实例:https://eval.in/1006543