我正在尝试解析从我们的系统生成的XML文件,其中生成了多个子元素,但它们是“在一起”,因为它们具有相同的子元素。
了解我粘贴在下面的部分xml代码...
每条记录都由它的订单#标识,然后它包含该货件的信息,最后列出了该订单的单个项目及其规格。
<forward_agent>
<record>
<order>562490</order>
<mfg._del_date>11-19-2008</mfg._del_date>
<forw.agent>263</forw.agent>
<customer>120</customer>
<name>BLUEBERRY AUBURN HILLS</name>
<item>'28.1461.00</item>
<ordered>1.0000</ordered>
<warehouse>001</warehouse>
<desitnation_city_state>AUBURN HILLS MI</desitnation_city_state>
<weight>60.1201</weight>
<size>9.70</size>
</record>
<record>
<order>562490</order>
<mfg._del_date>11-19-2008</mfg._del_date>
<forw.agent>263</forw.agent>
<customer>120</customer>
<name>BLUEBERRY AUBURN HILLS</name>
<item>'545</item>
<ordered>1.0000</ordered>
<warehouse>002</warehouse>
<desitnation_city_state>AUBURN HILLS MI</desitnation_city_state>
<weight>82.0120</weight>
<size>12.86</size>
</record>
<record>
<order> </order>
<mfg._del_date> </mfg._del_date>
<forw.agent> </forw.agent>
<customer> </customer>
<name> </name>
<item>'</item>
<ordered> </ordered>
<warehouse> </warehouse>
<desitnation_city_state>Total Lbs:</desitnation_city_state>
<weight>1658.1342</weight>
<size>199.36</size>
</record>
<record>
<order>562136</order>
<mfg._del_date>11-19-2008</mfg._del_date>
<forw.agent>263</forw.agent>
<customer>133</customer>
<name>BLUEBERRY ALBUQUERQUE</name>
<item>'4635</item>
<ordered>2.0000</ordered>
<warehouse>002</warehouse>
<desitnation_city_state>EL PASO TX</desitnation_city_state>
<weight>23.9863</weight>
<size>4.00</size>
</record>
<record>
<order>562136</order>
<mfg._del_date>11-19-2008</mfg._del_date>
<forw.agent>263</forw.agent>
<customer>133</customer>
<name>BLUEBERRY ALBUQUERQUE</name>
<item>'5590</item>
<ordered>1.0000</ordered>
<warehouse>002</warehouse>
<desitnation_city_state>EL PASO TX</desitnation_city_state>
<weight>0.0000</weight>
<size>0.00</size>
</record>
<record>
<order>562136</order>
<mfg._del_date>11-19-2008</mfg._del_date>
<forw.agent>263</forw.agent>
<customer>133</customer>
<name>BLUEBERRY ALBUQUERQUE</name>
<item>'5591</item>
<ordered>1.0000</ordered>
<warehouse>002</warehouse>
<desitnation_city_state>EL PASO TX</desitnation_city_state>
<weight>436.0082</weight>
<size>96.67</size>
</record>
<record>
<order> </order>
<mfg._del_date> </mfg._del_date>
<forw.agent> </forw.agent>
<customer> </customer>
<name> </name>
<item>'</item>
<ordered> </ordered>
<warehouse> </warehouse>
<desitnation_city_state>Total Lbs:</desitnation_city_state>
<weight>5093.0928</weight>
<size>613.88</size>
</record>
我希望输出按Order#分组,看起来类似于:
Item Ordered Weight Size
1221 1 320.6734 31.36
1601 1 34.0724 11.42
2122 1 86.0023 12.79
5543.SP 1 1075.254 121.23
28.1461.00 1 60.1201 9.7
545 1 82.012 12.86
Total Lbs: 1658.1342 199.36
最终,我可以根据总重量和尺寸或特定项目来分类订单。
--- --- EDIT
现在显示输入框,但它们没有充满任何东西。
当前的js代码
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "b2.xml",
dataType: "xml",
success: function(response) { parseXML(response); }
});
});
var recordList =[];
function parseXML(xml) {
$(xml).find('record').each(function () {
var entry = new Object();
entry.order= $(this).find('order').text();
entry.delDate= $(this).find('mfg._del_date').text();
entry.forw = $(this).find('forw.agent').text();
entry.customer= $(this).find('customer').text();
//
//
recordList.push(entry);
});
send(); // handles xml after parsed
}
function send(){
// for each row of a table
for(var i = 0; i < $("#table tr").size(); i++){
$('#orderNum' + i).val(recordList[i].order);
$('#delDate' + i).val(recordList[i].delDate);
$('#forw' + i).val(recordList[i].forw);
$('#customer' + i).val(recordList[i].customer);
}
}
当前的HTML代码
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="parser.js"></script>
</head>
<body>
<table>
<tr>
<td>Order #</td>
<td>Delete Date</td>
<td>Forward</td>
<td>Customer</td>
</tr>
<tr>
<td><input id="orderNum0" type="text" /></td>
<td><input id="delDate0" type="text" /></td>
<td><input id="forw0" type="text" /></td>
<td><input id="customer0" type="text" /></td>
</tr>
</table>
</body>
</html>
以下是截图:
http://s16.postimage.org/rd1uahm4j/localhost_ftl_parser_html.png
答案 0 :(得分:0)
<强> Record.js 强>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "record.xml",
dataType: "xml",
success: function(xml){
parseXML(xml);
},
error: function(response, textStatus, errorThrown){
console.log(textStatus, errorThrown);}
});
});
var recordList =[];
function parseXML(xml) {
$(xml).find('record').each(function () {
var entry = new Object();
entry.order= $(this).find('order').text();
entry.delDate= $(this).find('mfg').text();
entry.forw = $(this).find('forw').text();
entry.customer = $(this).find('customer').text();
entry.name = $(this).find('name').text();
entry.item = $(this).find('item').text();
entry.ordered = $(this).find('ordered').text();
entry.warehouse = $(this).find('warehouse').text();
entry.destination = $(this).find('desitnation_city_state').text();
entry.weight = $(this).find('weight').text();
entry.size = $(this).find('size').text();
recordList.push(entry);
});
send(); // handles xml after parsed
}
function send(){
// for each row of a table
addRows();
for(var i = 0; i < recordList.length; i++){
$('#orderNum' + i).val(recordList[i].order);
$('#delDate' + i).val(recordList[i].delDate);
$('#forw' + i).val(recordList[i].forw);
$('#customer' + i).val(recordList[i].customer);
$('#name'+i).val(recordList[i].name);
$('#item'+i).val(recordList[i].item);
$('#ordered'+i).val(recordList[i].ordered);
$('#warehouse'+i).val(recordList[i].warehouse);
$('#destination'+i).val(recordList[i].destination);
$('#weight'+i).val(recordList[i].weight);
$('#size'+i).val(recordList[i].size);
}
}
function addRows(){
for(var i = 1; i < recordList.length; i++){
var newField = "<tr><td><input id=\"orderNum"+i+"\" size=\"10\" type=\"text\" </td>"+
"<td><input id=\"delDate"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"forw"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"customer"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"name"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"item"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"ordered"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"warehouse"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"destination"+i+"\" type=\"text\"</td>"+
"<td><input id=\"weight"+i+"\" size=\"10\" type=\"text\"</td>"+
"<td><input id=\"size"+i+"\" size=\"10\" type=\"text\"</td></tr>"
$('#tableId').append(newField);
}
}
<强> Record.html 强>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="records.js"></script>
</head>
<body>
<table id="tableId">
<tr>
<td>Order #</td>
<td>Delete Date</td>
<td>Forward</td>
<td>Customer</td>
<td>Name</td>
<td>Item</td>
<td>Ordered</td>
<td>Warehouse</td>
<td>Destination</td>
<td>Weight</td>
<td>Size</td>
</tr>
<tr>
<td><input id="orderNum0" size="10" type="text" /></td>
<td><input id="delDate0" size="10" type="text" /></td>
<td><input id="forw0" size="10" type="text" /></td>
<td><input id="customer0" size="10" type="text" /></td>
<td><input id="name0" size="10" type="text" /></td>
<td><input id="item0" size="10" type="text" /></td>
<td><input id="ordered0" size="10" type="text" /></td>
<td><input id="warehouse0"size="10" type="text" /></td>
<td><input id="destination0" type="text" /></td>
<td><input id="weight0"size="10" type="text" /></td>
<td><input id="size0" size="10" type="text" /></td>
</tr>
</table>
</body>
</html>
只有一个问题,那就是您的xml条目名称为forw.agent和mfg.del_date ..您必须转义其中一些字符,但这会添加行并为这些表添加值。这是一个粗糙的页面,你可以自由地改变你的东西......尤其是输入框,你可以将它们改为标签或者你有什么......但是这会解析并显示......按照要求