当前位置 : 主页 > 网络编程 > PHP >

PHP解析 XML 数据

来源:互联网 收集:自由互联 发布时间:2021-06-30
$xml_string="?xml version='1.0'?moleculedb molecule name='Benzine' symbolben/symbol codeA/code /molecule molecule name='Water' symbolh2o/symbol codeK/code /molecule/moleculedb"; //load the xml string using simplexml function$xml = simplexml
$xml_string="<?xml version='1.0'?>
<moleculedb>
    <molecule name='Benzine'>
        <symbol>ben</symbol>
        <code>A</code>
    </molecule>
    <molecule name='Water'>
        <symbol>h2o</symbol>
        <code>K</code>
    </molecule>
</moleculedb>";
  
//load the xml string using simplexml function
$xml = simplexml_load_string($xml_string);
  
//loop through the each node of molecule
foreach ($xml->molecule as $record)
{
   //attribute are accessted by
   echo $record['name'], '  ';
   //node are accessted by -> operator
   echo $record->symbol, '  ';
   echo $record->code, '<br />';
}
网友评论