php 使用DOMDocument 解析xml

 Sample Code:

$xml = "<?xml version='1.0' encoding='UTF-8'?><register> <email>[email protected]</email><pass>dGVzdDEyMzQ1Ng==</pass></register>";

print_r(parseXML($xml,array('email','pass')));

function parseXML($xml,$tagNames)
{

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$root = $xmlDoc->documentElement;
$results = array();
foreach($tagNames as $tagName)
{
$elements = $root->getElementsByTagName($tagName);
$value = $elements->item(0)->nodeValue;
$temp =  array($tagName=>$value);
$results = array_merge($results,$temp);
}

return $results;



}

你可能感兴趣的:(PHP,xml,domdocument)