Tôi đang cố gắng chuyển đổi một chuỗi XML mà tôi nhận được từ một máy chủ sang JSON bên trong hàm Lambda của tôi.
Tôi đã thiết lập ví dụ khá đơn giản này để mô phỏng câu trả lời XML mà tôi nhận được từ máy chủ sử dụng DynamoDB. (Hiện nay Tôi chỉ cố gắng để có được những chuyển đổi xảy ra)Phân tích cú pháp XML thành JSON trong Amazon Lambda bằng cách sử dụng các thư viện js bên ngoài
'use strict';
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});
exports.handler = function (e, ctx, callback){
let table = "dsbTable";
let bpNumber = 1337;
var test;
var x2js = new X2JS();
let params = {
TableName: table,
Key:{
"bpNumber": bpNumber
},
};
docClient.get(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
callback(err, null);
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
console.log('test' +data.Item.getBp);
//var jsonObj = x2js.xml_str2json(data.Item.getBp);
//console.log(jsonObj);
callback(null, data);
}
});
} ;
nhận được hàng chỉ hoạt động tốt và được trả về như
{
"Item": {
"getBp": "<message version=\"1.0\" system=\"AVS/3\"><header><client>553</client><avs3-sales-organization>7564</avs3-sales-organization><avs3-service-provider>DSD</avs3-service-provider></header><body><business-partner><salutation-code>01</salutation-code><titel-code-academic/><titel-academic/><titel-code-royal/><titel-royal/><job-titel/><last-name1>Pickle</last-name1><last-name2/><first-name>N</first-name><street/><street-suffix/><street-number/><street-number-suffix/><address-line-1>10 Waterside Way</address-line-1><address-line-2/><address-line-3/><zipcode>NN4 7XD</zipcode><country-code>GB</country-code><city>NORTHAMPTON</city><district/><region-code>NH</region-code><region-text>Northamptonshire</region-text><company1/><company2/><company3/><department/><po-box/><po-box-zipcode/><po-box-city/><po-box-country-code/><major-customer-zipcode/><address-source/><advertisement>Y</advertisement><category/><bp-number>1100000772</bp-number><bp-number-external/><bp-group>ABON</bp-group><eu-sales-tax-number/><bic-master-number/><sector/><communication><communication-type>WW</communication-type><communication-value>[email protected]</communication-value><communication-default>Y</communication-default></communication><attribute><attribute-type>ACC</attribute-type><attribute-value>Y</attribute-value></attribute><attribute><attribute-type>OIEMEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OINLIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OISMEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OISMIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOEMIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOFXEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOFXIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOPTEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOPTIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOTEEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOTEIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>THEDSU</attribute-type><attribute-value/></attribute></business-partner></body></message>",
"bpNumber": 1337
}
}
vấn đề chính của tôi này bây giờ là tôi không thể tìm ra cách tôi có thể nhập bất kỳ tệp thư viện XMLtoJSON nào như this one here
Tôi hy vọng mã của tôi trong trường hợp này không hoàn toàn vô giá trị và có một giải pháp khá đơn giản.
'var jsonObj = x2js.xml_str2json (data.Item.getBp)' không hoạt động? Tôi nghĩ bạn không chỉ tham chiếu đến các tệp JS. –
Trước hết, cảm ơn bạn đã dành thời gian. yea, đó có lẽ là lý do. nhưng tôi không thể tìm ra cách tham chiếu nó bên trong hàm lambda của tôi. Im rất mới cho toàn bộ điều AWS. và javascript cho rằng vấn đề ... Tôi có lẽ sẽ phải thử một cái gì đó như thế này cho Lambda để có thể truy cập vào các thư viện bên ngoài http://docs.aws.amazon.com/de_de/lambda/latest/dg/nodejs- create-deployment-pkg.html. Tôi đã thử chỉ dán mã từ các thư viện xml2json bên trong chức năng Lambda của tôi nhưng điều đó chỉ ném tấn lỗi: -/ –
Kiểm tra liên kết này: http://stackoverflow.com/questions/950087/how-to-include-a -javascript-file-in-another-javascript-file –