Page 1 of 1
Open, edit, save XML using PHP
Posted: Mon Dec 12, 2005 6:20 am
by kchrist
Hello to all.
Is there any way to open, edit, save an XML which resides on a server (e.g.
http://localhost/xml/file/sample.xml) using PHP.
I want to give the ability to a user, with a PHP template.
BR,
Konstantinos
Posted: Mon Dec 12, 2005 8:41 am
by Chris Corbyn
You can open, edit and save any file using:
file_get_contents() to fetch the data
A textarea in a form to display the data and edit it
fopen($file, 'w+') to open the file ready for writing
fwrite() to save the contents back to file
You may also need
stripslashes() if magic_quotes is enabled in php.ini

Posted: Mon Dec 12, 2005 9:51 am
by patrikG
This is a script I'm using to parse XML-files. It uses
http://uk.php.net/xml_set_element_handler which makes things a bit easier. Returns a nicely formatted array. Modify it to your needs.
Code: Select all
/**
* XML parser package
*/
/**
* XML parser class
* @access public
* @author PatrikG (modified version of a php.net usernote)
* @version v 0.1 17.03.2003
*/
class parser{
var $att;
var $id;
var $title;
var $content;
var $index=-1;
var $xml_parser;
var $tagname;
var $node_number;
/**
* creates an XML-parser & XML-parser event handler, reads the XML-file $file and parses it
*
* @access public
* @param object $db reference to database object
* @param file $file XML-file
* @param string $base_element The element from which parsing will start (tag $base_element itself will be ignored)
* @param string $id_element The element which will be looped through
* @return void
*/
function parser($file,$base_element,$id_element){
$this->base_element = $base_element;
$this->id_element = $id_element;
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser,$this);
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, 'elementContent');
if (!$fp = fopen($file, "r")) {
$this->error[date("dMYHis")]= "Could not open XML-file ".$file;
return false;
}
while ($data = fread($fp, 4096)) {
$data=eregi_replace(">"."[[]]+"."<","><",$data);
if (!xml_parse($this->xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)));
}
}
xml_parser_free($this->xml_parser);
}
/**
* set start element
*
* @access public
* @param $parser parser
* @param stirng $name name of tag
* @param string $attrs attributes
* @return void
*/
function startElement($parser, $name, $attrs) {
if ($name== $this->base_element){
$this->index++;
}
else{
$this->att[$name]=0;
}
$this->tagname=$name;
}
/**
* element content
*
* @access public
* @param array $parser parser
* @param array $data data to be parsed
* @return void
*/
function elementContent($parser, $data) {
$this->result[$this->index][$this->tagname]=$data;
if ($this->tagname== $this->id_element){
$this->id[$this->index]=$data;
}
}
/**
* end element
*
* @access public
* @param array $parser parser
* @param sting $name name
* @return void
*/
function endElement($parser, $name){
$this->tagname=="";
}
}
Usage
Code: Select all
require_once('class_xml_parser.inc.php');
$xml = new parser("#myXMLfile.xml","XMLbasetag","XMLelement");
$xml will now be an object and $xml->result will contain a neat little array with all your data in it. For everything else, use d11 post above.