• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

XML class instance problems while getting result

Guest
Feb 19, 2007 Feb 19, 2007

Copy link to clipboard

Copied

Hi, I'm trying to use a XML object in a class I'm creating. Something like this:

class MyClass {
// vars
private var myXML:XML;
private var myString:String;

// constructor
function MyClass(url:String) {
this.myXML = new XML();
this.myXML.ignoreWhite = true;
this.myXML.onLoad = function(success) {
trace (this.myXML);
};
}
}

However this seems not work. The problem is that I'm never able to get the content of a XML object after loading it if it is declared as a class variable (after loading, myXML always get "undefined"). If I change the function above to a variable local only to the function it works perfectly:

// constructor
function MyClass(url:String) {
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
trace (myXML);
};
}

But, by doing this I'm not able to pass the XML result to any other class variable, since the word "this", inside the "onLoad" function will point to the XML itself and not to my class, so, including

this.myString = myXML;

would never work since there is no way to access the class variable myString inside the "onLoad" function.

Is there a way to access the XML result and maybe save it to another variable inside my class?
TOPICS
ActionScript

Views

227

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 20, 2007 Feb 20, 2007

Copy link to clipboard

Copied

function MyClass(url:String) {
this.myXML = new XML();
this.myXML.ignoreWhite = true;
this.myXML.onLoad = function(success) {
trace (this); -> inside the callback handler 'this' refers to the myXML object
};
}
}

So, why trace(this.myXML); won't work? As commented above, 'this' inside the callback handler refers to the myXML object. And inside myXML there is no object myXML (as a child..).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 29, 2024 Apr 29, 2024

Copy link to clipboard

Copied

LATEST

The problem arises because inside the onLoad function, the this keyword refers to the XML object itself, not the MyClass instance. To resolve this, you can store a reference to the MyClass instance outside the onLoad function and then use that reference inside the function. Here's how you can modify your code:

class MyClass {
// vars
private var myXML:XML;
private var myString:String;

// constructor
function MyClass(url:String) {
this.myXML = new XML();
this.myXML.ignoreWhite = true;

// Store a reference to the MyClass instance
var self:MyClass = this;

this.myXML.onLoad = function(success) {
// Use the reference to MyClass instance
trace(self.myXML);

// Now you can assign the loaded XML to a class variable
self.myString = self.myXML.toString();
};

// Load XML from URL
this.myXML.load(url);
}
}

In this modified version, self stores a reference to the MyClass instance, allowing you to access class variables within the onLoad function. Additionally, the XML is loaded asynchronously from the URL specified in the constructor. for more inforamtion follow my blog. <a href="https://eduhq.pk">Visit EduHQ.pk</a>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines