0

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.

BigRetroMike
  • 139
  • 1
  • 10
  • 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 Oct 19 '16 at 19:54
  • 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 Oct 19 '16 at 19:55
  • 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 Oct 19 '16 at 20:02
  • ok... now when starting "ember server" i have routes/getxml.js: line 9, col 22, 'xml2json' is not defined. – BigRetroMike Oct 19 '16 at 20:03
  • Check this out [How to use third party npm packages with ember cli app](http://stackoverflow.com/questions/26544578/how-to-use-third-party-npm-packages-with-ember-cli-app) – nem035 Oct 19 '16 at 20:07
  • I install it like the mark answer mention and added "import xml2json from 'npm:xml2json' end up with Error.captureStackTrace is not a function. url is 'test.xml' and the file is inside both /myapp/ dir and /myapp/app/ dir as im unsure which is the one the url look for. jQuery.Deferred exception: Error.captureStackTrace is not a function getFileName@http://localhost:4200/assets/vendor.js:66163:3 bindings@http://localhost:4200/assets/vendor.js:66092:40 ["/home/brm/myapp/node_modules/node-expat/lib/node-expat.js"] – BigRetroMike Oct 19 '16 at 20:13

1 Answers1

1

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

Ember Freak
  • 12,918
  • 4
  • 24
  • 54
  • I recreate my app. I added xml2json with bower and added app.import to ember_cli-build.js – BigRetroMike Oct 20 '16 at 05:41
  • it looks like i shouldn't add import xml2json from 'xml2json'; in route file .. now I have Cross-Origin Request Blocked as I try to get xml file from remote server which looks fine to me as now xml2json isin't throw I will try with local file – BigRetroMike Oct 20 '16 at 05:43
  • Yes. `import xml2json from 'xml2json';` - not required bower installation. – Ember Freak Oct 20 '16 at 05:46
  • In CLI where I 'ember server' i see that routes/getxml.js: line 9, col 22, 'xml2json' is not defined but in Inspector I see that there was XML Parsing Error.. Which is true.. i dont know the xml body : 12 so its valid – BigRetroMike Oct 20 '16 at 05:49
  • I assume that Inspector is right that there is XML parse error ? but cli error would explain why there is parse error (wrong/bad lib?). Also I changed ulr: 'test.xml' but Im not sure where should i put it as puting it inside /myapp/ or /myapp/app/ dont change anything in Consoli/inspector odput – BigRetroMike Oct 20 '16 at 05:59
  • getxml.js code is in my initial post. but I mark it place there – BigRetroMike Oct 20 '16 at 06:23
  • i believe xml2json issue is fixed. now your api endpoint is problem..just make sure `http://127.0.0.1:8080/GetXML` this resturns xml response...consider it asking as different question without xml2json stuff since it require more details from you and what you have tried – Ember Freak Oct 20 '16 at 06:38
  • Could you provide a simple example code for reading from file and printing it out as Im new to js/fw things? Also Im unsure is xml2json is fixed as there is 2 different errors in cli and console that conflict with each other. – BigRetroMike Oct 20 '16 at 06:47
  • Ember is client side frame work. it wont server file request ..i assume if you put `.fail(function(error) { console.log('error ', error); })` then you will get the exact error..Why do you want to do this way ?. is it for learning ? – Ember Freak Oct 20 '16 at 07:26
  • I dont want to server files.. Im getting xml out of REST API that currently have xml as output. And would like to convert that to json as json is used in ember for data. and display the json on web interface. – BigRetroMike Oct 20 '16 at 07:42
  • REST API url ? does it returns the result, could you be able put `console.log(' result',xmlData)` in success function and also add `.fail(function(error) { console.log('error ', error); })` too. dont include xml2tojson stuff – Ember Freak Oct 20 '16 at 07:47
  • model() { return Ember.$.ajax({ url: "http://127.0.0.1/GetXML.xml", type: 'GET', dataType: 'xml' }).done(function(xmlData){ var jsonData = xml2json.xml_to_object(xmlData); return jsonData; }).fail(function(error){console.log('error ', error); sadly dont give the extra output nothing more than xmlcode.replace is not a function in Inspector. GetXML.xml is accessible and have xml. Also I understand that I can Ignore "routes/getxml.js: line 10, col 21, 'xml2jsone' is not defined' '1 error' in CLI that I start ember server? – BigRetroMike Oct 20 '16 at 07:53
  • After addint result i get result XMLDocument { } inside Console. I also see firstChild, documentElement, activeelement – BigRetroMike Oct 20 '16 at 07:56
  • Try sending string format to xml_to_object method, `var jsonData = xml2json.xml_to_object(JSON.stringify(xmlData));` .. – Ember Freak Oct 20 '16 at 08:07
  • No error now. Only thing that i changes xmlData to jsonData in console.log and see that results is undefined i updated first post to resamble new route code – BigRetroMike Oct 20 '16 at 08:30
  • Either we are not providing exact format `xml_to_object` method expect or the `xml_to_object` might have bug. you need to find a way to test this..My guess `xml2json` object is defined. current problem is with `xml_to_object` method behavior. – Ember Freak Oct 20 '16 at 08:38
  • 1
    You helped me with more and more problems I had. I will mark this issue as resolved as it end up not throwing any errors, except the one in 'ember server' console with not define thing but that could be something with ember itself. I will try to read more docs/tutorial and try my best to understand it better. Thank you very much for your time and help. – BigRetroMike Oct 20 '16 at 09:17