1. 从后台读取xml数据,读取结果,话不多说,直接上代码
var kk = "<root><satellite>GF3</satellite><sensor>SAR</sensor><level>LEVEL1A</level><startTime>2017-01-20 11:02:34.571182</startTime><endTime>2017-01-20 11:02:43.420619</endTime><cloudPercent>0</cloudPercent><gsd>5</gsd><topLeftLat>34.977452</topLeftLat><topLeftLong>105.190206</topLeftLong><topRightLat>35.058405</topRightLat><topRightLong>105.773972</topRightLong><bottomRightLat>34.484358</bottomRightLat><bottomRightLong>105.890447</bottomRightLong><bottomLeftLat>34.403121</bottomLeftLat><bottomLeftLong>105.310854</bottomLeftLong></root>"
xml2js.parseString(kk, function (err, result) {
console.dir(result.root.bottomLeftLat[0]); // Prints JSON object!
});
结果如下图:
2. 前端json数据转换成xml数据传到后台
var mm = {
root: {
bottomLeftLat: ["34.403121"],
bottomLeftLong: ["105.310854"],
bottomRightLat: ["34.484358"],
bottomRightLong: ["105.890447"],
cloudPercent: ["0"],
endTime: ["2017-01-20 11:02:43.420619"],
gsd: ["5"],
level: ["LEVEL1A"],
satellite: ["GF3"],
sensor: ["SAR"],
startTime: ["2017-01-20 11:02:34.571182"],
topLeftLat: ["34.977452"],
topLeftLong: ["105.190206"],
topRightLat: ["35.058405"],
topRightLong: ["105.773972"]
}
}
var outxml = builder.buildObject(mm).toString();
结果如下图所示: