Re[2]: [PHP] Object assignment
| From: | Tom Rogers | Date: | Thu, 10 Jul 2003 02:33:04 +0000 |
| Subject: | Re[2]: [PHP] Object assignment | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-154663@lists.php.net to get a copy of this message | ||
Hi,
Thursday, July 10, 2003, 6:44:55 AM, you wrote:
MG> Tom:
MG> Thanks for the help. Using the array setup you described, I end up with the
MG> value of each "record" node being appended to a single instance of the
MG> parent node. And that single instance of the parent node is the final one
MG> that was parsed (the attributes verify this). The example I used below is
MG> outputting this:
MG> <rootElement>
MG> <record id="2">Value 1Value 2</record>
MG> </rootElement>
MG> Any ideas?
It seems we now have to clone nodes before using them. (php-4.3 +)
I did this as a test:
$test =array(
1=>array('type'=>'record','value'=>'Value
1','id'=>1),
2=>array('type'=>'record','value'=>'Value
2','id'=>2));
$elements = array();
$doc = domxml_new_doc("1.0");
//create a dummy document
ob_start();
echo <<<END
<rootelement>
</rootelement>
END;
$buffer = ob_get_contents();
ob_end_clean();
$doc = domxml_open_mem($buffer);
//get the root element
$ancestor = $doc->document_element();
//loop
foreach($test as $val){
$key = $val['type'];
$value = $val['value'];
$id = $val['id'];
if(!isset($elements[$key])){
$elements[$key] = $doc->create_element($key);
}
$thisChild =
$ancestor->append_child($elements[$key]->clone_node());//<<<<clone
$thisChild->set_attribute('id',$id);
$txt = $doc->create_text_node($value);
$thisChild->append_child($txt);
}
//end loop
echo '<pre>';
echo htmlentities($doc->dump_mem(true));
echo '</pre>';
Gave me
<?xml version="1.0"?>
<rootelement>
<record id="1">Value 1</record><record id="2">Value
2</record></rootelement>
--
regards,
Tom