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.
xmlData
is valid xml? Regardless, this is an issue with thexmlcode
library, not Ember or Firefox. What is happening is that variable internal to the library, calledxmlcode
, doesn't have a method calledreplace
. Usually this happens when you pass innull
orundefined
and then the code tries to callundefined.replace
and it breaks. - nem035"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