LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-22-2005, 09:41 PM   #1
carlosruiz
Member
 
Registered: Jul 2003
Location: Japan
Distribution: Mandrake
Posts: 53

Rep: Reputation: 15
Unhappy 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.

Last edited by carlosruiz; 06-22-2005 at 09:52 PM.
 
Old 06-23-2005, 02:16 AM   #2
carlosruiz
Member
 
Registered: Jul 2003
Location: Japan
Distribution: Mandrake
Posts: 53

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
Apache and PHP5 Problem (FC4) jjwl89 Linux - Software 5 06-07-2007 08:59 PM
amarok xml problem zippo85 Linux - Software 3 05-14-2005 06:58 PM
installing apache2+php5+mysql4 problem yangying Linux - Software 5 04-21-2005 01:24 AM
c... problem with xml library true_atlantis Programming 1 10-26-2004 06:34 PM


All times are GMT -5. The time now is 08:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration