Location: PHPKode > scripts > Handle remote data with PHP 5 and REST Web service > handle-remote-data-with-php-5-and-rest-web-service/server/classes/serverXmlData.class.php
<?php
// filename for the XML data - must be read/writable by Web server
$resource_filename = '../xml/myresource.xml';

/* start class processXmlData */
class ServerXmlData {
	private $resource_filename;
	private $doc;
	protected $id;

	public function __construct($resource_filename) {
		$this->resource_filename = $resource_filename;
	}

	// error returned when problem encountered
	public function get_error() {
   		/* includes prologue as the value returned is sent directly to the client */
  		 	return NULL;
	}

	// load XML document from file system
	public function getResource() {

   		$this->doc = new DOMDocument();
   		if ($this->doc->load($this->resource_filename, LIBXML_DTDATTR)) {
      		return $this->doc;
   		}
   		return NULL;
	}

	// Add a new element using $id with the contents $value.
	public function addResource($id, $value) {

  		if ($this->doc = $this->getResource()) {
  			$this->doc->formatOutput = true;
			$this->doc->preserveWhiteSpace = false;

   			$xpath = new DOMXPath($this->doc);
			$query = '/school/courses/course/descr[@nr = '.$id.']';
			$entries = $xpath->query($query);

			foreach ($entries as $entry) { $itexist =  "{$entry->nodeValue}";
				if($itexist != "") { return false;	}}

				$courses = $this->doc->createElement("courses");
				$course = $this->doc->createElement("course");
				$val = $this->doc->createElement('descr',$value);
				$val->setAttribute('nr',$id);
				$course->appendChild($val);
				$courses->appendChild($course);

				$this->doc->documentElement->appendChild($courses);

         		if ($this->doc->save($this->resource_filename)) {
            		return $this->doc->saveXML();
         		}
		}
  		return get_error();
	}

	// Update an existing element based on $id
	public function updateResource($id, $value, $fields) {

  		if ($this->doc = $this->getResource()) {
  			$this->doc->formatOutput = true;
   			$this->doc->preserveWhiteSpace = false;

   			$xpath = new DOMXPath($this->doc);
			$query = '/school/courses/course/descr[@nr = '.$id.']';
			$entries = $xpath->query($query);

       		$newtxt = $this->doc->createTextNode($value);

			foreach ($entries as $entry) {
				if($entry) {
    				foreach($entry->childNodes as $cnode) {
        				if($cnode->nodeType == 3) {
            				if($entry->nodeName == $fields) {
                				$cnode->parentNode->replaceChild($newtxt, $cnode);
                				$valchar = "sY06CSMqwoJiX";
            				}
        				}
    				}
				}
			}
        	if ($this->doc->save($this->resource_filename)) {
           		return $this->doc->saveXML().$valchar;
        	}
    	}
  		return get_error();
	}

	// Delete an existing element based on $id
	public function deleteResource($id) {

		$this->id = $id;

   		if ($this->doc = $this->getResource()) {
   			$this->doc->formatOutput = true;
   			$this->doc->preserveWhiteSpace = false;

			$entry = $this->doc->getElementsByTagName('descr')->item($this->id);

			if($entry != NULL) {
				if($entry->hasAttributes()) {
					if($this->id != NULL) {
						$xpath = new DomXPath($this->doc);
						$result = $xpath->query("//courses");
						$result->item($this->id)->parentNode->removeChild($result->item($this->id));
						$valchar = "sY06CSMqwoJiX";
					}
				}
			}
      		if ($this->doc->save($this->resource_filename)) {
      			return $this->doc->saveXML().$valchar;
     		}
    	}
 		return get_error();
	}

}
/* end class processXmlData */

	// set variable for specific action and class instance
	$action = '';
	$processxml = new  ServerXmlData($resource_filename);

	// set content type for XML
	header('Content-type: text/xml');

	// Determine action based on POST or GET
	if(isset($_POST) && isset($_POST['action']) && $_POST['action'] != 'docview') {
		$action = $_POST['action'];
	} elseif(isset($_GET) && isset($_GET['action']) && $_GET['action'] == 'docview') {
		$action = 'docview';
	}

	// Perform specified action as long as needed parameters have been passed
	if($action == 'docadd'
			&& isset($_POST['nr'])
			&& isset($_POST['value'])) {
			echo $processxml->addResource((int)$_POST['nr'],
										$_POST['value']);
	} elseif($action == 'docupdate'
			&& isset($_POST['nr'])
			&& isset($_POST['value'])
			&& isset($_POST['felder'])) {
			echo $processxml->updateResource((int)$_POST['nr'],
										$_POST['value'],
										$_POST['felder']);
	} elseif($action == 'docdelete'
			&& isset($_POST['nr'])) {
			echo $processxml->deleteResource((int)$_POST['nr']);
	} elseif($action == 'docview') {
		// return XML document without extra formatting
		if($doc = $processxml->getResource()) {
			echo $doc->saveXML();
		} else {
			echo $processxml->get_error();
		}
	} else {
		echo $processxml->get_error();
	}
  ?>
Return current item: Handle remote data with PHP 5 and REST Web service