我有一个包含大量数据的静态表。我想用JavaScript删除数据并用结果创建XML结果。
表样本:
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">Sn</td>
<td width="200">Item</td>
<td width="500">Discription</td></tr>
<tr>
<td>1</td>
<td width="200">Item 1</td>
<td>this is lenghty item discription</td>
</tr>
创建了预期的XML结果:
<content>
<sn>1</sn>
<item>Item1</item>
<discription>this is lenghty item discription</discription>
</content>
...
有人可以提供一个简单的JS代码供我使用。感谢
答案 0 :(得分:1)
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">Sn</td>
<td width="200">Item</td>
<td width="500">Discription</td></tr>
<tr>
<td>1</td>
<td width="200">Item 1</td>
<td>this is lenghty item discription</td>
</tr>
...解
var content = [];
$("table tr").each(function(){
var self = this;
content.push({
'content': {
'sn': $(self).find('td:first-child').text(),
'item': $(self).find('td:nth-child(2)').text(),
'description: $(self).find('td:nth-child(3)').text()
}
})
});
var xml = X2JS.json2xml_str(content);
上述解决方案使用x2js库。
expected XML result created:
<content>
<sn>1</sn>
<item>Item1</item>
<discription>this is lenghty item discription</discription>
</content>