php 类 DOMElement 的对象无法转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7648578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:05:25  来源:igfitidea点击:

Object of class DOMElement could not be converted to string

phpxml-parsingdomdocument

提问by Rapha?l

I try to parse an XML rss flux. Actually, an error is thrown:

我尝试解析 XML rss 流量。实际上,抛出了一个错误:

Catchable fatal error: Object of class DOMElement could not be converted to string in ...

I want to get the value "test" of the tag "link"

我想获得标签“link”的值“test”

Here is my code :

这是我的代码:

//check if url contents xml
            $content = file_get_contents($flux);

            $xml = new DOMDocument;
            $xml->loadXML($content);

            //get the link
            $link = $xml->getElementsByTagName('link')->item(0);

            echo $link;

Here is the flux :

这是通量:

<?xml version="1.0" encoding="ISO-8859-15" ?>
<rss version="2.0">
    <channel>
        <title>test</title>
        <link>http://test.fr</link>
    </channel>
</rss>

Anyone can help me ?

任何人都可以帮助我吗?

回答by hakre

$linkis an object which can not be converted to string (some objects can).

$link是一个不能转换为字符串的对象有些对象可以)。

To see which object it is, use var_dump($link);. I assume it's a DOMElementDocs, see the link for all properties and methods it has to offer, e.g.

要查看它是哪个对象,请使用var_dump($link);。我假设它是一个DOMElementDocs,请参阅它必须提供的所有属性和方法的链接,例如

echo $link->tagName;

or

或者

echo $link->textContent;