Coder Social home page Coder Social logo

Comments (7)

amitguptagwl avatar amitguptagwl commented on July 18, 2024 1

@savvasmarkstatkus APIs are changed in v4.
Eg

const {XMLParser, XMLBuilder, XMLValidator} = require('fast-xml-parser');

if(XMLValidator.validate()){
    const parser = new XMLParser();
    let jsonObj = parser.parse(xmlData);
    
    const builder = new XMLBuilder();
    let samleXmlData = builder.build(jsonObj);
}

from fast-xml-parser.

amitguptagwl avatar amitguptagwl commented on July 18, 2024
  1. Are you facing problem when you include fast-xml-parser or when you use it in your code?
  2. When you are importing other packages why are you using require to import fast-xml-parser?

I believe the problem is more related to syntax not the library. So please change it to import parser from fast-xml-parser and then try.

from fast-xml-parser.

jstolz avatar jstolz commented on July 18, 2024

Could you please provide me an exact syntax? Everything I try did not work

some errors
scripts.bundle.js:1 Uncaught ReferenceError: require is not defined
at scripts.bundle.js:1
(anonymous) @ scripts.bundle.js:1
core.js:3675 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
i-hub-header.component.ts:21 onDashboardClicked(..) executed.
core.js:1440 ERROR TypeError: Cannot read property 'parse' of undefined
at FileReader.reader.onloadend (imp-exp.service.ts:33)
at ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:4733)
at ZoneDelegate.invoke (zone.js:387)
at Zone.runGuarded (zone.js:151)
at FileReader.eval (zone.js:129)
.....

My source code
`
import { Injectable } from '@angular/core';

//var fastXmlParser = require('fast-xml-parser');
import parser from 'fast-xml-parser';
//import { parser } from 'fast-xml-parser';

@Injectable()
export class ImpExpService {

private files: any;
private fileContent: any;

constructor() {

}

setLoadedFiles(loadedFiles: any) {

this.files = loadedFiles;

const reader = new FileReader();
for (let item of this.files) {

  reader.readAsText(item, 'utf8');

  var that = this;
  reader.onloadend = function () {

    that.fileContent = reader.result;
    debugger;
    var jsonObj = parser.parse(that.fileContent);
    debugger;
  }
}

}
}

`

from fast-xml-parser.

amitguptagwl avatar amitguptagwl commented on July 18, 2024

I believe the issue is related to ES6 implementation. Check https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c

Have you tried to import any other 3rd party library?

from fast-xml-parser.

jstolz avatar jstolz commented on July 18, 2024

Hi,
I found a solution, just add the line "declare var require: any;" before the require.
Regards Jürgen


import { Injectable } from '@angular/core';

import { ImportData } from 'thevoidmodelmodule/dist/shared';
import { SBKDispoListen, SBKOrder } from 'thevoidmodelmodule/dist/ihub';
import { MongoDBService } from '../mongo-db.service';

declare var require: any;
var fastXmlParser = require('fast-xml-parser');

@Injectable()
export class ImpExpService {

  private files: any;
  private fileContent: any;
  private sbkDispoListen: SBKDispoListen = null;

  constructor(private mongoDBService: MongoDBService) {

  }
....
}

from fast-xml-parser.

savvasmarkstatkus avatar savvasmarkstatkus commented on July 18, 2024

I had better luck just using v3.19.0 vs 4 in Angular. Didn't want to spend anymore time dealing with TS errors.

"fast-xml-parser": "^3.19.0",

import { parse } from "fast-xml-parser"; // Couldn't get v4 to work, v3 seemed to.

this.result = parse(xmlStr, options);

from fast-xml-parser.

savvasmarkstatkus avatar savvasmarkstatkus commented on July 18, 2024

With Angular 13 I was getting Type 'XMLParser' has no call signatures. I tweaked it based on your example and looks like I was missing the parser.parse. Thanks.

from fast-xml-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.