[Extension] CXMLModel

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

Xml:


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

<AnyNodeName>

	<Environment id="I">

		<Node>1</Node>

		<Node>2</Node>

		<Node>3</Node>

		<Node>4</Node>

	</Environment>

	<Environment id="II">

		<Node>A</Node>

		<Node>B</Node>

		<Node>C</Node>

		<Node>D</Node>

		<Node>E</Node>

	</Environment>

</AnyNodeName>

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

	$model->moveNext();

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

	

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

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

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

	

	$model->moveFirst();

	

	/// 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'

		$count++;

	}

	$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?

Thanks

Marcos

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?

Hi,

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"?>

<services>

	<imcc label="" type="">

	    <description></description>

		<value></value>

		<port></port>

	</imcc>

	<imagenServer label="" type="">

		<description></description>

		<value></value>

		<port></port>

	</imagenServer>	

	<storageService label="" type="">

		<description></description>	

		<value></value>

		<port></port>

	</storageService>	

	<rest label="" type="">

		<description></description>

		<value></value>		

	</rest>

	<solr label="" type="">

		<description></description>

		<value></value>

	</solr>

	<flashMediaServer label="" type="">

		<description></description>

		<value></value>

	</flashMediaServer>

</services>

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

Am I doing something incredibly stupid?

Thanks,

G

Having had a bit of a play;


$xml = <<<EOD

		<root>

			<QuestionnaireXML>

				<Question1>ABC</Question1>

				<Question2>DEF</Question2>

				<Question3>GHI</Question3>

				<Question4>JKL</Question4>

			</QuestionnaireXML>

			<QuestionnaireXML>

				<Question1>MNO</Question1>

				<Question2>PQR</Question2>

				<Question3>STU</Question3>

				<Question4>VWX</Question4>

			</QuestionnaireXML>

		</root>

EOD;

				

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

		

		Yii::trace(print_r($model->length()));

		echo("<br/>");

		Yii::trace(print_r($model->Question1->value()));

		echo("<br/>");

		Yii::trace(print_r($model->get(1)->Question1->value()));




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.

Hi,

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?