2016-08-05 57 views
0

我使用下面的代碼設法將xml響應轉換爲角度1中的json格式。但我怎麼能使用類似的代碼在angular2,angular2 transformRequest xml轉換爲json轉換的用法

var xml = function() { 
     $http.get("./app/sampleXML.xml", 
       { 
        transformResponse: function (cnv) { 
         var x2js = new X2JS(); 
         var aftCnv = x2js.xml_str2json(cnv); 
         return aftCnv; 
        } 
       }) 
     .success(function (response) { 
      console.log(response); 
     }); 
    } 

Angular2:

getXml() { 
    this.http.get('../../jsonConfig/sampleXML.xml') 
    .map(response => response.text()) 
    .subscribe(data => { 
     if(data) { 
      console.log(data); 
     } 
    }); 

} 

回答

0

這是對我工作:

xml_str2json(extractXML(data.text(),'string1','string2')) 

其中xml2json來自於以下幾個庫:

https://github.com/abdmob/x2js

extractXML是一個字符串函數,它使我能夠從SOAP響應中提取xml主體。

我找不到可應用於Response的方法的任何文檔。

希望這可能有所幫助。

乾杯。