LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP5 SimpleXML (problem editing XML) (https://www.linuxquestions.org/questions/programming-9/php5-simplexml-problem-editing-xml-336279/)

carlosruiz 06-22-2005 09:41 PM

PHP5 SimpleXML (problem editing XML)
 
Hello all, I have a XML file with the following contents:
Code:

<?xml version="1.0" encoding="ISO-8859-1"?>
  <language id="English">
    <page id="applications">
      <word id="root_system">Root Application</word>
      <word id="test_system">Test Application</word>
      ... 
    </page>
  </language>

I need to edit the attributes and the text in between the word tags depending on user input, to edit the attributes tag of <word id="test_system"> I do the following:

Code:

$xml = simplexml_load_file('./languages/en.xml') or die ("Unable to load XML file!");
$xpath_query  = $xml->xpath('//page[@id="applications"]/word[@id="test_system"]');
$xpath_query[0]['id'] = "XXXXXXXX";

which works as expected, nevertheless when I try to edit the text in between the tags it fails, this is the code that fails:

Code:

$xml = simplexml_load_file('./languages/en.xml') or die ("Unable to load XML file!");
$xpath_query  = $xml->xpath('//page[@id="applications"]/word[@id="test_system"]');
$xpath_query[0] = "XXXXXXXX";

to edit the text in between I can do the following:

Code:

$xml = simplexml_load_file('./languages/en.xml') or die ("Unable to load XML file!");
$xpath_query  = $xml->xpath('//page[@id="applications"]');
$xpath_query[0]->word[1] = "XXXXXXXX";

but this approach involves knowing the position of <word id="test_system">Test Application</word> in the array $xpath_query, instead of using xpath to search for it, does any of you know how to change the text in between the tags as easy as it is to change the attributes? (isn't that functionality a logical approach? I mean if you can change the attributes you should be able to change the text in between the tags the same way.)

Thank you very much for your help.

carlosruiz 06-23-2005 02:16 AM

Well it seems that it is not possible to edit the values of a xml entry, from the results of xpath, so here is what I am doing:

Code:

function xml_edit_value($object_data, $xpath_expression, $attribute_name, $attribute_value, $replace_string){

        # xpath query.
        $xpathvar = $object_data->xpath($xpath_expression);
       
        $counter=0;       
       
        # iterates the xpath query results.
        foreach ($xpathvar[0] as $result):

                # checks the attribute of the entries in the xpath query,
                # if match is found will set $fieldnumber to the array field number of the matched entry.
                if ($result[$attribute_name] == $attribute_value):
                       
                        $fieldnumber[] = $counter;
               
                endif;
        $counter++;
        endforeach;
       
        # iterates the mached entries.
        foreach ($fieldnumber as $field):
       
                # replaces the value.       
                $xpathvar[0]->word[$field] = $replace_string;
        endforeach;
}

I use it this way:

$xml = simplexml_load_file('./test.xml') or die ("Unable to load XML file!");


xml_edit_value($xml, '//page[@id="applications"]', 'id', 'test_system', 'new value here');

now it can be saved etc.

hope it helps.


All times are GMT -5. The time now is 04:58 PM.