Change a value in a XML document with SimpleXML in PHP -


i have xml file want replace value constant when empty, checking attribute (annotation_ref). xml tags this:

    <annotation>         <ref_annotation annotation_id="id" annotation_ref="1234">             <annotation_value></annotation_value>         </ref_annotation>     </annotation> 

so, result of transformation be:

    <annotation>         <ref_annotation annotation_id="id" annotation_ref="1234">             <annotation_value>my_constant</annotation_value>         </ref_annotation>     </annotation> 

but instead of that, this:

    <annotation>         <ref_annotation annotation_id="id" annotation_ref="1234">             <annotation_value/>         </ref_annotation>     <annotation_value>my_constant</annotation_value></annotation> 

my code next:

$document = simplexml_load_file("my_document.eaf"); $aux = $annotation_document->annotation;  foreach ($aux $aux2) {  if ($aux2->ref_annotation->attributes()->annotation_ref == $my_condition) {    $aux2->annotation_value = $my_constant;  } } 

thank much.

you're talking wrong node:

if ($aux2->ref_annotation->attributes()->annotation_ref == $my_condition) {     ^^^^^^^^^^^^^^^^^^    $aux2->annotation_value = $my_constant;   ^^^^^^^^^^^^ 

you should have

$aux2->ref_annotation->annotation_value = $my_constant; 

Comments