gpt4 book ai didi

javascript - 将表数据转换为xml结构格式?

转载 作者:行者123 更新时间:2023-12-03 07:59:01 26 4
gpt4 key购买 nike

我坚持使用 xml 字符串准备。基本上我有一张表,包含一些行。如果我点击提交,我们需要显示 xml 格式的字符串,如下所示

<OrderSummaryIDs>
<Count>2</Count>
<Row>
<StartDate>08/01/2015</StartDate>
<EndDate>12/01/2015</EndDate>
<remark/>Test1</remark>
</Row>
</OrderSummaryIDs>

我尝试使用下面的代码,它不返回 xml 结构格式,

这是我的代码

var xmlStart =  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
var xml = xmlStart;
var xmlFirstRowSet = "<OrderSummaryIDs>";
var xmlLast="</OrderSummaryIDs>";

xmlFirstRowSet = xmlFirstRowSet;
var headerRowSet='';
var bodyRowSet ='';

var elements = $(".mutliple tr");
var size = elements.size();
bodyRowSet = bodyRowSet + "<Count>" + size + "</Count>";

bodyRowSet = bodyRowSet + "<Row>";
elements.each(function() {
var tdValue1 = $td.closest('table').find('td:first-child').html();
bodyRowSet = bodyRowSet + "<StartDate>";
bodyRowSet = bodyRowSet + tdValue1;
bodyRowSet = bodyRowSet + "</StartDate>";

/*var tdValue2 = $td.closest('table').find('td:eq(1)').html();
bodyRowSet = bodyRowSet + "<EndDate>";
bodyRowSet = bodyRowSet + tdValue2;
bodyRowSet = bodyRowSet + "</EndDate>";

var tdValue3 = $td.closest('table').find('td:last-child').html();
bodyRowSet = bodyRowSet + "<remark>";
bodyRowSet = bodyRowSet + tdValue3;
bodyRowSet = bodyRowSet + "</remark>";*/
});

bodyRowSet = bodyRowSet + "</Row>";
xml = xmlStart + xmlFirstRowSet + bodyRowSet+xmlLast;
alert(xml);

Fiddle

最佳答案

试试这个:

var elements = $(".mutliple tr");
var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
xml += '<OrderSummaryIDs><Count>' + elements.length + '</Count>';
elements.each(function() {
var tdValue1 = $(this).find('td:first-child').html();
xml += "<row><StartDate>" + tdValue1 + "</StartDate>";
//
//
//
xml += '</row>';
}
xml += '</OrderSummaryIDs>';
alert(xml);

关于javascript - 将表数据转换为xml结构格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34676787/

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