DOM文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口。它是一种与平台和语言无关的应用程序接口(API),它可以动态地访问程序和脚本,更新其内容、结构和www文档的风格(目前,HTML和XML文档是通过说明部分定义的)。DOM 可被 JavaScript 用来读取、改变 HTML、XHTML 以及 XML 文档,它本身属于浏览器。
HTML 文档中的所有节点组成了一个文档树(或节点树)。HTML 文档中的每个元素、属性、文本等都代表着树中的一个节点。树起始于文档节点,并由此继续伸出枝条,直到处于这棵树最低级别的所有文本节点为止。所以查找HTML中的某个标签即是查抄树上的某个节点。
安装
此扩展需要 libxml PHP 扩展。这表示需要使用--enable-libxml,尽管这将隐式完成因为 libxml 是缺省开启的。
此扩展默认为启用,编译时可通过下列选项禁用:--disable-dom
For Ubuntu: sudo apt-get install php7.0-xml For Fedora: yum -y install php-xml
本参考中的许多示例需要XML文件。我们将使用包含以下内容的book.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
]>
<book id="listing">
 <title>My lists</title>
 <chapter id="books">
  <title>My books</title>
  <para>
   <informaltable>
    <tgroup cols="4">
     <thead>
      <row>
       <entry>Title</entry>
       <entry>Author</entry>
       <entry>Language</entry>
       <entry>ISBN</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>The Grapes of Wrath</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>0140186409</entry>
      </row>
      <row>
       <entry>The Pearl</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>014017737X</entry>
      </row>
      <row>
       <entry>Samarcande</entry>
       <entry>Amine Maalouf</entry>
       <entry>fr</entry>
       <entry>2253051209</entry>
      </row>
      <!-- TODO: I have a lot of remaining books to add.. -->
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </chapter>
</book>
DOMAttr类
DOMAttr extends DOMNode
{
	/* 属性 */
	public readonly string $name ;
	public readonly DOMElement $ownerElement ;
	public readonly bool $schemaTypeInfo ;
	public readonly bool $specified ;
	public string $value ;
	/* 继承的属性 */
	public readonly string $nodeName ;
	public string $nodeValue ;
	public readonly int $nodeType ;
	public readonly DOMNode $parentNode ;
	public readonly DOMNodeList $childNodes ;
	public readonly DOMNode $firstChild ;
	public readonly DOMNode $lastChild ;
	public readonly DOMNode $previousSibling ;
	public readonly DOMNode $nextSibling ;
	public readonly DOMNamedNodeMap $attributes ;
	public readonly DOMDocument $ownerDocument ;
	public readonly string $namespaceURI ;
	public string $prefix ;
	public readonly string $localName ;
	public readonly string $baseURI ;
	public string $textContent ;
	/* 方法 */
	public __construct ( string $name [, string $value ] )
	public isId ( void ) : bool
	/* 继承的方法 */
	public DOMNode::appendChild ( DOMNode $newnode ) : DOMNode
	public DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] ) : string
	public DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] ) : int
	public DOMNode::cloneNode ([ bool $deep ] ) : DOMNode
	public DOMNode::getLineNo ( void ) : int
	public DOMNode::getNodePath ( void ) : string
	public DOMNode::hasAttributes ( void ) : bool
	public DOMNode::hasChildNodes ( void ) : bool
	public DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] ) : DOMNode
	public DOMNode::isDefaultNamespace ( string $namespaceURI ) : bool
	public DOMNode::isSameNode ( DOMNode $node ) : bool
	public DOMNode::isSupported ( string $feature , string $version ) : bool
	public DOMNode::lookupNamespaceUri ( string $prefix ) : string
	public DOMNode::lookupPrefix ( string $namespaceURI ) : string
	public DOMNode::normalize ( void ) : void
	public DOMNode::removeChild ( DOMNode $oldnode ) : DOMNode
	public DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode ) : DOMNode
}
DOMCdataSection类
DOMCdataSection extends DOMText 
{
	/* 继承的属性 */
	readonly public string $wholeText ;
	public string $data ;
	readonly public int $length ;
	public readonly string $nodeName ;
	public string $nodeValue ;
	public readonly int $nodeType ;
	public readonly DOMNode $parentNode ;
	public readonly DOMNodeList $childNodes ;
	public readonly DOMNode $firstChild ;
	public readonly DOMNode $lastChild ;
	public readonly DOMNode $previousSibling ;
	public readonly DOMNode $nextSibling ;
	public readonly DOMNamedNodeMap $attributes ;
	public readonly DOMDocument $ownerDocument ;
	public readonly string $namespaceURI ;
	public string $prefix ;
	public readonly string $localName ;
	public readonly string $baseURI ;
	public string $textContent ;
	/* Methods */
	public __construct ( string $value )
	/* Inherited methods */
	public DOMText::isElementContentWhitespace ( void ) : bool
	public DOMText::isWhitespaceInElementContent ( void ) : bool
	public DOMText::splitText ( int $offset ) : DOMText
	public DOMCharacterData::appendData ( string $data ) : void
	public DOMCharacterData::deleteData ( int $offset , int $count ) : void
	public DOMCharacterData::insertData ( int $offset , string $data ) : void
	public DOMCharacterData::replaceData ( int $offset , int $count , string $data ) : void
	public DOMCharacterData::substringData ( int $offset , int $count ) : string
	public DOMNode::appendChild ( DOMNode $newnode ) : DOMNode
	public DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] ) : string
	public DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] ) : int
	public DOMNode::cloneNode ([ bool $deep ] ) : DOMNode
	public DOMNode::getLineNo ( void ) : int
	public DOMNode::getNodePath ( void ) : string
	public DOMNode::hasAttributes ( void ) : bool
	public DOMNode::hasChildNodes ( void ) : bool
	public DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] ) : DOMNode
	public DOMNode::isDefaultNamespace ( string $namespaceURI ) : bool
	public DOMNode::isSameNode ( DOMNode $node ) : bool
	public DOMNode::isSupported ( string $feature , string $version ) : bool
	public DOMNode::lookupNamespaceUri ( string $prefix ) : string
	public DOMNode::lookupPrefix ( string $namespaceURI ) : string
	public DOMNode::normalize ( void ) : void
	public DOMNode::removeChild ( DOMNode $oldnode ) : DOMNode
	public DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode ) : DOMNode
}
DOMCharacterData类
表示具有字符数据的节点。没有节点直接对应于该类,但其他节点确实继承自该类
DOMCharacterData extends DOMNode
{
	/* 属性 */
	public string $data ;
	readonly public int $length ;
	/* 继承的属性 */
	public readonly string $nodeName ;
	public string $nodeValue ;
	public readonly int $nodeType ;
	public readonly DOMNode $parentNode ;
	public readonly DOMNodeList $childNodes ;
	public readonly DOMNode $firstChild ;
	public readonly DOMNode $lastChild ;
	public readonly DOMNode $previousSibling ;
	public readonly DOMNode $nextSibling ;
	public readonly DOMNamedNodeMap $attributes ;
	public readonly DOMDocument $ownerDocument ;
	public readonly string $namespaceURI ;
	public string $prefix ;
	public readonly string $localName ;
	public readonly string $baseURI ;
	public string $textContent ;
	/* 方法 */
	public appendData ( string $data ) : void
	public deleteData ( int $offset , int $count ) : void
	public insertData ( int $offset , string $data ) : void
	public replaceData ( int $offset , int $count , string $data ) : void
	public substringData ( int $offset , int $count ) : string
	/* 继承的方法 */
	public DOMNode::appendChild ( DOMNode $newnode ) : DOMNode
	public DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] ) : string
	public DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] ) : int
	public DOMNode::cloneNode ([ bool $deep ] ) : DOMNode
	public DOMNode::getLineNo ( void ) : int
	public DOMNode::getNodePath ( void ) : string
	public DOMNode::hasAttributes ( void ) : bool
	public DOMNode::hasChildNodes ( void ) : bool
	public DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] ) : DOMNode
	public DOMNode::isDefaultNamespace ( string $namespaceURI ) : bool
	public DOMNode::isSameNode ( DOMNode $node ) : bool
	public DOMNode::isSupported ( string $feature , string $version ) : bool
	public DOMNode::lookupNamespaceUri ( string $prefix ) : string
	public DOMNode::lookupPrefix ( string $namespaceURI ) : string
	public DOMNode::normalize ( void ) : void
	public DOMNode::removeChild ( DOMNode $oldnode ) : DOMNode
	public DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode ) : DOMNode
}
DOMComment
表示注释节点,字符由分隔<!-- and -->
DOMComment extends DOMCharacterData 
{
	/* 继承的属性 */
	public string $data ;
	readonly public int $length ;
	public readonly string $nodeName ;
	public string $nodeValue ;
	public readonly int $nodeType ;
	public readonly DOMNode $parentNode ;
	public readonly DOMNodeList $childNodes ;
	public readonly DOMNode $firstChild ;
	public readonly DOMNode $lastChild ;
	public readonly DOMNode $previousSibling ;
	public readonly DOMNode $nextSibling ;
	public readonly DOMNamedNodeMap $attributes ;
	public readonly DOMDocument $ownerDocument ;
	public readonly string $namespaceURI ;
	public string $prefix ;
	public readonly string $localName ;
	public readonly string $baseURI ;
	public string $textContent ;
	/* 方法 */
	public __construct ([ string $value ] )
	/* 继承的方法 */
	public DOMCharacterData::appendData ( string $data ) : void
	public DOMCharacterData::deleteData ( int $offset , int $count ) : void
	public DOMCharacterData::insertData ( int $offset , string $data ) : void
	public DOMCharacterData::replaceData ( int $offset , int $count , string $data ) : void
	public DOMCharacterData::substringData ( int $offset , int $count ) : string
	public DOMNode::appendChild ( DOMNode $newnode ) : DOMNode
	public DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] ) : string
	public DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] ) : int
	public DOMNode::cloneNode ([ bool $deep ] ) : DOMNode
	public DOMNode::getLineNo ( void ) : int
	public DOMNode::getNodePath ( void ) : string
	public DOMNode::hasAttributes ( void ) : bool
	public DOMNode::hasChildNodes ( void ) : bool
	public DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] ) : DOMNode
	public DOMNode::isDefaultNamespace ( string $namespaceURI ) : bool
	public DOMNode::isSameNode ( DOMNode $node ) : bool
	public DOMNode::isSupported ( string $feature , string $version ) : bool
	public DOMNode::lookupNamespaceUri ( string $prefix ) : string
	public DOMNode::lookupPrefix ( string $namespaceURI ) : string
	public DOMNode::normalize ( void ) : void
	public DOMNode::removeChild ( DOMNode $oldnode ) : DOMNode
	public DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode ) : DOMNode
}
DOMDocument类
表示整个HTML或XML文档;用作文档树的根
DOMDocument extends DOMNode 
{
	/* 属性 */
	readonly public string $actualEncoding ;
	readonly public DOMConfiguration $config ;
	readonly public DOMDocumentType $doctype ;
	readonly public DOMElement $documentElement ;
	public string $documentURI ;
	public string $encoding ;
	public bool $formatOutput ;
	readonly public DOMImplementation $implementation ;
	public bool $preserveWhiteSpace = TRUE ;
	public bool $recover ;
	public bool $resolveExternals ;
	public bool $standalone ;
	public bool $strictErrorChecking = TRUE ;
	public bool $substituteEntities ;
	public bool $validateOnParse = FALSE ;
	public string $version ;
	readonly public string $xmlEncoding ;
	public bool $xmlStandalone ;
	public string $xmlVersion ;
	/* 继承的属性 */
	public readonly string $nodeName ;
	public string $nodeValue ;
	public readonly int $nodeType ;
	public readonly DOMNode $parentNode ;
	public readonly DOMNodeList $childNodes ;
	public readonly DOMNode $firstChild ;
	public readonly DOMNode $lastChild ;
	public readonly DOMNode $previousSibling ;
	public readonly DOMNode $nextSibling ;
	public readonly DOMNamedNodeMap $attributes ;
	public readonly DOMDocument $ownerDocument ;
	public readonly string $namespaceURI ;
	public string $prefix ;
	public readonly string $localName ;
	public readonly string $baseURI ;
	public string $textContent ;
	/* 方法 */
	public __construct ([ string $version [, string $encoding ]] )
	public createAttribute ( string $name ) : DOMAttr
	public createAttributeNS ( string $namespaceURI , string $qualifiedName ) : DOMAttr
	public createCDATASection ( string $data ) : DOMCDATASection
	public createComment ( string $data ) : DOMComment
	public createDocumentFragment ( void ) : DOMDocumentFragment
	public createElement ( string $name [, string $value ] ) : DOMElement
	public createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] ) : DOMElement
	public createEntityReference ( string $name ) : DOMEntityReference
	public createProcessingInstruction ( string $target [, string $data ] ) : DOMProcessingInstruction
	public createTextNode ( string $content ) : DOMText
	public getElementById ( string $elementId ) : DOMElement
	public getElementsByTagName ( string $name ) : DOMNodeList
	public getElementsByTagNameNS ( string $namespaceURI , string $localName ) : DOMNodeList
	public importNode ( DOMNode $importedNode [, bool $deep = FALSE ] ) : DOMNode
	public load ( string $filename [, int $options = 0 ] ) : mixed
	public loadHTML ( string $source [, int $options = 0 ] ) : bool
	public loadHTMLFile ( string $filename [, int $options = 0 ] ) : bool
	public loadXML ( string $source [, int $options = 0 ] ) : mixed
	public normalizeDocument ( void ) : void
	public registerNodeClass ( string $baseclass , string $extendedclass ) : bool
	public relaxNGValidate ( string $filename ) : bool
	public relaxNGValidateSource ( string $source ) : bool
	public save ( string $filename [, int $options = 0 ] ) : int
	public saveHTML ([ DOMNode $node = NULL ] ) : string
	public saveHTMLFile ( string $filename ) : int
	public saveXML ([ DOMNode $node [, int $options = 0 ]] ) : string
	public schemaValidate ( string $filename [, int $flags = 0 ] ) : bool
	public schemaValidateSource ( string $source [, int $flags ] ) : bool
	public validate ( void ) : bool
	public xinclude ([ int $options = 0 ] ) : int
	/* 继承的方法 */
	public DOMNode::appendChild ( DOMNode $newnode ) : DOMNode
	public DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] ) : string
	public DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] ) : int
	public DOMNode::cloneNode ([ bool $deep ] ) : DOMNode
	public DOMNode::getLineNo ( void ) : int
	public DOMNode::getNodePath ( void ) : string
	public DOMNode::hasAttributes ( void ) : bool
	public DOMNode::hasChildNodes ( void ) : bool
	public DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] ) : DOMNode
	public DOMNode::isDefaultNamespace ( string $namespaceURI ) : bool
	public DOMNode::isSameNode ( DOMNode $node ) : bool
	public DOMNode::isSupported ( string $feature , string $version ) : bool
	public DOMNode::lookupNamespaceUri ( string $prefix ) : string
	public DOMNode::lookupPrefix ( string $namespaceURI ) : string
	public DOMNode::normalize ( void ) : void
	public DOMNode::removeChild ( DOMNode $oldnode ) : DOMNode
	public DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode ) : DOMNode
}
