php 如何将 XML 字符串回显到 HTML 页面以进行调试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8099507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:57:50  来源:igfitidea点击:

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

phpxmldebuggingescaping

提问by jcmitch

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.

好的,所以我有一个 php 脚本,我需要以某种方式查看我的一个变量的值。问题是这个变量是从服务器返回的很长的 XML 字符串。我知道其中有一条错误消息,但我需要实际看看它在说什么。如果我尝试打印或回显值,它只显示部分后跟一个 ... 或者如果我使用 var_dump 它会做同样的事情。我什至试图用该值回显 javascript 警报,但失败了,因为 xml 中存在单引号和双引号,导致警报引号无法正确识别。我只需要查看这个变量的值。有什么建议吗?谢谢。

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.

编辑:实际上说错了。Echo 和 print 无法正确显示值,因为标签位于 <> 括号中,因此它被识别为 html 标签。

回答by nickb

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

您可以使用htmlentities输出 XML 字符串,以便您可以在浏览器中获得它的纯文本视图。

<?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.

或者,您可以解析 XML 字符串以显示错误消息,但这可能比您需要的更复杂。

回答by simshaun

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

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

回答by wens

i always use this:

我总是用这个:

echo "<pre>". htmlentities($s) . "</pre>";

回答by Nikolay Baluk

Try this:

尝试这个:

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

See also: CDATA - (Unparsed) Character Data

另请参阅: CDATA -(未解析的)字符数据

回答by Puggan Se

i usaly use:

我通常使用:

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

as its only the < that are a problem

因为它唯一的 < 是一个问题

回答by Jürgen Thelen

You could just save the XML string to a file. If it's well-formed XML, you can view it with every browser (and expand/collapse nodes ^^).

您可以将 XML 字符串保存到文件中。如果它是格式良好的 XML,您可以使用每个浏览器查看它(并展开/折叠节点 ^^)。