Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo an XML string to an HTML page for debugging?

Okay so I have a php script and I need to somehow view the value of one of my variables. The thing is this variable is a very long string of XML that got returned from a server. I know it has an error message in it but I need to actually see what it is saying. If I try and Print or echo the value it only displays part followed by a ... or if I use var_dump it does the same. I've even gone as far as trying to echo a javascript alert with the value but that fails because there are single and double quotes in the xml causing the alert quotes not to be recognized correctly. I just need to see the value of this variable. Any advice? Thanks.

Edit: Actually said that wrong. Echo and print don't display the value correctly because the tags are in <> brackets so it is recognizing as an html tag.

like image 805
jcmitch Avatar asked Nov 11 '11 20:11

jcmitch


People also ask

How do I echo an XML file?

The contents from a URL can be fetched through the file_get_contents() and it can be echoed. or read using the readfile function. readfile('http://example.com/'); header('Content-type: text/xml'); //The correct MIME type has to be set before displaying the output. echo $xml->asXML(); or $xml->asXML('filename.

How do you echo value in HTML?

The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

What is echo in HTML?

Using echo shorthand or separating HTML: PHP echo shorthand can be used to display the result of any expression, value of any variable or HTML markup.

How get data from XML URL in PHP?

$url = 'http://www.example.com'; $xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA); $url can be php file, as long as the file generate xml format data as output. Show activity on this post.


5 Answers

i usaly use:

echo nl2br(str_replace('<', '&lt;', $xml));

as its only the < that are a problem

like image 162
Puggan Se Avatar answered Oct 20 '22 22:10

Puggan Se


You can use htmlentities to output the XML string so that you can get a plaintext view of it in a browser.

<?php echo htmlentities( $xml_string); ?>

Alternatively, you can parse the XML string to reveal the error message, but this may be more complicated than what you need.

like image 25
nickb Avatar answered Oct 20 '22 23:10

nickb


Try echo htmlentities($var, ENT_COMPAT, 'UTF-8')

like image 28
simshaun Avatar answered Oct 20 '22 23:10

simshaun


i always use this:

echo "<pre>". htmlentities($s) . "</pre>";
like image 35
vvens Avatar answered Oct 20 '22 23:10

vvens


Try this:

echo '<pre>'.$xml_string.'</pre>';

See also: CDATA - (Unparsed) Character Data

like image 25
Nikolay Baluk Avatar answered Oct 20 '22 22:10

Nikolay Baluk