得到这个错误
在非对象上调用成员函数attributes()
我已经在SO上找到了多个答案,但它们似乎都没有解决我的问题?
这是XML:
<Routes> <Route type="source" name="incoming"> </Route> <Routes>这是PHP:
$doc = new SimpleXMLElement('routingConfig.xml', null, true); class traverseXML { function getData() { global $doc; $routeCount = count($doc -> xpath("Route")); //this value returns correctly $routeArr = array(); for ($i = 1; $i <= $routeCount; $i++) { $name = $doc -> Route[$i] -> attributes() -> name; array_push($routeArr, $name); } return $routeArr; } } $traverseXML = new traverseXML; var_dump($traverseXML -> getData());我理解错误的含义,但它是如何成为非对象的呢? 如何返回Routes/Route[1]的name属性?
Getting this error
Call to a member function attributes() on a non-object
I have found multiple answers to this on SO, but none of them seem to solve my problem?
Here is the XML:
<Routes> <Route type="source" name="incoming"> </Route> <Routes>Here is the PHP:
$doc = new SimpleXMLElement('routingConfig.xml', null, true); class traverseXML { function getData() { global $doc; $routeCount = count($doc -> xpath("Route")); //this value returns correctly $routeArr = array(); for ($i = 1; $i <= $routeCount; $i++) { $name = $doc -> Route[$i] -> attributes() -> name; array_push($routeArr, $name); } return $routeArr; } } $traverseXML = new traverseXML; var_dump($traverseXML -> getData());I understand what the error means, but how is it a non-object? How do I return the name attribute of Routes/Route[1] ?
最满意答案
您的$doc是<Routes> 。 试图获得->Routes来自它的->Routes正试图获得
<Routes> <Routes>你需要做$doc->Route[$i] 。 在文档根目录后命名变量时,此类错误的频率较低:
$Routes = new SimpleXMLElement('routingConfig.xml', null, true);此外,您的XML无效。 Routes元素未关闭。
此外,您不需要XPath。 SimpleXML是可遍历的,因此您可以通过执行操作来覆盖所有路由
foreach ($Routes->Route as $route) {而attributes()返回一个数组,所以你不能链->name它,但必须用方括号访问它。 但是无论如何都没有必要使用attributes() ,因为你可以通过方括号直接从SimpleXmlElements获取属性,例如
echo $route['name'];这是一个打印“传入”的示例:
$xml = <<< XML <Routes> <Route type="source" name="incoming"/> </Routes> XML; $routes = simplexml_load_string($xml); foreach ($routes->Route as $route) { echo $route['name']; }演示
如果你想用XPath做,你可以收集数组中的所有属性,如下所示:
$routeNames = array_map('strval', $Routes->xpath('/Routes/Route/@name'));是的,这只是一行:)
至于你的班级:
不要使用global 。 忘了它存在。 如果你想拥有一个类,请注入依赖项,例如do
class Routes { private $routes; public function __construct(SimpleXmlElement $routes) { $this->routes = $routes; } public function getRouteNames() { return array_map('strval', $this->routes->xpath('/Routes/Route/@name')); } } $routes = new Routes(simplexml_load_string($xml)); print_r($routes->getRouteNames());演示
Your $doc is <Routes>. Trying to get ->Routes from it is trying to get
<Routes> <Routes>You need to do $doc->Route[$i]. Errors like this are less frequent when you name your variable after the document root:
$Routes = new SimpleXMLElement('routingConfig.xml', null, true);Also, your XML is invalid. The Routes element is not closed.
In addition, you don't need the XPath. SimpleXML is traversable, so you can foreach over all the routes by doing
foreach ($Routes->Route as $route) {And attributes() returns an array, so you cannot chain ->name off it but must access it with square brackets. But it's not necessary to use attributes() anyway because you can get attributes from SimpleXmlElements directly via square brackets, e.g.
echo $route['name'];Here is an example that will print "incoming":
$xml = <<< XML <Routes> <Route type="source" name="incoming"/> </Routes> XML; $routes = simplexml_load_string($xml); foreach ($routes->Route as $route) { echo $route['name']; }
demo
If you want to do it with XPath, you can collect all the attributes in an array like this:
$routeNames = array_map('strval', $Routes->xpath('/Routes/Route/@name'));Yes, it's just that one line :)
As for your class:
Don't use global. Forget it exists. If you want to have a class, inject the dependency, e.g. do
class Routes { private $routes; public function __construct(SimpleXmlElement $routes) { $this->routes = $routes; } public function getRouteNames() { return array_map('strval', $this->routes->xpath('/Routes/Route/@name')); } } $routes = new Routes(simplexml_load_string($xml)); print_r($routes->getRouteNames());demo
更多推荐
发布评论