Updating XML file in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dagadakrishna
Forum Commoner
Posts: 29
Joined: Fri Jan 19, 2007 3:30 am

Updating XML file in PHP

Post by dagadakrishna »

hi

this is krishna kanth dagada

today only i joned in this forum .

my doubt is :Using PHP can i update XML data according to uer choice,is it posiible r not

details: a page consists of form with some fields where the value or data of the fields comes from XML file.

If user enters some values in the fields ans clicks update button then XML file has to update in server .

is it posiible by using PHP


waiting for reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yes it is possible
dagadakrishna
Forum Commoner
Posts: 29
Joined: Fri Jan 19, 2007 3:30 am

Post by dagadakrishna »

yes is it posiible?

can u plz give some sugestions in this iam new to php

waiting for ur reply eagerly
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

eagerly eh? how exciting :)

There's SimpleXml or DOM for processing XML. file_get_contents(), file_put_contents() could be useful. And if you google you'll find a whole bunch of tutorials on getting data from forms.

Please note: We help people with their code, we don't write it for them. I'll be of more use to you if you come back with some code I can comment on.
dagadakrishna
Forum Commoner
Posts: 29
Joined: Fri Jan 19, 2007 3:30 am

Updating XML file in PHP

Post by dagadakrishna »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi thanks for giving reply

but i thnk u did nt understood my question

this is my XML

[syntax="xml"]<?xml version="1.0" encoding="ISO-8859-1"?>
<Book Revision_Number="0">

	
	<System_Configuration Name="Tunnel_1" Number_of_Cameras="4" Number_of_Rangers="1" Update_Count="0"/>
	<Ranger_Configuration Name="Tunnel_1" Number_of_Cameras="4" Number_of_Rangers="1" Update_Count="0"/>

</Book>

AND THIS IS MY HTML FILE

Code: Select all

       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<form action="something.php" method="post">

	<CENTER><INPUT TYPE="text" NAME="camera">Cameras
	<INPUT TYPE="text" NAME="ranger">Rangers<BR><BR>
	<INPUT TYPE="submit" value=update>
</CENTER>

</form>

</BODY>
</HTML>

so if users enters some values in fields like cameras and rangers and clicks update button then it has to update the attributes of Number_of_Cameras,Number_of_Rangers in the xml file .


this is my problem hopeu understood my question

c u bye waiting for reply................


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What didn't I understand? Those technologies are perfectly suited to what you are trying to do (possibly DOM is better than SimpleXML for this). As I remember PHP Power Programming has a chapter dedicated to XML.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Welcome. Be sure to use

Code: Select all

and/or

Code: Select all

in your posts when adding code so it helps people with reading them. Thanks![/b]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]11.[/b] Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.[/quote]
dagadakrishna
Forum Commoner
Posts: 29
Joined: Fri Jan 19, 2007 3:30 am

updating XML file in server

Post by dagadakrishna »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi ole
what u told is correct ole i tried according to ur suggestion i succeded at last after one week,

THANK YOU VERY MUCH OLE.

EVERYTHING IS COMPLEATED but one small problem 

this is my XML file


[syntax="xml"]<Mounting_Position   Value="TOP">
			
				<Angles Roll="10" Pitch="0" Yaw="0"/> <!-- RANGE: -110 to 110  UNITS: degrees -->
				<Offsets X="00" Y="10" Z="0"/> <!-- RANGE: -50 to 50 inches  UNITS: inches or cm -->
				<!-- NOTE: The following are embedded in the Offset, but these needs better definition -->
				<!-- Maximum_Range Value="10"/> -->
				<!-- Distance_to_Scanline Value="00"/> -->
</Mounting_Position>

EVERYTHING IS COMPLEATED but one small problem

this is my XML file

Code: Select all

<System_Configuration>

<Mounting_Position_a   Value="TOP">
			
				<Angles Roll="10" Pitch="0" Yaw="0"/> <!-- RANGE: -110 to 110  UNITS: degrees -->
				<Offsets X="00" Y="10" Z="0"/> <!-- RANGE: -50 to 50 inches  UNITS: inches or cm -->
				<!-- NOTE: The following are embedded in the Offset, but these needs better definition -->
				<!-- Maximum_Range Value="10"/> -->
				<!-- Distance_to_Scanline Value="00"/> -->
</Mounting_Position_a>

<Mounting_Position_b  Value="TOP">
			
				<Angles Roll="10" Pitch="0" Yaw="0"/> <!-- RANGE: -110 to 110  UNITS: degrees -->
				<Offsets X="00" Y="10" Z="0"/> <!-- RANGE: -50 to 50 inches  UNITS: inches or cm -->
				<!-- NOTE: The following are embedded in the Offset, but these needs better definition -->
				<!-- Maximum_Range Value="10"/> -->
				<!-- Distance_to_Scanline Value="00"/> -->
</Mounting_Position_b >
<Mounting_Position_c  Value="TOP">
			
				<Angles Roll="10" Pitch="0" Yaw="0"/> <!-- RANGE: -110 to 110  UNITS: degrees -->
				<Offsets X="00" Y="10" Z="0"/> <!-- RANGE: -50 to 50 inches  UNITS: inches or cm -->
				<!-- NOTE: The following are embedded in the Offset, but these needs better definition -->
				<!-- Maximum_Range Value="10"/> -->
				<!-- Distance_to_Scanline Value="00"/> -->
</Mounting_Position_c>

</System_Configuration>


here is my PHP code[/syntax]

Code: Select all

<html>
<head>
<title>Leapfrog</title>
</head>
<body>
 <?php echo $_POST["T1"]; ?>
<?php

 $doc = new DomDocument;

               // We need to validate our document before refering to the id

               $doc->validateOnParse = true;

               //Load the save.xml file into DOMDocument

               $doc->Load('../xml/leapfrog_configuration.xml');

               // We retrieve the attribute named id of the employee element

$attr = $doc->getElementsByTagName('Angles')->item(2)->getAttribute('Roll');


$old=$attr;
$fname = "../xml/leapfrog_configuration.xml";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

$content = str_replace($old, $_POST["T1"], $content);
echo $attr;


$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
echo "changed Succefully"
?>
<script>
</script>
</body>
<html>

here iam trying to change the roll attribute value of angles node, according to the user entered value in roll field.
But my problem is there r so many angles nodes and roll attribute values in my xml file ,then how to update the desired roll attribute value can u suggest me ole.

another problem is if give window.location="user_updation.html"; where user_updation.html is the page where user updates so iam pointing to same page at last .if i give point to the same page by using this code whole page itself not working and if i remove this its working fine.so i did not kept any thing in script above.

ok bye ole


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by dagadakrishna on Tue Jan 23, 2007 5:33 am, edited 1 time in total.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You shouldn't need to use fopen(), fwrite() or st_replace() here. DOM comes with setAttribute() and save() methods. setAttribute() in particular should help you achieve what you are having difficulty with here. Come back to me if this doesn't help you
dagadakrishna
Forum Commoner
Posts: 29
Joined: Fri Jan 19, 2007 3:30 am

Post by dagadakrishna »

hi ole

i made little bit changes in my code like changing item(0) to item(1) to get desired attribute its working fine but problem is if give window.location="user_updation.html"; where user_updation.html is the page where user updates so iam pointing to same page at last .if i give point to the same page by using this code whole page itself not working and if i remove this its working fine.so i did not kept any thing in script above.

bye ole
dagadakrishna
Forum Commoner
Posts: 29
Joined: Fri Jan 19, 2007 3:30 am

Post by dagadakrishna »

hi

i got the output and its working fine

thanks ole
still if any doubts means i will ask ole


thanks once again
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dagadakrishna, since you've chosen to ignore your private messages, here's a public one:
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
Post Reply