一 代码
<?php
header("Content-Type:text/html;charset=utf-8"); //设置编码
/* 创建XML格式的字符串 */
$str = <<<XML
<?xml version='1.0' encoding='gb2312'?>
<object name='commodity'>
<book type='computerbook'>
<bookname name='PHP从入门到精通'/>
</book>
<book type='historybook'>
<booknanme name='上下五千年'/>
</book>
</object>
XML;
$xml = simplexml_load_string($str); //创建一个simplexml对象
foreach($xml->children() as $layer_one){ //循环子节点元素
foreach($layer_one->attributes() as $name => $vl){ //输出各个节点的属性和值
echo $name.'::'.$vl;
}
echo '<br>';
foreach($layer_one->children() as $layer_two){ //输出第二层节点元素
foreach($layer_two->attributes() as $nm => $vl){ //输出各个节点的属性和值
echo $nm."::".$vl;
}
echo '<br>';
}
}
?>
name::PHP从入门到精通
type::historybook
name::上下五千年