library.xml:
super
admin
change
program
Apache2
PeterWainwright
Wrox
1
AdvancedPHPProgramming
GeorgeSchlossnagle
DeveloperLibrary
1
3
VisualFoxPro6-ProgrammersGuide
EricStroo
MicrosoftPress
2
MasteringJava2
JohnZukowski
Sybex
4
/**********************************************/
readlibrary.php:
$xml=newDOMDocument('1.0');
$xml->load('library.xml');
$groups=array();
$XMLGroups=$xml->getElementsByTagName('groups')->item(0);
foreach($XMLGroups->getElementsByTagName('group')as$groupNode){
/*注意我们是如何得到属性的*/
$gid=$groupNode->getAttribute('gid');
$groups[$gid]=$groupNode->firstChild->nodeValue;
}
?>
XMLLibraryforeach($xml->getElementsBytagName('user')as$user):
$name=$user->getElementsByTagName('name')->item(0)->firstChild->nodeValue;
$author=$user->getElementsByTagName('author')->item(0)->firstChild->nodeValue;
$userCategories=$user->getElementsByTagName('group');
$catList='';
foreach($userCategoriesas$category){
$catList.=$groups[$category->firstChild->nodeValue].',';
}
$catList=substr($catList,0,-2);?>
//echo($name."-".$author."-".$catList."
\n");
echo($name."||".$catList."
\n");
endforeach;?>