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?

2
  • 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. Commented Aug 29, 2015 at 5:45
  • Which library is it? This one: github.com/henrikingo/xml2json Commented Aug 29, 2015 at 8:17

2 Answers 2

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

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

2 Comments

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?
what is this referring to inside your javascript file ? Can you update your question for better understanding by putting JS code
0

Maybe you should try this:

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

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

Comments

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.