XML Parsing with xml_set_character_data_handler help!!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsainteclaire
Forum Newbie
Posts: 5
Joined: Fri Aug 28, 2009 2:38 pm

XML Parsing with xml_set_character_data_handler help!!!!

Post by dsainteclaire »

Ok, so I am hoping that maybe I'm just a dumb noob at PHP, and that either there's an easy answer to my question, or a much better way to do what I'm trying to do.

I am reading an XML file in that is returned to me from a SOAP webservice call (yeah yeah I don't like SOAP either). And I'm using PHP's SAX functions to parse the document that I get back, and I want to build an HTML table based on certain tags. My question is this: Is there some kind of global or built-in variable that I can look at from my function that gets passed to the parser's xml_set_character_data_handler callback? Right now my really simple code looks like this:

Code: Select all

function startTag($xmlParser, $tag, $attrs)
{
    if($tag == 'dat:basePrice')
    {
        echo '<tr>';
    }   
}
 
function endTag($xmlParser, $tag)
{
    if($tag == 'dat:basePrice')
    {
        echo '</tr>';
    }
}
 
function dataHandler($xmlParser, $data)
{
    echo '<td>'.htmlspecialchars($data).'</td>';
}
 
$xmlParser = xml_parser_create();
xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xmlParser, 'startTag', 'endTag');
xml_set_character_data_handler($xmlParser, 'dataHandler');
xml_parse($xmlParser, $responseEnvelope, true);
xml_parser_free($xmlParser);
And I'm trying to build a simple HTML table for a proof of concept for a mashup. Problem is that my content handler obviously outputs every character string into the table, because I don't know how to tell it to only enter values into the table for certain tags. Right now I'm not doing it with classes because I'm just trying to get it to work and will build classes around it all once I have some idea what I'm doing. But I tried to create a local variable at the top of the PHP block that should be in scope for all the functions below it. That doesn't seem to work though. My startTag method can store the tag value and echo it just fine, but then if I try to access the same value, which I had called currentTag, it's empty in the endTag method.

Can someone help me out??
Post Reply