2010-11-21 111 views

回答

5

在這裏你去:

$dom = new DOMDocument; 
$dom->load('http://www.w3.org/2002/08/xhtml/xhtml1-transitional.xsd'); 
$xsns = 'http://www.w3.org/2001/XMLSchema'; 
$elements = array(); 
foreach ($dom->getElementsByTagNameNS($xsns, 'element') as $element) { 
    if ($element->hasAttribute('name')) { 
     echo $element->getAttribute('name'); 
     $docs = $element->getElementsByTagNameNS($xsns, 'documentation'); 
     foreach ($docs as $doc) { 
      echo "\t", $doc->nodeValue; 
     } 
     echo PHP_EOL; 
    } 
} 

上面的代碼將輸出所有的架構定義的元素類型(不DTD)爲XHTML1 Transitional(不是HTML)加任何文件,例如

pre 
     content is "Inline" excluding 
     "img|object|applet|big|small|sub|sup|font|basefont" 

它使用PHP的原生DOM擴展來做到這一點。 The DOM extension在下面使用libxml,在速度方面優於SimpleHtmlDom並提供對標記的控制。 The DOM interface is a language agnostic W3C specification

對於替代DOM擴展看到

+0

+1;) – 2010-11-21 12:40:48

+0

爲了比較簡單的Html DOM解析器與DOMDocument,請參閱我的答案[這裏](http://stackoverflow.com/questions/4098895/how-to-determine-if-text-string-appears-as-a-child-of-一個名爲-HTML的標籤/ 4235909#4235909),[here](http://stackoverflow.com/questions/2735291/domdocument-class-unable-access-domnode/4230447#4230447)和[here](http://stackoverflow.com/問題/ 4044812 /正則表達式-DOM文檔匹配和替換文本而不是-IN-A-LINK/4209925#4209925)。 – 2010-11-21 12:49:44

-2

在本文檔中,它說

// Dumps the internal DOM tree back into string 
$str = $html; 

// Print it! 
echo $html; 

我想回聲應該是$ STR不是$ HTML,但這樣的文件說什麼。


// Dumps the internal DOM tree back into string 
$str = $html->save(); 

// Dumps the internal DOM tree back into a file 
$html->save('result.htm'); 

希望這有助於。

文檔:http://simplehtmldom.sourceforge.net/manual.htm

+0

它不會顯示html標籤:( – woninana 2010-11-21 09:58:26

1

不,解析器是一個簡單的HTML解析器,它沒有能力來解析DTD,它的內部邏輯處理HTML元素是沒有暴露的(或者甚至會製作方式表達將其以可讀的形式呈現,即使稍微方便一點)。

+0

我該怎麼做才能回顯html標籤? – woninana 2010-11-21 10:21:12

+1

這是一個不同的問題,人們需要知道你爲什麼試圖做到這一點爲了提供一個很好的答案 – Quentin 2010-11-21 10:25:32

+0

我發現了一個鏈接http://stackoverflow.com/questions/2917940/what-libraries-will-parse-a-dtd-using-php如果有一個dtd解析器,但不幸的是,沒有 – woninana 2010-11-21 10:36:37

相關問題