Update DOMXML element

BeGe

Вождь Апачей, блин (c)
Update DOMXML element

PHP:
foreach($params as $key=>$val){
                    if (!in_array($key,$nodes)){
                         $newnode=$dom->create_element($key);
                         $newnode->set_content($val);
                         $dom_root->append_child($newnode);
                    } else {
                         $ns=$dom->get_elements_by_tagname("root/".$key);
                         $ns[0]->set_content($val);
                         unset($ns);
                    };
в чем грабли ?
 

[DAN]

Старожил PHPClub
Попробуй так:
PHP:
foreach($params as $key=>$val){ 
                    if (!in_array($key,$nodes)){ 
                         $newnode=$dom->create_element($key); 
                         $newnode->set_content($val); 
                         $root=$newnode->root();
                         $dom_root->add_child($root->clone_node(true)); 
                    } else { 
                         $ns=$dom->get_elements_by_tagname("root/".$key); 
                         $ns[0]->set_content($val); 
                         unset($ns); 
                    };
 

BeGe

Вождь Апачей, блин (c)
Может я плохо объяснил ситуацию. :(((

Есть xml документ структуры
PHP:
<?xml version="1.0"?>
<root>
<param1>1</param1>
<param2>2</param2>
</root>
функция просто добавляет туда параметры
если есть <param1> то оно просто обновляет его значение
если такого нет то добавляет в документ новую node.

пример

в функцию передаёться param1=5 и param3=3
результирущий документ должен быть

PHP:
<?xml version="1.0"?>
<root>
<param1>5</param1>
<param2>2</param2>
<param3>3</param3>
</root>
 

[DAN]

Старожил PHPClub
Так в чем проблема-то ?
Ошибки вылезают или просто не добавляется ?
Если ошибки, пиши, какие.
 

BeGe

Вождь Апачей, блин (c)
оно добавляет значение а не переписывает
 

[DAN]

Старожил PHPClub
Ничего не могу понять. Что куда добавляет ?
Что ты хочешь получить и что ты получаешь ?

P.S. Как говорит tony, телепаты в отпуске.
 

BeGe

Вождь Апачей, блин (c)
PHP:
           if(!$dom = domxml_open_mem($xmlstring)) {
                $error="Error while parsing the document\n";
                return $error;
            };

           $dom_root = $dom->document_element();
           $arr=$dom_root->child_nodes();

           $nodes=array();

           foreach($arr as $val){
           array_push($nodes,$name=$val->node_name());
           };
           $nodes=array_unique($nodes);

           foreach($params as $key=>$val){
                   if (!in_array($key,$nodes)){
                         $newnode=$dom->create_element($key);
                         $newnode->set_content($val);
                         $dom_root->append_child($newnode);
                    } else {
                         $ns=$dom_root->get_elements_by_tagname($key);
                         $ns[0]->set_content($val);
                         unset($ns);
                    };

           };
           $xmlfile = $dom->dump_mem();
 

BeGe

Вождь Апачей, блин (c)
входные данные :

<?xml version="1.0"?>
<root>
<param1>1</param1>
<param2>2</param2>
</root>

$params( "param1"=>4, "param3"=>3)

выходный данные

<?xml version="1.0"?>
<root>
<param1>14</param1>
<param2>2</param2>
<param3>3</param3>
</root>

желаемый результат

<?xml version="1.0"?>
<root>
<param1>4</param1>
<param2>2</param2>
<param3>3</param3>
</root>
 

[DAN]

Старожил PHPClub
As of v4.2.2 of PHP, with libxml version 2.4.19, set_content does not replace the content of the node, but only appends to it. Bug, feature, who knows!

To replace the content of a node, create a new node, copy the properties and children of the old node to the new one, then set the content of the new one and use replace_node to put it back into the DoM.

Надеюсь, перевести сможешь ? :)
 

[DAN]

Старожил PHPClub
PHP:
/**
* Replace node contents
*
* Needed as a workaround for bug/feature of set_content
* This version puts the content 
* as the first child of the new node. 
* If you need it somewhere else, simply
* move $newnode->set_content() where
* you want it.
*/
function replace_content( &$node, &$new_content )
{
$dom =& $node->owner_document();

$newnode =& $dom->create_element( $node->tagname );

$newnode->set_content( $new_content );

$atts =& $node->attributes();
foreach ( $atts as $att )
{
$newnode->set_attribute( $att->name, $att->value );
}

$kids =& $node->child_nodes();
foreach ( $kids as $kid )
{
if ( $kid->node_type() != XML_TEXT_NODE )
{
$newnode->append_child( $kid );
}
}

$node->replace_node( $newnode );
}
 
Сверху