【发布时间】:2011-08-01 09:38:53
【问题描述】:
假设我有以下 XML 结构:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<parent>
<child1>some value</child1>
<child2>another value</child2>
</parent>
</main>
我做了一个 XML 的变量,现在我想获取 child1 的值,所以我使用 SimpleXML:
$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->main->parent->child1;
但我收到此消息:注意:尝试在第 x 行的 /x.php 中获取非对象的属性
我也用 $xml->parent->child1 尝试过,但没有成功。
有人吗??
【问题讨论】:
-
sigh 这一定是有史以来最常见的错误之一。当您将 XML 文档加载到 SimpleXmlElement 中时,根节点是 SimpleXmlElement,例如
$xml = <main>. -
感谢戈登的解释。