How to Generate XML in angular js (angular 6)

How to Generate XML in angular js (angular 6)


_________________________________________________________________________________

import { Injectable } from '@angular/core';
import * as XMLWriter from 'xml-writer';
import * as FileSaver from 'file-saver';

@Injectable({
  providedIn: 'root'
})
export class XmlService {
  xw = new XMLWriter;
  constructor() { }

  public generateXml(paramData: any, _ReferenceNo) {
    // Start XML Node
    this.xw = new XMLWriter;
    this.xw.startDocument();
    this.xw.startElement('root');
    this.xw.text('*TAP Instruction sheet created and approved on the Tritex IOT Management platform');
    this.generateNode(paramData, true); // For Home
    this.generateNode(paramData, false); // For Visitor
    this.xw.endElement();

    const blob = new Blob([this.xw], { type: 'application/xhtml+xml;charset=UTF-8' });
    FileSaver.saveAs(blob, _ReferenceNo + '.xhtml');
  }

  generateNode(paramData, isHome) {
    const tapValue = paramData.newTAPInstructionForm;
    const taxTreatmentList = paramData.taxTreatmentList;
    const chargingIntervalEnumList = paramData.chargingIntervalEnumList;
    const perEnumList = paramData.perEnumList;
    const serviceList = paramData.serviceList;
    const tapServiceList = paramData.tapServiceList;

    const _entity = isHome ? tapValue.HomeEntity : tapValue.VisitorEntity;
    const _sectionText = isHome ? 'HPMN' : 'VPMN';

    _entity.forEach(element => {
      // this.xw.startElement('section').writeAttribute('S', _sectionText).writeAttribute('OC', element.OperatorCode)
      //   .writeElement('tag', 'Some content');
      // this.xw.endElement();
      this.xw.startElement('section').writeAttribute('S', _sectionText).writeAttribute('OC', element.OperatorCode);
      const ISO = element.OperatorCurrencyISO;
      console.log(ISO);
      // Common Details
      this.xw.startElement('CD').writeAttribute('EDT', element.EffectiveDate);
      if (isHome) {
        this.xw.writeAttribute('PA', tapValue.TradingEntity);
        this.xw.writeAttribute('PB', tapValue.CounterParty);
        this.xw.writeAttribute('O', element.OperatorName);
        this.xw.writeAttribute('TC', element.OperatorCode);
      } else {
        this.xw.writeAttribute('PA', tapValue.TradingEntity);
        this.xw.writeAttribute('PB', tapValue.CounterParty);
        this.xw.writeAttribute('O', element.OperatorName);
        this.xw.writeAttribute('TC', element.OperatorCode);
      }

      // Period
      element.EntityDetails[0].TapService.forEach((eleTapService, periodIndex) => {
        // const tempEleTabDetails = eleTapService.TapService[periodIndex];
        this.xw.startElement('PD');
        this.xw.writeAttribute('P', 'Period' + Number(periodIndex + 1));
        this.xw.writeAttribute('SDT', (eleTapService.StartDate) ? eleTapService.StartDate as string : 'n/a');
        this.xw.writeAttribute('EDT', (eleTapService.EndDate) ? eleTapService.EndDate as string : 'n/a');
        this.xw.writeAttribute('ISO', (ISO) ? ISO as string : 'n/a');
        element.EntityDetails.forEach((eleEntity, entityIndex) => {
          const eleTabDetails = eleEntity.TapService[periodIndex].TapServicePeriodWise;

          this.xw.startElement('RW');
          this.xw.writeAttribute('PA', eleEntity.PartyATAPCode);
          this.xw.writeAttribute('PB', eleEntity.PartyBTAPCode);
          this.xw.writeAttribute('E', (element.Exclusions) ? element.Exclusions : 'n/a');
          this.xw.writeAttribute('T', taxTreatmentList.filter(x => x.EnumID === eleEntity.TaxTreatmentEnumID)
            .map(x => x.DisplayName).toString());

          eleTabDetails.forEach((eleTab) => {
            this.xw.startElement('RD');
            this.xw.writeAttribute('MS', tapServiceList.filter(x => x.EnumID === eleTab.TapServiceEnumID)
              .map(x => x.SubType).toString());
            this.xw.writeAttribute('SS', serviceList.filter(x => x.EnumID === eleTab.ServiceEnumID).map(x => x.DisplayName).toString());
            this.xw.writeElement('R', (eleTab.Rate) ? eleTab.Rate as string : 'n/a');
            if (eleTab.TapServiceEnumID === 7 || eleTab.TapServiceEnumID === 8) {
              this.xw.writeElement('P', perEnumList.filter(x => x.EnumID === eleTab.PerEnumID).map(x => x.DisplayName).toString());
            }
            this.xw.writeElement('I', chargingIntervalEnumList.filter(x => x.EnumID === eleTab.IntervalEnumID)
              .map(x => x.DisplayName).toString());
            this.xw.endElement(); // RD
          });

          this.xw.endElement(); // RW
        });
        this.xw.endElement(); // PD
      });

      this.xw.endElement(); // Common Details
      this.xw.endElement(); // section
    });
  }
}

No comments:

Post a Comment

Thank You For Your Great Contribution

Featured Post

Asp.net And HTML Css And Web Development: How To Use Mat Table /Data Table in angular 6

Asp.net And HTML Css And Web Development: How To Use Mat Table /Data Table in angular 6 : How To Use Mat Table /Data Table in angular 6 ...

Popular Posts