SimpleXML-Load Child-试图获取 属性
SimpleXML-Load Child-trying to get property
我遇到了以下问题,我可能找不到我的错误?
我尝试加载以下 XML(超过 15k 行)结构:
<Config>
<SystemFiles>
<Core>
<Info>
<Network>
<Capture>
<Store>
<Mount01>
<Mount02>
</Store>
</Core>
</Config>
我需要访问 Store 的子结构及其每个子结构。我的函数看起来像:
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
我在这里忘记了什么或者我的错误是什么?谢谢!
使用 SimpleXML 解析时不需要包含根节点名称 XML - 它已经锚定在该级别。
您应该能够从
更改路径
$xml->Config->Core->Store->children()
至
$xml->Core->Store->children()
注意:我假设您的 <Mount01>
和 <Mount02>
节点仅包含文本内容,否则您将无法回显它们。
请在此处查看工作示例:https://eval.in/1006543
我遇到了以下问题,我可能找不到我的错误?
我尝试加载以下 XML(超过 15k 行)结构:
<Config>
<SystemFiles>
<Core>
<Info>
<Network>
<Capture>
<Store>
<Mount01>
<Mount02>
</Store>
</Core>
</Config>
我需要访问 Store 的子结构及其每个子结构。我的函数看起来像:
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
我在这里忘记了什么或者我的错误是什么?谢谢!
使用 SimpleXML 解析时不需要包含根节点名称 XML - 它已经锚定在该级别。
您应该能够从
更改路径$xml->Config->Core->Store->children()
至
$xml->Core->Store->children()
注意:我假设您的 <Mount01>
和 <Mount02>
节点仅包含文本内容,否则您将无法回显它们。
请在此处查看工作示例:https://eval.in/1006543