如何解析XML在2角 [英] How to parse xml in Angular 2

查看:176
本文介绍了如何解析XML在2角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是使用XML而不是JSON的API。如何以下XML转换成JSON任何建议或如何正确地在ngFor指令中使用的数据?

此外,也可观察到的是合适的吗?

 <病例文件>
  <序列号> 123456789< /序列号>
    <交易日期和GT; 20150101< /交易的日期与GT;
      <案件档案的头>
       <申请日> 20140101< /申请日>
      < /箱文件头>
 // ...
   <&分类GT;
  <分类>
   < international- code-总-NO→1< / international- code-总-NO>
   <主体 - code取代; 025< /主体 - code>
  < /分类>
 < /分类>
 < /区分文件>
 <病例文件>
     <序列号> 234567890< /序列号>
    <交易日期和GT; 20160401< /交易的日期与GT;
      <案件档案的头>
       <申请日> 20160401< /申请日>
      < /箱文件头>
// ...
   <&分类GT;
  <分类>
   < international- code-总-NO→1< / international- code-总-NO>
   <主体 - code取代; 042< /主体 - code>
  < /分类>
 < /分类>
< /区分文件>


 出口类apiService {
   构造函数(私人HTTP:HTTP){}   私人_apiUrl ='应用程序/ API';   getCaseFile(){
     返回this.http.get(this._apiUrl)
//转换到这里JSON?
                    .MAP(RES =>< CaseFile []方式> res.json()数据)
                     .catch(this.handleError);
   }
    私人的HandleError(错误:响应){     console.error(错误);
    返回Observable.throw(error.json()错误||'服务器错误);
   }
 }< D​​IV * ngFor =#案卷的CF> {{案例file.serial数}}< / DIV>


解决方案

根据图书馆的http:/ /goessner.net/download/prj/jsonxml/ ,我实现了一个样本接收XML数据,并分析它们到Angular2应用程序:

  VAR标题=新的报头();
(......)
headers.append(接受,应用程序/ XML');返回this.http.get('https://angular2.apispark.net/v1/companies/',{
  标题:标题
})地图(RES => JSON.parse(xml2json(res.text(),'')));

要能够使用图书馆,你需要先来解析XML字符串:

  VAR parseXml;如果(typeof运算window.DOMParser!=未定义){
  parseXml =功能(xmlStr){
    回报(新window.DOMParser()).parseFromString(xmlStr,文/ XML);
  };
}否则如果(typeof运算window.ActiveXObject =未定义&放大器;!&安培;
   新window.ActiveXObject(Microsoft.XMLDOM)){
    parseXml =功能(xmlStr){
      VAR xmlDoc中=新window.ActiveXObject(Microsoft.XMLDOM);
      xmlDoc.async =假;
      xmlDoc.loadXML(xmlStr);
      返回xmlDoc中;
  };
}其他{
  抛出新的错误(没有找到XML解析器);
}

看到这个问题:

看到这个plunkr: https://plnkr.co/edit/dj63ASQAgrNcLLlwyAum?p = preVIEW

I'm using an API that uses XML instead of JSON. Any suggestions on how to convert the following XML to JSON or how to properly use the data in an ngFor directive?

Also, would an observable be appropriate here?

<case-file>
  <serial-number>123456789</serial-number>
    <transaction-date>20150101</transaction-date>
      <case-file-header>
       <filing-date>20140101</filing-date>
      </case-file-header>
 // ...
   <classifications>
  <classification>
   <international-code-total-no>1</international-code-total-no>
   <primary-code>025</primary-code>
  </classification>
 </classifications>
 </case-file>
 <case-file>
     <serial-number>234567890</serial-number>
    <transaction-date>20160401</transaction-date>
      <case-file-header>
       <filing-date>20160401</filing-date>
      </case-file-header>
//...
   <classifications>
  <classification>
   <international-code-total-no>1</international-code-total-no>
   <primary-code>042</primary-code>
  </classification>
 </classifications>
</case-file>


export class apiService {
   constructor (private http: Http) {}

   private _apiUrl = 'app/api';  

   getCaseFile () {
     return this.http.get(this._apiUrl)
//conversion to JSON here?
                    .map(res => <CaseFile[]> res.json().data)
                     .catch(this.handleError);
   }
    private handleError (error: Response) {

     console.error(error);
    return Observable.throw(error.json().error || 'Server error');
   }
 }

<div *ngFor="#cf of case-file">{{case-file.serial-number}}</div>

解决方案

Based on the library http://goessner.net/download/prj/jsonxml/, I implemented a sample to receive XML data and parse them into an Angular2 application:

var headers = new Headers();
(...)
headers.append('Accept', 'application/xml');

return this.http.get('https://angular2.apispark.net/v1/companies/', {
  headers: headers
}).map(res => JSON.parse(xml2json(res.text(),'  ')));

To be able to use the library, you need to parse first the XML string:

var parseXml;

if (typeof window.DOMParser != "undefined") {
  parseXml = function(xmlStr) {
    return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
  };
} else if (typeof window.ActiveXObject != "undefined" &&
   new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
      var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = "false";
      xmlDoc.loadXML(xmlStr);
      return xmlDoc;
  };
} else {
  throw new Error("No XML parser found");
}

See this question:

See this plunkr: https://plnkr.co/edit/dj63ASQAgrNcLLlwyAum?p=preview.

这篇关于如何解析XML在2角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆