如何在JavaScript中打印漂亮的XML? [英] How to print pretty xml in javascript?

查看:55
本文介绍了如何在JavaScript中打印漂亮的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用JavaScript漂亮打印xml的最佳方法是什么?我通过ajax调用获取xml内容,并在textarea中显示此请求之前,我想对其进行格式化,以使它看起来不错:)

What's the best way to pretty-print xml in JavaScript? I obtain xml content through ajax call and before displaying this request in textarea i want to format it so it looks nice to the eye :)

推荐答案

这不会涉及任何缩进,但有助于对XML进行编码,以供在< pre> 中使用< textarea> 标签:

This does not take care of any indenting, but helps to encode the XML for use within <pre> or <textarea> tags:

/* hack to encode HTML entities */
var d = document.createElement('div'); 
var t = document.createTextNode(myXml); 
d.appendChild(t);
document.write('<pre>' + d.innerHTML + '</pre>');

如果您想突出显示并且节点可折叠/展开,而不是< textarea> ,请参阅.

And if, instead of a <textarea>, you'd want highlighting and the nodes to be collapsable/expandable, then see Displaying XML in Chrome Browser on Super User.

这篇关于如何在JavaScript中打印漂亮的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆