hi,
i have a problem with a recursive function call and i think the reason is that i should work with references. i tried it in various ways, but couldn't find a solution.
i wrote a script, that parses a xml-document and converts (should do it) it in a tree of xml-elements.
this is the code:
<?
class CTreeElement
{
var $m_szName;
var $m_arSubElems;
var $m_parent;
// constructor
function CTreeElement($szName, $parent)
{
$this->m_szName = $szName;
$this->m_arSubElems = array();
$this->m_parent = $parent;
$this->test = 'init val';
}
function addSubElem($sub)
{
array_push($this->m_arSubElems, $sub);
}
}
$g_iDepth = 0;
$g_xmlTree = new CTreeElement('root', null);
$g_curElem = $g_xmlTree;
$xml_data = '<?xml version="1.0"?>
<name1>
<name1_1>
<name1_1_1>
</name1_1_1>
<name1_1_2>
</name1_1_2>
</name1_1>
<name1_2>
<name1_2_1>
</name1_2_1>
<name1_2_2>
</name1_2_2>
</name1_2>
</name1>';
function startElement($parser, $name, $attrs)
{
global $g_iDepth;
global $g_curElem;
$newElem = new CTreeElement($name, $g_curElem);
$g_curElem->addSubElem($newElem);
$g_curElem = $newElem;
$g_iDepth++;
}
function endElement($parser, $name)
{
global $g_iDepth;
global $g_curElem;
$g_curElem = $g_curElem->m_parent;
print('endElement: ' . $g_curElem->m_szName . ': ' . count($g_curElem->m_arSubElems) . '<br>');
$g_iDepth--;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!xml_parse($xml_parser, $xml_data))
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
xml_parser_free($xml_parser);
?>
the output is:
endElement: NAME1_1: 0
endElement: NAME1_1: 0
endElement: NAME1: 0
endElement: NAME1_2: 0
endElement: NAME1_2: 0
endElement: NAME1: 0
endElement: root: 0
my interpretation is that $g_curElem = $newElem; assigns to $g_curElem a copy of $newElem and i do the addSubElem() call to this copy of the new element.
which is the right way?
please help
matze