Jump to content

Recommended Posts

I'm working on a class to parse XML and am having a frustrating time figuring out why the value of one of my class variables is disappearing.  Here is a class I made to test the problem:

 

class Parser {
var $insideq;
var $tag="";
var $qtext="";
var $num=0;


function startElement($parser,$tagname,$attr) {
$this->tag=$tagname;
echo $tagname . "<br>";
echo $this->qtext;
}


function characterData($parser, $data) {
if ($this->tag=="TEXT") {
$this->qtext=$data;
$this->num=1;
} 
echo $this->qtext;
echo $this->num;

}

function endElement($parser,$tagname) {
if ($tagname=="SELECTONE") {
echo $this->qtext;

}
echo "\\" . $tagname . "<br>";

}

}

 

Here is the XML that is being used to test:

<SELECTONE>
<TEXT>Testing text</TEXT>
<punch></punch>
<PUNCH></PUNCH>
<PUNCH></PUNCH>
</SELECTONE>

 

And the output is as follows:

 

SELECTONE

0TEXT

Testing text1Testing text\TEXT

1PUNCH

\PUNCH

1PUNCH

\PUNCH

1PUNCH

\PUNCH

1 \SELECTONE

 

 

The program should be printing the value of $qtext ("Testing text") at the closing of each tag.  Instead, it only seems to be storing the string for the duration of the Text tag and then prints blank for the rest of the tags.  It has no problem remembering the value of $num, which leads me to believe there's something quirky about the $data variable in the characterData function (which is a character data handler function used like this - xml_set_character_data_handler($xml_parser, "characterData");

 

I'm clearly screwing something up, but I'm completely baffled as to what it is.

Link to comment
https://forums.phpfreaks.com/topic/51343-xml-parsing-class-possible-scope-problem/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.