利用Xpath读取XML文件

[b]1. 打印XML文档中所有category节点[/b]

$xmlDoc = new DOMDocument();
$xmlDoc->load("xml/category.xml");

$categories = $xmlDoc->getElementsByTagName("category");

print $xmlDoc->saveXML();


[b]2. 按条件查询获取/subCategories/subCategory节点的内容,进而遍历[/b]

$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;
}

你可能感兴趣的:(php,XML,PHP)