利用Xpath读取XML文件
1. 打印XML文档中所有category节点
$xmlDoc = new DOMDocument();$xmlDoc->load("xml/category.xml");$categories = $xmlDoc->getElementsByTagName("category");print $xmlDoc->saveXML();
$category = $_GET["category"];$categoryName = $_GET["categoryName"];$xmlDoc = new DOMDocument();$xmlDoc->load("xml/subCategory.xml");$xPath = new DOMXPath($xmlDoc);$nodes = $xPath->query("/subCategories/subCategory[categoryId ='".$category."']");for($i=0; $i<$nodes->length 0; $i++){ $subCategory = $nodes->item($i); $ids = $subCategory->getElementsByTagName( "id" ); $id = $ids->item(0)->nodeValue;}