angular6 xml文件解析成json數據,json數據生成xml

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();

結果如下圖所示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章