<?php $hhc = new hhc(); $t = getContentTree(); $hhc->setContentTree($t); $hhc->setOutFileName('test.hhc'); $hhc->output(); function getContentTree() { $doc = new DOMDocument("1.0", "UTF-8"); //$doc->loadHTMLFile('E:\\shopexts\\trunk\\docs\\t2t\\index.html'); $doc->loadHTMLFile('index.html'); $xp = new DOMXPath($doc); $xpathString = "//html/body/*[@class='toc']/ol/li/a"; $nodes = $xp->query($xpathString); $i = 0; #获取第一级 foreach ($nodes as $node) { $attri = $node->attributes->item(0)->nodeValue; $value = $node->nodeValue; $topics[$i]['attri'] = $attri; $topics[$i]['value'] = $value; $i++; } #获取第二级 foreach ($topics as $t_key => $topic) { $xpathString = "//html/body/*[@class='toc']/ol/li/a[@href='" . $topic['attri'] . "']/parent::*/ul/li/a"; $nodes = $xp->query($xpathString); $i = 0; foreach ($nodes as $node) { $attri = $node->attributes->item(0)->nodeValue;