2

For a web project, I've included a JavaScript file as a script src, as shown here.

<script src="xml2json.js"> //same directory as the web project

Next, I tried to invoke a method within xml2json, called xml_str2json.

 downloadUrl("ship_track_ajax.php", function(data) {

                  var xml_string = data.responseText; //an XML string
                  //A parser to transform XML string into a JSON object is required.
                  //Use convert XML to JSON with xml2json.js
                  var markers = xml2json.xml_str2json(xml_string);  
 }

However, console log indicates "Uncaught ReferenceError: xml2json is not defined", even though xml2json is included as a script src. Can anyone tell me as to what is wrong?

Fawzan
  • 4,738
  • 8
  • 41
  • 85
  • 8
    Including the file won't make the functions in there have the prefix 'xml2json' - unless there is an object in that file with that name and the methods you're using. – sje397 Aug 29 '15 at 05:45
  • Which library is it? This one: https://github.com/henrikingo/xml2json – Mario Murrent Aug 29 '15 at 08:17

2 Answers2

0

You have to call the function directly in javascript without reffering the filename as like

xml_str2json(xml_string);

If the function is defined in any of the included file it will be invoked.

I hope this will solve your problem

Ankit Agarwal
  • 1,350
  • 10
  • 15
  • Thanks Ankit. However, I tried doing that and it says that xml_str2json not defined. Within the external file, the function is defined as this.xml_str2json. Could this be an issue? – user3600725 Aug 29 '15 at 06:50
  • what is `this` referring to inside your javascript file ? Can you update your question for better understanding by putting JS code – Ankit Agarwal Aug 29 '15 at 06:52
0

Maybe you should try this:

 var json = xml2json(parseXml(xml), "  ");

See Demo from https://github.com/henrikingo/xml2json

Mario Murrent
  • 712
  • 5
  • 24