BigRetroMike BigRetroMike - 1 month ago 21
Node.js Question

Ember first application wont recognize xmlcode inside xml2json

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.

Answer

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

Comments