Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/11/2007, 13:03
perikut
 
Fecha de Ingreso: enero-2004
Mensajes: 18
Antigüedad: 20 años, 3 meses
Puntos: 0
manejo php DOM

Buenas,
estoy recien empezando con las herramientas de DOM php para manipular XMLs. Quiero que el usuario altere dinámicamente los valores de un tag (<Filter>) e incrustar éste en un XML predefinido. El XML original es el siguiente:
...
<Rule>
<Filter>
<PropertyIsEqualTo>
<PropertyName>userID</PropertyName>
<Literal>nom USUARI</Literal>
</PropertyIsEqualTo>
</Filter>
<Other_things>
</Other_things>
</Rule>
...
Después de ejecutar el código php (podeis verlo abajo) el tag <Filter> es insertado pero DENTRO del antiguo <Filter>, cuando en realidad deberia is seguido del <Filter> que hay. Éste es el mal resultado:

<Filter>
<PropertyIsEqualTo>
<PropertyName>userID</PropertyName>
<Literal>nom USUARI</Literal>
</PropertyIsEqualTo>

---->nuevo <Filter> mal colocado

<Filter>
<PropertyIsEqualTo>
<PropertyName>genus</PropertyName>
<Literal>XXX</Literal>
</PropertyIsEqualTo>
</Filter>
</Filter>

Gracias a todos,
éste es el código php

<?

$xmlstr =<<<XML
<Filter xmlns:gml="http://www.opengis.net/gml">
<PropertyIsEqualTo>
<PropertyName>genus</PropertyName>
<Literal>valor A PASSAR</Literal>
</PropertyIsEqualTo>

</Filter>
$sxe = simplexml_load_string($xmlstr);
//Values we will pass to the new <Filter> to insert
$species=array('ontophaugs332','copris');
$dom = new DOMDocument;
//XML we will insert the data to
$dom -> load('edit_iberia3.xml');

//Where will be the data inserted
$where_to_insert= $dom->getElementsByTagName('Filter')->item(0);//->item(0);

//we pass parameters of the array
foreach ($species as $sp=>$value)
{
$sxe->PropertyIsEqualTo->Literal=$value;

//This function takes the node NODE of class SimpleXML and makes it into a DOMElement node
$node = dom_import_simplexml($sxe);
//This function returns a copy of the node to import and associates it with the current document.
$node = $dom->importNode($node, true);
$before_insert->appendChild($node);
}

//we save the XML file
echo $dom->save(simples2);
?>