Hi,

Friday, July 11, 2003, 4:30:41 AM, you wrote:
MG> Well I've made a signifigant improvement to this function, and it's now the
MG> parsing operation is only something like 26 lines.  It works flawlessly (so
MG> far) for XML that does not have repeat element names on the same level.
MG> Which seems to be bad form anyway, using similar element names and
MG> distinguishing them with attributes.

MG> I'd still like the code to work with XML that did have similarly named
MG> elements in the same node level.. can you see anything in my DOMXML calls
MG> that might indicate the problem?

I rearranged the way you did your parsing and came up with this:

function parseBranch($branch, &$doc, &$parent)
{
        static $elements = array();
        foreach ($branch as $key => $val) { 
                switch ($key) { 
                        // parent attributes 
                        case ('ATTRIBUTES'): 
                                foreach ($val as $attrName => $attrVal) {
                                        switch ($attrName){
                                                case 'VALUE':
                                                        $nodeValue  = 
$doc->create_text_node($attrVal);
                                                        $parentValue = 
$parent->append_child($nodeValue); 
                                                break;
                                                default:
                                                        
$parent->set_attribute($attrName, $attrVal);
                                                break;
                                        }
                                } 
                        break; 
                        // parent value 
                        case ('VALUE'): 
                                $nodeValue  = $doc->create_text_node($val); 
                                $parentValue = $parent->append_child($nodeValue); 
                        break; 
                        default: 
                                // add child element
                                if(!isset($elements[$key])){
                                        $elements[$key] = $doc->create_element($key);
                                }
                                // parse children of this node 
                                foreach ($val as $children) {
                                        //create node
                                        $child = 
$parent->append_child($elements[$key]->clone_node());
                                        //set its elements
                                        parseBranch($children, $doc, $child); 
                                } 
                        break; 
                } 
        } 
}

which gives

<?xml version="1.0"?>
<ROOTELEMENT ID="1">
  <RECORD ID="1">Value 1</RECORD>
  <RECORD ID="2">Value 2</RECORD>
</ROOTELEMENT>

-- 
regards,
Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to