aaronczichon / xmlToJsonTs

Typescript module to convert an XML string into JSON
3 stars 0 forks source link

ReferenceError: DOMParser is not defined #1

Open jeffkatz opened 7 years ago

jeffkatz commented 7 years ago

Good day

I am working on Nativescript, Angular2, Typescript. I am using the npm module, xmlToJsonTs to my project.

I am getting this, ReferenceError: DOMParser is not defined

Thank you

aaronczichon commented 7 years ago

Can you give me some more information? A sample code with reproduce this error would be great. Thanks

jeffkatz commented 7 years ago

The function populateListService.load() queries the server and returns b64 encoded data which I decode, and then use gZip inflate it from binary data using pako.js, then get the xml. I then pass the XML to the xmlToJsonTs function.

`import { Component, ElementRef, NgZone, OnInit, ViewChild } from "@angular/core"; import { Http, Headers } from "@angular/http"; import { Router } from "@angular/router"; import { Observable } from "rxjs/Rx";

import { Lists } from "../../services/lists.service"; import { PopulateListService } from "../../services/populatelist.service";

import "rxjs/add/operator/do"; import "rxjs/add/operator/map";

import Pako = require('pako'); import Base64 = require('base-64'); import { XmlParser, IParserConfiguration } from 'xmlToJsonTs';

@Component({ moduleId: module.id, selector: 'selector', styleUrls: ["listjob.css"], templateUrl: 'listjob.component.html', providers: [PopulateListService] }) export class ListJobComponent implements OnInit { isLoading = false; listLoaded = false;

private theXMLformat;
private theXMLString;

constructor(private router: Router, private populateListService: PopulateListService) {
}

ngOnInit() {
    this.isLoading = true;
    this.populateListService.load()
        .subscribe(data => {
            var decodedData = this.decodeData(data);
            this.theXMLformat = this.processInfo(decodedData);
            this.theXMLString = this.xmlToJson(this.theXMLformat);
        });
}

decodeData(instr) {
    instr = String(instr);
    try {
        if (instr.substr(0, 4) === 'b64=') {
            instr = Base64.decode(instr.substr(4));
        }
    } catch (error) {
        console.log(error);
    }
    return instr;
}

processInfo(compressed) {
    try {
        var result = Pako.inflate(compressed, { to: 'string' });
        return result;
    } catch (err) {
        console.log(err);
    }
}

xmlToJson(xmlstring) {
    try {
        let config = <IParserConfiguration>{};
        config.removeLineBreaks = true;
        config.removeComments = true;
        config.transformTextOnly = true;

        let parser = new XmlParser();
        let json = parser.toJson(xmlstring, config);
        console.log(json);
    } catch (error) {
        console.log(error);
    }
}

}`

aaronczichon commented 7 years ago

DOMParser is an element of HTML document. What kind of UI do you have in your application?

jeffkatz commented 7 years ago

It's a Mobile App built with Nativescript and Angular2. If I understand you correctly, the xmlToJsonTs is written for web browser application, and therefore, I cannot use it for the Mobile Application.

aaronczichon commented 7 years ago

Yes @jeffkatz. Native Script is using the systems UI components, right? Maybe I could try to update the xmlToJsonTs that it creates a new HTML document and using its DOMParser. Maybe this should work.

I'll try it out.