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.

6
  • 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. Commented Oct 19, 2016 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 ? Commented Oct 19, 2016 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. Commented Oct 19, 2016 at 20:02
  • ok... now when starting "ember server" i have routes/getxml.js: line 9, col 22, 'xml2json' is not defined. Commented Oct 19, 2016 at 20:03
  • Check this out How to use third party npm packages with ember cli app Commented Oct 19, 2016 at 20:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

17 Comments

I recreate my app. I added xml2json with bower and added app.import to ember_cli-build.js
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
Yes. import xml2json from 'xml2json'; - not required bower installation.
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 : <ART><CD><ID>1</ID></CD><CD><ID>2</ID></CD></ART> so its valid
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.