[Extension] CXMLModel

This extension provides easy access to XML data from file or string.


<?xml version="1.0" encoding="ISO-8859-1"?>


	<Environment id="I">






	<Environment id="II">








Sample usage:

public function test1()


	$model = Environment::model($this->xml); // $this->xml can be path to file or xml string


	$this->assertEquals(2, $model->length());

	$this->assertEquals('I', $model->getAttribute("id"));


	/// moveNext/movePrevious/move... affects model


	$this->assertEquals('II', $model->getAttribute("id"));


	/// getNext/getPrevious/get.. don't

	$this->assertEquals('I', $model->getFirst()->getAttribute("id"));

	$this->assertEquals('II', $model->getAttribute("id"));




	/// you can iterate over it

	$count = 0;

	$array = array();

	foreach ($model as $k => $v)


		$array[$v->getAttribute("id")] = $k;

		$this->assertEquals('I', $model->getAttribute("id")); // and it will not affect '$model'



	$this->assertTrue(array_key_exists('I', $array));

	$this->assertTrue(array_key_exists('II', $array));

	$this->assertEquals(2, $count);


	/// easy access to children

	$this->assertEquals(4, $model->Node->length());


	/// and to node value

	$this->assertEquals('1', $model->Node->value());

	$this->assertEquals('3', $model->Node->get(2)->value());


It is first public release, so it might be (and probably is) bugged.

I am waiting for feedback and suggestions.

You can download extension here.

Hi There,

I discovered Yii just last week ago, Its truly amazing.

I’ve got some XML fields in my model (using ACRecord) and I need to convert some of that XML nodes to standard variable that can be viewed and validated as any other active record in my model.

Basically I would like to keep using the Yii framework to add/save/validate my XML variables, is that possible in anyway? can you please provide me with some more examples?



I’m a bit confused how this is used you’re saying it can be used as a function and as a class? I need to access element data can you be a bit more clear in accessing it? How does it compare in usage to SimpleXML?


Just trying to use CXMLModel and have run into a problem.

I can’t seem to instigate a working model. For example, I have tried using:

$model = CXMLModel::model(Yii::app()->basePath . '/data/config.xml');  

Where Yii::app()->basePath . ‘/data/config.xml’ is the path to the following (pretty blank) xml file:

<?xml version="1.0" encoding="UTF-8"?>


	<imcc label="" type="">





	<imagenServer label="" type="">





	<storageService label="" type="">





	<rest label="" type="">




	<solr label="" type="">




	<flashMediaServer label="" type="">





But when I do a var_dump of $model I just get null back.

Am I doing something incredibly stupid?



Having had a bit of a play;

$xml = <<<EOD

















		$model = QuestionnaireXML::model($xml);	







Where the contents of QuestionnaireXML.php;

class QuestionnaireXML extends CXMLModel


	public static function model($xml, $className=__CLASS__)


	    return parent::model($xml, $className);



I think this extension doesn’t do what I expected it to do. I was expecting to be able to use this class like an active record, defining rules in the derived class so that I could use the yii form builder to create a form from the model. However thinking about it a bit I would have expected the ‘model’ to be defined by an XML schema and the data to be saved in a a seperate XML file. Or perhaps the writing of an XML ‘database’ connection method, but then it all depends on what you are trying to do.


I need to access the node names and their parent nodes from an XSD file i.e., the xml schema. Is it possible to access those data using this extension?