How to print beautiful xml in javascript?

What is the best way to beautifully print xml in JavaScript? I get xml content through an ajax call and before displaying this request in textarea I want to format it so that it looks beautiful for the eyes :)

+3
source share
5 answers

This does not require indentation, but helps to code XML for use in tags <pre>or <textarea>:

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

And if instead <textarea>you want to highlight, and the nodes should be resettable / expandable, see XML mapping in Chrome browser to Super User.

+4
source

take a look at vkBeautify.js plugin

http://www.eslinstructor.net/vkbeautify/

, . javascript, 1,5 . : 5 . XML- 50K.

+5

, XML- RegExp " > " node , RegExp , .

XSLT javascript.
.

+1

Arjan <pre>. "" xml- html, 2 . .

+1

prettydiff.com/markup_beauty.js. , JSTL.

<c:out value="<strong>text</strong>"/>

- prettydiff.com. "beautify" "markup".

It is important that you use the right tool to decorate your XML, and not to rush your work arbitrarily. Otherwise, you add space markers where they are not intended, and delete them where they were intended. For raw data, this may be indirect, but for human consumer content, it destroys the integrity of your code, especially with respect to recursion.

+1
source

Source: https://habr.com/ru/post/1713634/


All Articles