-1

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->firstChild);
1
  • I'm afraid you don't understand what are you doing. Why you use 2 different php extensions (SOMSocument and SimpleXML) in parallel here? Btw, $DOC->firstChild has DOMNode type Commented Jul 10, 2011 at 11:27

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

10 Comments

I have saved xml response in an array
@Hearaman - But how did you save it in the array? As a string or did you save it as a DOMDocument object? Can you show me the line of code you're using to save the XML in the array? And by array do you mean the $_SESSION array?
$res contains this curl response stackoverflow.com/questions/6611477/…
@Hearaman - The other problem is that we don't really know which one of those lines is causing you a problem. It may not be the way it's store. It could be the Parse function as I pointed on above. If that function expects a string, it won't work because $DOC->firstNode returns a DOMNode, not a string.
using this code $xa=new xmlParser(); $xmlf=$xa->xml2array($res); $xmla=($xmlf["soapenv:Envelope"]["soapenv:Body"]["ns1:OTA_HotelAvailRQResponse"]["OTA_HotelAvailRQReturn"]); $_SESSION['hotelavail_rs']=$xmla;. Now $xmla is an array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.