0
votes

Sorry if this problem could be because my lack of knowledge in node/npm area.

I created my first ember webapplication

I install xml2json with npn and bower and added

app.import('node_module/xml2json/xml2json.js') 

in ember_build_cli.js before return app.toTree();

I added custom route via generate and added a function that should read xml, convert it to json and output data

/myapp/app/routes/getxml.js

import Ember from 'ember';
export default Ember.Route.extend({
model() {
  return Ember.$.ajax({
   url: "data/test.xml",
    dataType:"xml"
  }).done(function(xmlData){
      var jsonData = xml2json.xml_to_object(xmlData);
      return jsonData;
 });
}
});

/myapp/app/data/test.xml

<ART>
<CD>
<ID>1</ID>
</CD>
<CD>
<ID>2</ID>
</CD>
</ART>

I have a problem that xml2json dont work. As I was able to install Ember Inspector inside Firefox I end up with this error:

TypeError: xmlcode.replace is not a function

As I understand the xml2json is imported correctly but Ember/Firefox don't understand code xmlcode.replace inside xml2json library.

Did I miss something when installing application or im doing something wrong?

EDIT:

After creating app from start and installing xml2json only via bower install adding

app.import('bower_components/xml2json/xml2json.js')

in /myapp/ember-cli-build.js when running ember server I see

routes/getxml.js: line 9, col 22, 'xml2json' is not defined

but in Firefox Dev Console i see:

XML Parsing Error: syntax error Location: http://localhost:4200/getxml Line Number 1, Column 1:

And nothing more. And now im unsure if xml2json is loaded (because there is XML parse error) or is not as ember cli state is not defined.

Edit3:

/myapp/app/routes/getxml.js

    import Ember from 'ember';

    export default Ember.Route.extend({
    model() {
      return Ember.$.ajax({
       url: "http://127.0.0.1/GetXML.xml",
       dataType: 'xml'
  }).done(function(xmlData){
    var jsonData = xml2json.xml_to_object(JSON.stringify(xmlData));
    console.log('result', jsonData);
    return jsonData;
  }).fail(function(error){
    console.log('error ', error);
  });
}
});

Print results as undefined in Console inside Firefox Console.

1
Are you sure xmlData is valid xml? Regardless, this is an issue with the xmlcode library, not Ember or Firefox. What is happening is that variable internal to the library, called xmlcode, doesn't have a method called replace. Usually this happens when you pass in null or undefined and then the code tries to call undefined.replace and it breaks. - nem035
It should be - but I can check local version. If I load it as get.xml in url where should i put file to ember to pick it up ? - BigRetroMike
This has nothing to do with ember :). You put the file wherever your server expects it to be. If you're just using localhost and serving files of the filesystem directly, the location should be relative to where you start the server. For example "http://127.0.0.1:8080/GetXML" should try to load the file at {directory-where-server-is-running}/GetXML. Since you're expecting an XML file, you should add the .xml extension to the request url. - nem035
ok... now when starting "ember server" i have routes/getxml.js: line 9, col 22, 'xml2json' is not defined. - BigRetroMike

1 Answers

1
votes

app.import('node_module/xml2json/xml2json.js')

app.import is only for vendor and bower_components files. not for node modules.

I install xml2json with npn and bower and added

Choose either NPM or Bower for modules, but not both.

  • if you choose bower installation then bower install xml2json and include app.import('bower_components/xml2json/xml2json.js')

  • if you choose npm then follow this procedure, Need install browserify and then install required npm modules. npm install ember-browserify --save-dev and then npm install xml2json --save-dev. You need to import it in file wherever you want to access import xml2json from 'npm:xml2json';

url: "http://127.0.0.1:8080/GetXML"

Ensure this endpoint is returning xml response.

xml2json.xml_to_object(xmlData);

I saw toJson and toXml for conversion instead of xml_to_object