0
retriving子節點,如果我有這樣的XML:問題與SimpleXML中
<field name="gender" type="select1">
<label>Gender</label>
<item>
<label>Male</label>
<value>1</value>
</item>
<item>
<label>Female</label>
<value>2</value>
</item>
<constraints>
<required/>
</constraints>
</field>
假設我必須讓每個項目的標籤,並創建一個HTML表單。我怎麼能做到這一點使用XML對象simplexml的文件print_r的說明這一點:
[item] => Array
(
[0] => SimpleXMLElement Object
(
[label] => Male
[value] => 1
)
[1] => SimpleXMLElement Object
(
[label] => Female
[value] => 2
)
)
,但在我的代碼
foreach($xml as $field)
{
$type = $xml->field[$i]->attributes()->type ;
$name = $xml->field[$i]->attributes()->name ;
$required = $xml->field[$i]->constraints[0]->required[0] ;
//checkboxes and radios
echo $xml->field[$i]->lable ;
//check the lable
if(is_array($xml->field[$i]->item))
{
echo 'yes it\'s a array ' ;
}
$i++ ;
}
使用此代碼
if ($type == 'select1' || $type == 'radio') {
foreach ($field->item as $item) {
echo "{$item->label} = {$item->value}\n";
}
}
居然還有場的另一臺標籤由項目標籤組成,所以第二個foreach循環將被忽略
<field name="language" type="select">
<label>Language(s)</label>
<item>
<label>English</label>
<value>1</value>
</item>
<item>
<label>French</label>
<value>2</value>
</item>
<item>
<label>Persian</label>
<value>4</value>
</item>
<constraints/>
</field>
如果我有更多的節點,我該如何檢查它。因爲它在第二個foreach循環上重複其他節點 –
mehdi
2011-02-12 19:21:51