Elie
Elie

Reputation: 13875

Get XML results as string

I have the following code which generates XML:

$dom = new DomDocument('1.0');
...snip adding elements to document...
$xmlout = $dom->saveXML();
$api = "myapi.com/myservice?". trim($xmlout);

The problem is that if I look at the actual value of $api it looks like

"myapi.com/myservice?"<xml-structure>

instead of

"myapi.com/myservice?<xml-structure>"

when I print out $api using print_r(); and I suspect that this is interfering with actually calling the api, since the url is incorrect. My suspicion is that $dom->saveXML() is returning an associative array, and not, in fact, a string - and this is implied by the fact that if I echo $xmlout, it only prints the values of the various nodes, but not the nodes themselves.

What is the simplest way to get a string representation of the XML which includes the names of all the nodes, etc?

Upvotes: 0

Views: 77

Answers (1)

Scott Saunders
Scott Saunders

Reputation: 30414

saveXML() returns a string, not an array. https://www.php.net/manual/en/domdocument.savexml.php

It has the content you're looking for. Make sure that you're looking at the string in source code view, NOT in your browser window. The browser will interpret all the tags as tags and not display them, just as it does not display <p> and other html tags.

Also, note that trim() will not remove whitespace between tags if it exists, only at the beginning and end of the string - not each line within the string.

Upvotes: 1

Related Questions