gpt4 book ai didi

javascript - 文本区域行应以 XML 结构显示

转载 作者:行者123 更新时间:2023-11-30 12:31:52 25 4
gpt4 key购买 nike

我有一个很小的要求,我想读取文本区域行并需要像 xml 格式一样显示它。我尝试使用以下代码,它成功地给出了带有 xml 结构的警报。我们必须以 xml 格式显示整行。这是我的代码

$(document).ready(function(){
$("#saveImgID").click(function(){
var xmlStart = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Rowsets DateCreated=\"2014-11-20T16:41:17\" EndDate=\"2014-11-20T16:41:17\" StartDate=\"2014-11-20T16:41:17\" Version=\"14.0 SP4 Patch 0 (Nov 22, 2013)" +">";
var xmlEnd = "</Rowset></Rowsets>";
var lines = $('#fileContent1').val().split(/\n/);
var texts = []
for (var i=0; i < lines.length; i++) {
if (/\S/.test(lines[i])) {
texts.push($.trim(lines[i]));
}
}
for (var i=0; i < texts.length; i++) {
alert(xmlStart+"<Rowset><Row><Line>"+texts[i]+"</Line></Row>"+xmlEnd);
}
});
});

我必须显示像

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Rowsets DateCreated="2014-11-20T16:41:17" EndDate="2014-11-20T16:41:17" StartDate="2014-11-20T16:41:17" Version="14.0 SP4 Patch 0 (Nov 22, 2013)>
<Rowset>
<Row>
<Line>101,abc,andy,1000</Line>
</Row>
<Row>
<Line>202,abc,sachin,2000</Line>
</Row>
<Row>
<Line>303,abc,smith,3000</Line>
</Row>
</Rowset>
</Rowsets>

这是 fiddle JsFiddle

最佳答案

只需在循环内构建 XML 并在循环外发出警报: http://jsfiddle.net/hy91m3tr/4/

$(document).ready(function(){
$("#saveImgID").click(function(){
var xmlStart = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Rowsets DateCreated=\"2014-11-20T16:41:17\" EndDate=\"2014-11-20T16:41:17\" StartDate=\"2014-11-20T16:41:17\" Version=\"14.0 SP4 Patch 0 (Nov 22, 2013)" +">";
var xmlEnd = "</Rowset></Rowsets>";
var xml;
var lines = $('#fileContent1').val().split(/\n/);
var texts = []
for (var i=0; i < lines.length; i++) {
if (/\S/.test(lines[i])) {
texts.push($.trim(lines[i]));
}
}
xml = xmlStart + "<Rowset>";
for (var i=0; i < texts.length; i++) {
xml += "<Row><Line>"+texts[i]+"</Line></Row>";
}
xml += xmlEnd;
alert(xml);
});
});

关于javascript - 文本区域行应以 XML 结构显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27477397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com