2017-04-22 133 views
0

我是angular2的新手,希望將XML轉換爲json。我使用get方法來接收它。每當我直接在json接收數據時發佈我。Angular2:獲取XML數據並將其轉換爲JSON

請幫助我的工作示例。下面是我的XML

<?xml version="1.0"?> 
 
<catalog> 
 
    <book id="bk101"> 
 
     <author>Gambardella, Matthew</author> 
 
     <title>XML Developer's Guide</title> 
 
     <genre>Computer</genre> 
 
     <price>44.95</price> 
 
     <publish_date>2000-10-01</publish_date> 
 
     <description>An in-depth look at creating applications 
 
     with XML.</description> 
 
    </book> 
 
    <book id="bk102"> 
 
     <author>Ralls, Kim</author> 
 
     <title>Midnight Rain</title> 
 
     <genre>Fantasy</genre> 
 
     <price>5.95</price> 
 
     <publish_date>2000-12-16</publish_date> 
 
     <description>A former architect battles corporate zombies, 
 
     an evil sorceress, and her own childhood to become queen 
 
     of the world.</description> 
 
    </book> 
 
    <book id="bk103"> 
 
     <author>Corets, Eva</author> 
 
     <title>Maeve Ascendant</title> 
 
     <genre>Fantasy</genre> 
 
     <price>5.95</price> 
 
     <publish_date>2000-11-17</publish_date> 
 
     <description>After the collapse of a nanotechnology 
 
     society in England, the young survivors lay the 
 
     foundation for a new society.</description> 
 
    </book> 
 
    <catalog>

+0

你說的'問題,我在JSON directly.'每次我收到的數據,如果你已經收到JSON數據,爲什麼你需要解析XML? – Dummy

+0

https://github.com/Leonidas-from-XIV/node-xml2js – cartant

+0

@Dummy這次我想出了具有XML數據而不是JSON的Web服務 – Jay

回答

0
function parse(xmlDoc) { 
    var result = { catalog: [] }; 
    var books = xmlDoc.getElementsByTagName("book"); 
    for (var i = 0; i < books.length; i++) { 
     var book = {}; 
     book.id = books[i].getAttribute("id"); 
     book.author = books[i].getElementsByTagName("author")[0].textContent; 
     book.title = books[i].getElementsByTagName("title")[0].textContent; 
     book.author = books[i].getElementsByTagName("author")[0].textContent; 
     book.genre = books[i].getElementsByTagName("genre")[0].textContent; 
     book.price = books[i].getElementsByTagName("price")[0].textContent; 
     book[publish_date] = books[i].getElementsByTagName("publish_date")[0].textContent; 
     book.description = books[i].getElementsByTagName("description")[0].textContent; 
     result.catalog.push(book); 
    } 
    return result; 
} 
0

我寧願與NPM模塊,而不是使用Javascript,因爲

這是打字稿& JavaScript不 同時擔任該會的工作,但在去做一個構建或分配它不會工作,因爲它會給出錯誤xml2json沒有被定義,因爲它只在javascript中可用&不在TS或在轉譯時(發生在我的情況) 我做了這樣的事情。

var parser = new xml2js.Parser({explicitArray : false}); 
//used xml2js parser from npm (https://www.npmjs.com/package/xml2js) 
//and in my service i used this 
this.http.get(this.newsURL) 
       .flatMap(res=>{ 
         return Observable.fromPromise(this.getJSON(res.text())) 
       }) 
       .catch((error:any) => Observable.throw(error.json().error || 'Server error')); 

編輯:

this.getJSON()是其將XML的()的函數> JSON。

getJSON(xmlText){ 
    return new Promise(function(resolve,reject){ 
     parser.parseString(xmlText,function(err,result){ 
      if(!err){ 
       resolve(result.rss.channel.item); 
      }else{ 
       reject(err); 
      } 
     }) 
    }) 
    } 

而且需要進口

var xml2js = require('xml2js'); 
var parser = new xml2js.Parser({explicitArray : false}); 
+0

我應該在哪裏寫這段代碼「var parser = new xml2js.Parser({explicitArray:false});「 – Jay

+0

@Jay:在你的http調用返回XML的服務方法。這裏'this.newsURL'是http URL。 –

+0

我應該通過那裏面的任何外觀事件像ngOnInit – Jay

相關問題