开发者

Error in php code "Object of class DOMDocument could not be converted to string"

Bellow code showing "Object of class DOMDocument could not be converted to string". How can i solve this issue.

$rs=$_SESSION['hotelavail_rs'];
// I have stored xml response in a session.
// xml response should be string type.
$str=(string)$rs;

$DOC = new DOMDocument();
$xml =new SimpleXMLElement($str);
$DOC->loadXML($str);
$Data = Parse($DOC->firs开发者_开发技巧tChild);


According to the docs for DOMDocument, you call saveXML() to dump the XML to a string.

I'm assuming the error is occurring on this line?

$str=(string)$rs;

In which case you should change it to,

$str = $rs->saveXML();

But since you seem to be loading it all back into a DOMDocument anyway, why not just do this?

$rs = $_SESSION['hotelavail_rs'];
$Data = Parse($rs->firstChild);

If the error is occurring later (In the Parse function for example) then a different solution will be required. It would be helpful to know the exact line on which the error is being thrown.

Note: None of the above is tested, I'm just going on what the docs say.


I see 2 potential problems. I say potential because I don't know what line the error is caused by.

  • Are you sure that the XML is saved as a string in the session? You may want to try var_dump($_SESSION['hotelavail_rs']); and make sure that it's a string and not a DOMDocument object. If it's not a string, that would cause a problem with the $str=(string)$rs; line.
  • The other potential problem is the Parse() function. Is it expecting a DOMNode or a string? If it's expecting a string, that won't work as DOMDocument::firstNode returns a DOMNode object. Unfortunately, DOMDocument doesn't have a way to return a particular node and child nodes as a string.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜