php解析xml简单实例

<?php
header("content-type:text/html;charset=utf-8");
$dom = new DOMDocument(); //DOMDocument 代表整个XML文档对象
$dom->load("1.xml");
$messages = $dom->getElementsByTagName('mess'); 
foreach ($messages as $book) 
{ 
    //get name
    $name = $book->getAttribute('name'); 
     
    //get user id
    $nodeAuth = $book->getElementsByTagName('userID'); 
    $strAuth = $nodeAuth->item(0)->nodeValue; 
 
    //get time
    $nodePub = $book->getElementsByTagName('time'); 
    $strtime = $nodePub->item(0)->nodeValue; 
 
    //get MB
    $nodemess = $book->getElementsByTagName('MB'); 
    $strmess = $nodemess->item(0)->nodeValue;        
     
    //save data 
    $arrInfo['name'] = $name; 
    $arrInfo['userID'] = $strAuth; 
    $arrInfo['time'] = $strtime; 
    $arrInfo['MB'] = $strmess; 
 
    //save info 
    $arrInfos[] = $arrInfo; 
} 
 print_r($arrInfos);   
?>

你可能感兴趣的:(php解析xml简单实例)