XML 다루기

XML 다루기 updated_at: 2024-01-26 17:10

XML

npm i xml2js
npm i --save-dev @types/xml2js
npm i timers // stream

app.modules.ts

import { HttpClientModule } from '@angular/common/http';
imports:[HttpClientModule]
import { Component } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import * as xml2js from 'xml2js';
@Component({
  selector: 'app-root',
  template:`
  `
})
export class XmlComponent1{
  constructor(
    private http:HttpClient
  ) {
    this.loadXML();
  }

  private loadXML(){
    /*Read Data*/
    this.http.get('assets/xmls/users.xml',  {
      headers: new HttpHeaders()
        .set('Content-Type', 'text/xml')
        .append('Access-Control-Allow-Methods', 'GET')
        .append('Access-Control-Allow-Origin', '*')
        .append('Access-Control-Allow-Headers', "Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Request-Method"),
      responseType: 'text'
    })
    .subscribe((data) => {
      this.parseXML(data)
      .then((data: any) => {
        console.log('data >>', data);
        // this.xmlItems = data;
      });
    });
    /*Read Data*/
  }


  //store xml data into array variable
  private parseXML(data: any) {
    return new Promise(resolve => {
      let k: string | number;
      const arr: any = [];
      const parser = new xml2js.Parser(
      {
        trim: true,
        explicitArray: true
      });

      parser.parseString(data, (err: any, result: any) => {
        if (err) {
          console.log(err);
        }
        const obj = result.Employee;
        for (k in obj.emp) {
          var item = obj.emp[k];
          arr.push({
            id: item.id[0],
            name: item.name[0],
            email: item.email[0],
          });
        }
        resolve(arr);
    });
    });
  }
}

Table of contents 목차

평점을 남겨주세요
평점 : 5.0
총 투표수 : 1

질문 및 답글