tags:

views:

188

answers:

3

I'm making an interface-website to update a concert-list on a band-website. The list is stored as an XML file an has this structure :

I already wrote a script that enables me to add a new gig to the list, this was relatively easy... Now I want to write a script that enables me to edit a certain gig in the list. Every Gig is Unique because of the first attribute : "id" . I want to use this reference to edit the other attributes in that Node.

My PHP is very poor, so I hope someone could put me on the good foot here...

My PHP script :

<?php
$id = $_POST['id'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$venue = $_POST['venue'];

$xml_file="gig.xml";
$fh = fopen($xml_file,'r');
$current_tag=" ";
function start_element($parser,$element_name,$element_attrs)
{
      $current_tag = $element_name;
      if($element_name == 'GIG' && $element_attrs["ID"] == $id)
      {
            echo 'gig ID =' . $id;
                                             // here the new info has to replace the old
      };
};
if($fh) {  
echo "&verify=success&"; 
} else {  
echo "&verify=fail&"; 
}
fclose($fh);
?> 
A: 

You'll want to load the xml file in a domdocument with

<?
$xml = new DOMDocument();
$xml->load("xmlfile.xml");
//find the tags that you want to update
$tags = $xml->getElementsByTagName("GIG");
//find the tag with the id you want to update
foreach ($tags as $tag) {
   if($tag->getAttribute("id") == $id) { //found the tag, now update the attribute
      $tag->setAttribute("[attributeName]", "[attributeValue]");
   }
}

//save the xml
$xml->save();
?>

code is untested, but it's a general idea

Charles Ma
thanks for your response...$tag->setAttribute("[attributeName]", "[attributeValue]"); this code alone is not working...please suggest it...
viswanathan
how are you trying to use it? have a look at the documentation first to make sure you're using it correctly http://www.php.net/manual/en/domelement.setattribute.php
Charles Ma
+1  A: 

Well i dunno what your XML structure looks like but:

<gig id="someid">
 <venue></venue>
 <day></day>
 <month></month>
<year></year>
</gig>

$xml = new SimpleXmlElement('gig.xml',null, true);
$gig = $xml->xpath('//gig[@id="'.$_POST['id'].'"]');
$gig->venue = $_POST['venue'];
$gig->month = $_POST['month'];
// etc..

$xml->asXml('gig.xml)'; // save back to file

now if instead all these data points are attributes you can use $gig->attributes()->venue to access it.

There is no need for the loop really unless you are doing multiple updates with one post - you can get at any specific record via an XPAth query. SimpleXML is also a lot lighter and a lot easier to use for this type of thing than DOMDOcument - especially as you arent using the feature of DOMDocument.

prodigitalson
+1 for SimpleXML.. Checkout these resources to have a better understanding of what's been done here..PHP's SimpleXML class - http://devzone.zend.com/article/688and Xpath - http://www.w3schools.com/XPath/xpath_syntax.asp
Anurag
A: 

Hi, check this, may be it will be help full for u.

http://stackoverflow.com/questions/1193528/how-to-modify-xml-file-using-php

Avinash