Reputation: 12431
I am using PHP to parse an XML response,
After some debugging, I have found a strange occurrence with my application. This is what I've been doing, I make the request and then I echo the entire response. The response is as-expected XML. When I copy this entire document and paste it into a php script which then runs simplexml_load_string on it, it spits out a Simple XML object like you would expect.
When I run it from the actual request, I print the response, I can see it, looks like normal XML. But then when I load the xml string using simplexml_load_string, it prints nothing at all. I have tried a few variations but every object it returns is just empty. The script is running because I echo "Done" at the end of the script. I have tried each of these:
$xml = simplexml_load_string(htmlspecialchars_decode($response_body));
$xml = simplexml_load_string($response_body);
$xml = new SimpleXMLElement($response_body);
The first one I've tried (with htmlspecialchars_decode) is because, for some reason, the unedited response, prints with <
signs instead of '<' signs. Any suggestions, or advice would be hugely helpful!
By the way, I am printing the object like this:
print_r($xml);
Upvotes: 0
Views: 8400
Reputation: 2156
The reason that it's not printing with 'echo', is that echo
prints a variable.
The simplexml_load_string is a built in function that turns an output of an XML function into an object.
You can not use echo
to print an object. print_r()
is a better option to print objects.
Upvotes: 1
Reputation: 82028
You're probably looking for the asXML function.
SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element
You mentioned below that the object is returning empty from the pieces of code posted in the question. Have you tried var_dump? I suspect that you're actually getting the return of 'FALSE'. That will print out as nothing and it happens when you have malformed XML and you try to pass it through the code in your question.
Here are a couple of steps you can try:
&tl;
where you really want a <
. Either that or you have an "
when you want "
. CTRL-U should show you that.Upvotes: 1
Reputation: 668
You can convert your xml object into array and then use like an array.
Example :
$xml = simplexml_load_file('path/to/xml');
function toarray($data) {
if (is_object($data)) $data = get_object_vars($data);
return (is_array($data)) ? array_map(__FUNCTION__,$data) : $data;
}
$dat = toarray($xml);
Now $dat
becomes array and use it like
echo $dat['key_name'];
Hope it may help to get your result. Thank You CHeers...!!
Upvotes: 1
Reputation: 10268
Use instead:
var_dump($xml);
It will show you what type the variable is. This way you will know if the variable is false
or null
, which print_r
does not. If the variable is null
or false
it won't print anything.
So, If you get an SimpleXML
object all is fine, if you however get false
then the parsing encountered an error. To being able to figure out what went wrong you need to turn on E_WARNING
, since simplexml_load_string
produces a warning and not a error.
Upvotes: 2