Open, edit, save XML using 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
kchrist
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2005 3:35 am
Location: Greece

Open, edit, save XML using PHP

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
Post Reply