内建的 DOM 解析器使在 PHP 中处理 XML 文档成为可能。
W3C DOM 提供了针对 HTML 和 XML 文档的标准对象集,以及用于访问和操作这些文档的标准接口。
W3C DOM 被分为不同的部分 (Core, XML 和 HTML) 和不同的级别 (DOM Level 1/2/3):
Core DOM - 为任何结构化文档定义标准的对象集
XML DOM - 为 XML 文档定义标准的对象集
HTML DOM - 为 HTML 文档定义标准的对象集
如果您希望学习更多有关 XML DOM 的知识,请访问我们的 XML DOM 教程。
如需读取和更新 - 创建创建并处理 - 一个 XML 文档,您需要 XML 解析器。
有两种基本的 XML 解析器类型:
基于树的解析器:这种解析器把 XML 文档转换为树型结构。它分析整篇文档,并提供了 API 来访问树种的元素,例如文档对象模型 (DOM)。
基于事件的解析器:将 XML 文档视为一系列的事件。当某个具体的事件发生时,解析器会调用函数来处理。
DOM 解析器是基于树的解析器。
请看下面的 XML 文档片段:
<?xml version="1.0" encoding="ISO-8859-1"?> <from>John</from>
XML DOM 把 XML 视为一个树形结构:
Level 1: XML 文档
Level 2: 根元素: <from>
Level 3: 文本元素: "John"
DOM XML 解析器函数是 PHP 核心的组成部分。无需安装就可以使用这些函数。
将在我们的例子中使用下面的 XML 文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
我们需要初始化 XML 解析器,加载 XML,并把它输出:
<?php $xmlDoc = new DOMDocument(); $xmlDoc->load("note.xml"); print $xmlDoc->saveXML(); ?>
以上代码的输出:
George John Reminder Don't forget the meeting!
假如您在浏览器窗口中查看源代码,会看到下面这些 HTML:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
上面的例子创建了一个 DOMDocument-Object,并把 "note.xml" 中的 XML 载入这个文档对象中。
saveXML() 函数把内部 XML 文档放入一个字符串,这样我们就可以输出它。
我们要初始化 XML 解析器,加载 XML,并循环 <note> 元素的所有元素:
<?php $xmlDoc = new DOMDocument(); $xmlDoc->load("note.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { print $item->nodeName . " = " . $item->nodeValue . "<br />"; } ?>
以上代码的输出:
#text = to = George #text = from = John #text = heading = Reminder #text = body = Don't forget the meeting! #text =
在上面的例子中,您看到了每个元素之间存在空的文本节点。
当 XML 生成时,它通常会在节点之间包含空白。XML DOM 解析器把它们当作普通的元素,如果您不注意它们,有时会产生问题。
如果您希望学习更多有关 XML DOM 的知识,请访问我们的 XML DOM 教程。
http://www.w3school.com.cn/php/php_xml_dom.asp
(PHP 5, PHP 7)
Represents an entire HTML or XML document; serves as the root of the document tree.
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 DOMAttr createAttribute ( string $name
)
public DOMAttr createAttributeNS ( string $namespaceURI
, string $qualifiedName
)
public DOMCDATASection createCDATASection ( string $data
)
public DOMComment createComment ( string $data
)
public DOMDocumentFragment createDocumentFragment ( void )
public DOMElement createElement ( string $name
[, string $value
] )
public DOMElement createElementNS ( string $namespaceURI
, string $qualifiedName
[, string $value
] )
public DOMEntityReference createEntityReference ( string $name
)
public DOMProcessingInstruction createProcessingInstruction ( string $target
[, string $data
] )
public DOMText createTextNode ( string $content
)
public DOMElement getElementById ( string $elementId
)
public DOMNodeList getElementsByTagName ( string $name
)
public DOMNodeList getElementsByTagNameNS ( string $namespaceURI
, string $localName
)
public DOMNode importNode ( DOMNode $importedNode
[, bool $deep
] )
public mixed load ( string $filename
[, int $options
= 0 ] )//加载XML文件
public bool loadHTML ( string $source
[, int $options
= 0 ] )//加载HTML字符串
public bool loadHTMLFile ( string $filename
[, int $options
= 0 ] )//加载HTML文件
public mixed loadXML ( string $source
[, int $options
= 0 ] )//加载XML字符串
public void normalizeDocument ( void )
public bool registerNodeClass ( string $baseclass
, string $extendedclass
)
public bool relaxNGValidate ( string $filename
)
public bool relaxNGValidateSource ( string $source
)
public int save ( string $filename
[, int $options
] )//保存XML文件
public string saveHTML ([ DOMNode $node
= NULL ] )//保存HTML字符串
public int saveHTMLFile ( string $filename
)//保存HTML文件
public string saveXML ([ DOMNode $node
[, int $options
]] )//保存XML字符串
public bool schemaValidate ( string $filename
[, int $flags
] )
public bool schemaValidateSource ( string $source
[, int $flags
] )
public bool validate ( void )
public int xinclude ([ int $options
] )
/* 继承的方法 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode
)
public string DOMNode::C14N ([ bool $exclusive
[, bool $with_comments
[, array $xpath
[, array $ns_prefixes
]]]] )
public int DOMNode::C14NFile ( string $uri
[, bool $exclusive
[, bool $with_comments
[, array $xpath
[, array$ns_prefixes
]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep
] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )//是否包含子节点
public DOMNode DOMNode::insertBefore ( DOMNode $newnode
[, DOMNode $refnode
] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI
)
public bool DOMNode::isSameNode ( DOMNode $node
)//Indicates if two nodes are the same node
public bool DOMNode::isSupported ( string $feature
, string $version
)
public string DOMNode::lookupNamespaceURI ( string $prefix
)
public string DOMNode::lookupPrefix ( string $namespaceURI
)
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode
)
public DOMNode DOMNode::replaceChild ( DOMNode $newnode
, DOMNode $oldnode
)
}
actualEncoding
Deprecated. Actual encoding of the document, is a readonly equivalent to encoding.
config
Deprecated. Configuration used when DOMDocument::normalizeDocument() is invoked.
doctype
The Document Type Declaration associated with this document.
documentElement
This is a convenience attribute that allows direct access to the child node that is the document element of the document.
documentURI
The location of the document or NULL
if undefined.
encoding
Encoding of the document, as specified by the XML declaration. This attribute is not present in the final DOM Level 3 specification, but is the only way of manipulating XML document encoding in this implementation.
formatOutput
Nicely formats output with indentation and extra space.
implementation
The DOMImplementation object that handles this document.
preserveWhiteSpace
Do not remove redundant white space. Default to TRUE
.
recover
Proprietary. Enables recovery mode, i.e. trying to parse non-well formed documents. This attribute is not part of the DOM specification and is specific to libxml.
resolveExternals
Set it to TRUE
to load external entities from a doctype declaration. This is useful for including character entities in your XML document.
standalone
Deprecated. Whether or not the document is standalone, as specified by the XML declaration, corresponds to xmlStandalone.
strictErrorChecking
Throws DOMException on errors. Default to TRUE
.
substituteEntities
Proprietary. Whether or not to substitute entities. This attribute is not part of the DOM specification and is specific to libxml.
validateOnParse
Loads and validates against the DTD. Default to FALSE
.
version
Deprecated. Version of XML, corresponds to xmlVersion.
xmlEncoding
An attribute specifying, as part of the XML declaration, the encoding of this document. This is NULL
when unspecified or when it is not known, such as when the Document was created in memory.
xmlStandalone
An attribute specifying, as part of the XML declaration, whether this document is standalone. This is FALSE
when unspecified.
xmlVersion
An attribute specifying, as part of the XML declaration, the version number of this document. If there is no declaration and if this document supports the "XML" feature, the value is "1.0".
Note:
The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.
DOMDocument::__construct — Creates a new DOMDocument object
DOMDocument::createAttribute — Create new attribute
DOMDocument::createAttributeNS — Create new attribute node with an associated namespace
DOMDocument::createCDATASection — Create new cdata node
DOMDocument::createComment — Create new comment node
DOMDocument::createDocumentFragment — Create new document fragment
DOMDocument::createElement — Create new element node
DOMDocument::createElementNS — Create new element node with an associated namespace
DOMDocument::createEntityReference — Create new entity reference node
DOMDocument::createProcessingInstruction — Creates new PI node
DOMDocument::createTextNode — Create new text node
DOMDocument::getElementById — Searches for an element with a certain id
DOMDocument::getElementsByTagName — Searches for all elements with given local tag name
DOMDocument::getElementsByTagNameNS — Searches for all elements with given tag name in specified namespace
DOMDocument::importNode — Import node into current document
DOMDocument::load — Load XML from a file
DOMDocument::loadHTML — Load HTML from a string
DOMDocument::loadHTMLFile — Load HTML from a file
DOMDocument::loadXML — Load XML from a string
DOMDocument::normalizeDocument — Normalizes the document
DOMDocument::registerNodeClass — Register extended class used to create base node type
DOMDocument::relaxNGValidate — Performs relaxNG validation on the document
DOMDocument::relaxNGValidateSource — Performs relaxNG validation on the document
DOMDocument::save — Dumps the internal XML tree back into a file
DOMDocument::saveHTML — Dumps the internal document into a string using HTML formatting
DOMDocument::saveHTMLFile — Dumps the internal document into a file using HTML formatting
DOMDocument::saveXML — Dumps the internal XML tree back into a string
DOMDocument::schemaValidate — Validates a document based on a schema
DOMDocument::schemaValidateSource — Validates a document based on a schema
DOMDocument::validate — Validates the document based on its DTD
DOMDocument::xinclude — Substitutes XIncludes in a DOMDocument Object
http://php.net/manual/zh/class.domdocument.php
最新评论: