Non riesco a capire il funzionamento.... vi posto il mio problema, magari qualcuno mi aiuta a capire!
Esempio di XML:
codice:
<?xml version="1.0"?>
<root>
<item>
<input name="prova" cls="cls">Text</input>
<class>input</class>
<id>prova</id>
<value>valore è default</value>
<maxlength>è</maxlength>
<minlength>0</minlength>
<readonly>0</readonly>
<required>1</required>
<type></type>
<regexp></regexp>
<style></style>
</item>
</root>
Questo è il mio file xml da leggere, e lo vorrei rappresentare con una classe da me creata:
codice:
/*########################## classe objTag - v1.0 ############################
$obj = new objTag(tag,valore); instanzia la classe
$obj->setAttribute($arr); setta gli attributi da un array(chiave,valore)
$obj->setName(string); setta il nome del tag (<nome>)
$obj->setValue(string); setta il valore del tag (<nome>valore</nome>)
$obj->addAttribute(chiave,valore); aggiunge un attributo
obj->removeAttribute(chiave); rimuove l'attributo
$tmp=$obj->getName() ritorna il nome del tag
$tmp=$obj->getValue() ritorna il valore del tag
$tmp=$obj->getAttribute(chiave) ritorna il valore dell'attributo
#############################################################################*/
class objTag{
var $tagName;
var $tagValue;
var $tagAttribute;
function objTag($a,$b){
$this->tagName=$a;
$this->tagValue=$b;
}
function setAttribute($arr){
if (sizeof($arr)) {
while (list($k, $v) = each($arr)){
$this->tagAttribute[$k]=$v;
}
}
}
function getAttribute($a){
if (array_key_exists($a, $this->tagAttribute)) {
return $this->tagAttribute[$a];
}else{
return null;
}
}
function setName($a){ $this->tagName=$a; }
function setValue($a){ $this->tagValue=$a; }
function getName(){ return $this->tagName; }
function getValue(){ return $this->tagValue; }
function addAttribute($a,$b){ $this->tagAttribute[$a]=$b; }
function removeAttribute($a){
if (array_key_exists($a, $this->tagAttribute)) {
unset($this->tagAttribute[$a]);
}
}
}
Che non fa altro tenere dentro se ogni tag (quelli dentro ITEM) con tutti i dati. Per racchiudere tutti i tag userei un array bidimensionale:
codice:
arr[tag_item][tag_interno]
Non so se è la forma più semplice, ma a me non vengono altre idee per ora. Il brutto è che non ho capito come si fa il parser del file XML! Ho visto che ci sono 3 metodi, ma non ne ho capito l'utilizzo.
HELP!!!