Hướng dẫn javascript format xml in textarea - định dạng javascript xml trong textarea

Dưới đây là một máy tính nhỏ có chứa nhỏ có tác dụng cho hầu hết các trường hợp thực hiện thụt lề tốt cho các dòng dài và tô màu cho đầu ra nếu cần thiết.

function formatXml(xml,colorize,indent) { 
  function esc(s){return s.replace(/[-\/&<> ]/g,function(c){         // Escape special chars
    return c==' '?' ':'&#'+c.charCodeAt(0)+';';});}            
  var sm='
',se='
',sd='
', sa='
',tb='
',tc='
', ind=indent||' ',sz='
',tz='
',re='',is='',ib,ob,at,i; if (!colorize) sm=se=sd=sa=sz=''; xml.match(/(?<=<).*(?=>)|$/s)[0].split(/>\s*').match(/^(<[!?\/]?)(.*?)([?\/]?>)$/s); // Split outer brackets ib=ob[2].match(/^(.*?)>(.*)<\/(.*)$/s)||['',ob[2],'']; // Split inner brackets at=ib[1].match(/^--.*--$|=|('|").*?\1|[^\t\n\f \/>"'=]+/g)||['']; // Split attributes if (ob[1]=='')+sz+sd+esc(ib[2])+sz+sm+esc('') is+=ind; // Increase indent }); return re; }

Đối với bản demo, xem https://jsfiddle.net/dkb0la16/

Thành công: function (dữ liệu) {

$ ('. Info2').

TheMITLicense(MIT) MIT License (MIT)
Copyright(c)2016StuartPowers (c) 2016 Stuart Powers
Permissionisherebygranted,freeofcharge,toanypersonobtainingacopy is hereby granted, free of charge, to any person obtaining a copy
ofthissoftwareandassociateddocumentationfiles(the"Software"),todeal this software and associated documentation files (the "Software"), to deal
intheSoftwarewithoutrestriction,includingwithoutlimitationtherights the Software without restriction, including without limitation the rights
touse,copy,modify,merge,publish,distribute,sublicense,and/orsell use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copiesoftheSoftware,andtopermitpersonstowhomtheSoftwareis of the Software, and to permit persons to whom the Software is
$ ('. Info2'). append ($ ('và định dạng phản hồi!')); to do so, subject to the following conditions:
}, above copyright notice and this permission notice shall be included in all
copiesorsubstantialportionsoftheSoftware. or substantial portions of the Software.
Lỗi: function () {$ ('. Info2'). SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM,, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Cũng có sẵn OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE..
functionformatXml(xml){ formatXml(xml) {
varformatted=''; formatted = '';
varreg=/(>)( reg = /(>)(<)(\/*)/g;
jQuery.each (xml.split ('\ r \ n'), function (index, node) = xml.replace(reg, '$1\r\n$2$3');
varpad=0; pad = 0;
{.each(xml.split('\r\n'), function(index, node) {
varindent=0; indent = 0;
if(node.match(/.+]*>$/)){ (node.match( /.+<\/\w[^>]*>$/ )) {
indent=0; = 0;
}elseif(node.match(/^ else if (node.match( /^<\/\w/ )) {
if(pad!=0){ (pad != 0) {
pad-=1; -= 1;
otherif (node.match (/^ [* [^^/THER >.*$/)))
Đệm+= ''; else if (node.match( /^<\w([^>]*[^\/])?>.*$/ )) {
indent=1; = 1;
}else{ else {
indent=0; = 0;
otherif (node.match (/^ [* [^^/THER >.*$/)))
varpadding=''; padding = '';
for(vari=0;i (var i = 0; i < pad; i++) {
otherif (node.match (/^ [* [^^/THER >.*$/))) += ' ';
otherif (node.match (/^ [* [^^/THER >.*$/)))
Đệm+= ''; += padding + node + '\r\n';
pad+=indent; += indent;
được định dạng+= đệm+nút+'\ r \ n';);
returnformatted; formatted;
otherif (node.match (/^ [* [^^/THER >.*$/)))
xml_raw='blahblahtralala'; = 'blahblahtralala';
xml_formatted=formatXml(xml_raw); = formatXml(xml_raw);
Đệm+= ''; = xml_formatted.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/ /g, ' ').replace(/\n/g,'
'
);
varmydiv=document.createElement('div'); mydiv = document.createElement('div');
mydiv.innerHTML=xml_escaped;.innerHTML = xml_escaped;
document.body.appendChild(mydiv);.body.appendChild(mydiv);

$ ('. Info2').

JavaScript XML Pretty Print - Stuart Powershtml>
JavaScript XML Pretty Print - Stuart Powershead>
Textarea {title>Javascript xml pretty print - Stuart Powerstitle>
JavaScript XML Pretty Print - Stuart Powersscript src="http://bit.ly/jqymin">script>
JavaScript XML Pretty Print - Stuart Powersstyle>
Chiều rộng: 80%; {
Chiều cao: 50%;: 80%;
tràn-x: cuộn;: 50%;
Overflow-y: Cuộn;: scroll;
Hiển thị: Khối;: scroll;
Biên giới: 1px màu đen rắn;: block;
Đệm: 5px;: 1px solid black;
Biên độ: 5px;: 5px;
}: 5px;
xml = xml.replace (reg, '$ 1 \ r \ n $ 2 $ 3');
JavaScript XML Pretty Print - Stuart Powersstyle>
JavaScript XML Pretty Print - Stuart Powersscript>
functionformatXml(xml){ formatXml(xml) {
varformatted=''; formatted = '';
varreg=/(>)( reg = /(>)(<)(\/*)/g;
jQuery.each (xml.split ('\ r \ n'), function (index, node) = xml.replace(reg, '$1\r\n$2$3');
varpad=0; pad = 0;
{.each(xml.split('\r\n'), function(index, node)
}
varindent=0; indent = 0;
if(node.match(/.+]*>$/)) (node.match( /.+<\/\w[^>]*>$/ ))
}
indent=0; = 0;
otherif (node.match (/^ [* [^^/THER >.*$/)))
elseif(node.match(/^ if (node.match( /^<\/\w/ ))
}
if(pad!=0) (pad != 0)
}
pad-=1; -= 1;
otherif (node.match (/^ [* [^^/THER >.*$/)))
otherif (node.match (/^ [* [^^/THER >.*$/)))
Đệm+= ''; if (node.match( /^<\w[^>]*[^\/]>.*$/ ))
}
indent=1; = 1;
otherif (node.match (/^ [* [^^/THER >.*$/)))
else
}
indent=0; = 0;
otherif (node.match (/^ [* [^^/THER >.*$/)))
varpadding=''; padding = '';
for(vari=0;i (var i = 0; i < pad; i++)
}
otherif (node.match (/^ [* [^^/THER >.*$/))) += ' ';
otherif (node.match (/^ [* [^^/THER >.*$/)))
Đệm+= ''; += padding + node + '\r\n';
pad+=indent; += indent;
được định dạng+= đệm+nút+'\ r \ n';);
returnformatted; formatted;
otherif (node.match (/^ [* [^^/THER >.*$/)))
$(function(){(function(){
varurl="http://careers.stackoverflow.com/jobs/feed?searchTerm=python&location=02144&range=30"; url = "http://careers.stackoverflow.com/jobs/feed?searchTerm=python&location=02144&range=30";
$.ajax({.ajax({
Đệm+= '';: url,
dataType:"text",:"text",
được định dạng+= đệm+nút+'\ r \ n';:function(){$('.info').append($('

requesting: '+url+'

'
));},
});:function(){$('.info').append($('

error! '+url+'

'
));},
URL: URL,: function(data) {
$('.unformatted').text(data);('.unformatted').text(data);
otherif (node.match (/^ [* [^^/THER >.*$/)))
được định dạng+= đệm+nút+'\ r \ n';);
$.ajax({.ajax({
Đệm+= '';: url,
dataType:"text",:"text",
beforeSend:function(){:function(){
được định dạng+= đệm+nút+'\ r \ n';('.info2').append($('

requesting '+url+'

'
));
});('.info2').append($('

and formatting the response!'));

URL: URL,,
Beforesend: function () {$ ('. Info').:function(){$('.info2').append($('

error! '+url+'

'
));},
Thành công: function (dữ liệu) {: function(data) {
xml_neat=formatXml(data); = formatXml(data);
$('.formatted').text(xml_neat);('.formatted').text(xml_neat);
}
}););
}););
JavaScript XML Pretty Print - Stuart Powersscript>
JavaScript XML Pretty Print - Stuart Powershead>
JavaScript XML Pretty Print - Stuart Powersbody>
JavaScript XML Pretty Print - Stuart Powerscode>
Cũng có sẵn<a href="http://sente.cc/misc/javascript_xml_pretty_print.html">
http://sente.cc/misc/javascript_xml_pretty_print.html
JavaScript XML Pretty Print - Stuart Powersa>
JavaScript XML Pretty Print - Stuart Powerscode>
JavaScript XML Pretty Print - Stuart Powerspre class="info">pre>
Cũng có sẵnh3>unformattedh3>
JavaScript XML Pretty Print - Stuart Powerstextarea class="unformatted">textarea>
Cũng có sẵnh3>formattedh3>
JavaScript XML Pretty Print - Stuart Powerstextarea class="formatted">textarea>
JavaScript XML Pretty Print - Stuart Powersbody>
JavaScript XML Pretty Print - Stuart Powershtml>