在Php中循环遍历SimpleXmlElement的元素和属性

时间:2014-01-08 19:00:31

标签: php simplexml

<info id="12AB" conf="D4XD">
<cinfo code="CB">Consumer Business</cinfo>
<cinfo code="EB">Enterprise Business</cinfo>
</info>

如何遍历cinfo元素,我可以读取值,即消费者业务和属性值,即CB?

$obj = new SimpleXMLElement($xmldata);
    foreach ($obj->children() as $cinfos) {
?????



    }

1 个答案:

答案 0 :(得分:1)

This应该这样做:

$obj = new SimpleXMLElement($xml);

foreach ($obj->cinfo as $cinfos) {
    // This is the value
    $val = $cinfos[0];
    echo $val."\r\n";;
    // Iterate through the attributes
    foreach($cinfos[0]->attributes() as $key => $val) {
        echo $key,'="',$val,"\"\n";
    }
}