2010-11-21 110 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

相关问题