note 47255 deleted from simplexmlelement.attributes by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Sep 2009 15:30:47 +0000 |
| Subject: | note 47255 deleted from simplexmlelement.attributes by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160678@lists.php.net to get a copy of this message | ||
Note Submitter: christian at koch dot net
----
PHP5:: I think this is a fine solution to get the attributes easier...
<?php
class EFXMLElement extends SimpleXMLElement {
function attributes() {
return new EFXMLAttributes(parent::attributes());
}
}
class EFXMLAttributes {
private $attributes;
function __construct($attributes) {
$this->attributes = $attributes;
}
function __get($name) {
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
} else {
return false;
}
}
}
if (file_exists('pref.xml')) {
$tst = new EFXMLElement(file_get_contents('pref.xml'));
var_dump($tst->plugins->plugin[0]->attributes()->name);
} else {
exit('Failed to open test.xml.');
}
?>
XML::example
<?xml version="1.0" encoding="ISO8859-1" ?>
<preferences>
<system>
EF3
</system>
<websites>
<website name="demo">
<scriptpath>/</scriptpath>
</website>
<website name="other">
x
</website>
</websites>
<plugins>
<plugin type="single" name="content" ontemplate="yes">
<tag><![CDATA[<%content%>]]></tag>
<class>EFContentPlugin</class>
<source>/ef3/plugins/EFContentPlugin.class</source>
</plugin>
<plugin type="single" name="attribute" ontemplate="no">
<tag><![CDATA[<%attribute @%>]]></tag>
<class>EFAttributePlugin</class>
<source>/ef3/plugins/EFAttributePlugin.class</source>
</plugin>
<plugin type="single" name="component" ontemplate="no">
<tag><![CDATA[<%component @%>]]></tag>
<class>EFComponentPlugin</class>
<source>/ef3/plugins/EFComponentPlugin.class</source>
</plugin>
<plugin type="double" name="klammer" ontemplate="no">
<tag><![CDATA[<KLAMMER@>]]></tag>
<closer><![CDATA[<KLAMMER@>]]></closer>
<class>KlammerPlugin</class>
<source>/ef3/plugins/KlammerPlugin.class</source>
</plugin>
</plugins>
</preferences>