Book/dom-Phpdoc专题
Document Object Model
目录
- 简介
- 安装/配置
- 预定义常量
- 范例
- DOMAttr — The DOMAttr class
- DOMAttr::__construct — Creates a new DOMAttr object
- DOMAttr::isId — Checks if attribute is a defined ID
- DOMCdataSection — The DOMCdataSection
class
- DOMCdataSection::__construct — Constructs a new DOMCdataSection object
- DOMCharacterData — The
DOMCharacterData class
- DOMCharacterData::appendData — Append the string to the end of the character data of the node
- DOMCharacterData::deleteData — Remove a range of characters from the node
- DOMCharacterData::insertData — Insert a string at the specified 16-bit unit offset
- DOMCharacterData::replaceData — Replace a substring within the DOMCharacterData node
- DOMCharacterData::substringData — Extracts a range of data from the node
- DOMComment — The DOMComment class
- DOMComment::__construct — Creates a new DOMComment object
- DOMDocument — The DOMDocument class
- 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. Only XML Schema 1.0 is supported.
- 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
- DOMDocumentFragment — The
DOMDocumentFragment class
- DOMDocumentFragment::appendXML — Append raw XML data
- DOMDocumentType — The DOMDocumentType class
- DOMElement — The DOMElement class
- DOMElement::__construct — Creates a new DOMElement object
- DOMElement::getAttribute — Returns value of attribute
- DOMElement::getAttributeNode — Returns attribute node
- DOMElement::getAttributeNodeNS — Returns attribute node
- DOMElement::getAttributeNS — Returns value of attribute
- DOMElement::getElementsByTagName — Gets elements by tagname
- DOMElement::getElementsByTagNameNS — Get elements by namespaceURI and localName
- DOMElement::hasAttribute — Checks to see if attribute exists
- DOMElement::hasAttributeNS — Checks to see if attribute exists
- DOMElement::removeAttribute — Removes attribute
- DOMElement::removeAttributeNode — Removes attribute
- DOMElement::removeAttributeNS — Removes attribute
- DOMElement::setAttribute — Adds new attribute
- DOMElement::setAttributeNode — Adds new attribute node to element
- DOMElement::setAttributeNodeNS — Adds new attribute node to element
- DOMElement::setAttributeNS — Adds new attribute
- DOMElement::setIdAttribute — Declares the attribute specified by name to be of type ID
- DOMElement::setIdAttributeNode — Declares the attribute specified by node to be of type ID
- DOMElement::setIdAttributeNS — Declares the attribute specified by local name and namespace URI to be of type ID
- DOMEntity — The DOMEntity class
- DOMEntityReference — The
DOMEntityReference class
- DOMEntityReference::__construct — Creates a new DOMEntityReference object
- DOMException — The DOMException class
- DOMImplementation — The
DOMImplementation class
- DOMImplementation::__construct — Creates a new DOMImplementation object
- DOMImplementation::createDocument — Creates a DOMDocument object of the specified type with its document element
- DOMImplementation::createDocumentType — Creates an empty DOMDocumentType object
- DOMImplementation::hasFeature — Test if the DOM implementation implements a specific feature
- DOMNamedNodeMap — The DOMNamedNodeMap
class
- DOMNamedNodeMap::count — Get number of nodes in the map
- DOMNamedNodeMap::getNamedItem — Retrieves a node specified by name
- DOMNamedNodeMap::getNamedItemNS — Retrieves a node specified by local name and namespace URI
- DOMNamedNodeMap::item — Retrieves a node specified by index
- DOMNode — The DOMNode class
- DOMNode::appendChild — Adds new child at the end of the children
- DOMNode::C14N — Canonicalize nodes to a string
- DOMNode::C14NFile — Canonicalize nodes to a file
- DOMNode::cloneNode — Clones a node
- DOMNode::getLineNo — Get line number for a node
- DOMNode::getNodePath — Get an XPath for a node
- DOMNode::hasAttributes — Checks if node has attributes
- DOMNode::hasChildNodes — Checks if node has children
- DOMNode::insertBefore — Adds a new child before a reference node
- DOMNode::isDefaultNamespace — Checks if the specified namespaceURI is the default namespace or not
- DOMNode::isSameNode — Indicates if two nodes are the same node
- DOMNode::isSupported — Checks if feature is supported for specified version
- DOMNode::lookupNamespaceUri — Gets the namespace URI of the node based on the prefix
- DOMNode::lookupPrefix — Gets the namespace prefix of the node based on the namespace URI
- DOMNode::normalize — Normalizes the node
- DOMNode::removeChild — Removes child from list of children
- DOMNode::replaceChild — Replaces a child
- DOMNodeList — The DOMNodeList class
- DOMNodeList::count — Get number of nodes in the list
- DOMNodeList::item — Retrieves a node specified by index
- DOMNotation — The DOMNotation class
- DOMProcessingInstruction —
The DOMProcessingInstruction class
- DOMProcessingInstruction::__construct — Creates a new DOMProcessingInstruction object
- DOMText — The DOMText class
- DOMText::__construct — Creates a new DOMText object
- DOMText::isElementContentWhitespace — Returns whether this text node contains whitespace in element content
- DOMText::isWhitespaceInElementContent — Indicates whether this text node contains whitespace
- DOMText::splitText — Breaks this node into two nodes at the specified offset
- DOMXPath — The DOMXPath class
- DOMXPath::__construct — Creates a new DOMXPath object
- DOMXPath::evaluate — Evaluates the given XPath expression and returns a typed result if possible
- DOMXPath::query — Evaluates the given XPath expression
- DOMXPath::registerNamespace — Registers the namespace with the DOMXPath object
- DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions
- DOM 函数
- dom_import_simplexml — Gets a DOMElement object from a SimpleXMLElement object
简介
DOMAttr represents an attribute in the DOMElement object.
类摘要
DOMAttr
class DOMAttr extends DOMNode {
/* 属性 */
public <span
class="modifier">readonly string
$name ;
public <span
class="modifier">readonly DOMElement
$ownerElement ;
public <span
class="modifier">readonly bool
$schemaTypeInfo ;
public <span
class="modifier">readonly bool
$specified ;
public string
$value ;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">string $name [,
string
$value ] )
public bool isId ( <span class="methodparam">void )
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
name
The name of the attribute
ownerElement
The element which contains the attribute
schemaTypeInfo
Not implemented yet, always is null
specified
Not implemented yet, always is null
value
The value of the attribute
参见
DOMAttr::__construct
Creates a new DOMAttr object
说明
public <span
class="methodname">DOMAttr::__construct ( <span
class="methodparam">string $name [,
string
$value ] )
Creates a new DOMAttr object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writable node, use .
参数
name
The tag name of the attribute.
value
The value of the attribute.
范例
示例 #1 Creating a new DOMAttr object
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$attr = $element->setAttributeNode(new DOMAttr('attr', 'attrvalue'));
echo $dom->saveXML();
?>
以上例程会输出:
<?xml version="1.0" encoding="utf-8"?>
<root attr="attrvalue" />
参见
- DOMDocument::createAttribute
DOMAttr::isId
Checks if attribute is a defined ID
说明
public bool DOMAttr::isId ( <span class="methodparam">void )
This function checks if the attribute is a defined ID.
According to the DOM standard this requires a DTD which defines the attribute ID to be of type ID. You need to validate your document with or DOMDocument::validateOnParse before using this function.
返回值
成功时返回 true, 或者在失败时返回 false。
范例
示例 #1 DOMAttr::isId() Example
<?php
$doc = new DomDocument;
// We need to validate our document before referring to the id
$doc->validateOnParse = true;
$doc->Load('book.xml');
// We retrieve the attribute named id of the chapter element
$attr = $doc->getElementsByTagName('chapter')->item(0)->getAttributeNode('id');
var_dump($attr->isId()); // bool(true)
?>
简介
The DOMCdataSection inherits from <span class="classname">DOMText for textural representation of CData constructs.
类摘要
DOMCdataSection
class DOMCdataSection <span class="ooclass"> extends DOMText {
/* 继承的属性 */
public <span
class="modifier">readonly string
$wholeText ;
public string
$data ;
public <span
class="modifier">readonly int $length
;
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* Methods */
public <span
class="methodname">__construct ( <span
class="methodparam">string $value )
/* Inherited methods */
public bool DOMText::isElementContentWhitespace ( void )
public bool DOMText::isWhitespaceInElementContent ( void )
public DOMText
DOMText::splitText ( <span
class="methodparam">int $offset )
public void
DOMCharacterData::appendData ( <span
class="methodparam">string $data )
public void
DOMCharacterData::deleteData ( <span
class="methodparam">int $offset ,
int $count
)
public void
DOMCharacterData::insertData ( <span
class="methodparam">int $offset ,
string
$data )
public void
DOMCharacterData::replaceData ( <span
class="methodparam">int $offset ,
int $count
, string
$data )
public string
DOMCharacterData::substringData ( <span
class="methodparam">int $offset ,
int $count
)
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
DOMCdataSection::__construct
Constructs a new DOMCdataSection object
说明
public <span
class="methodname">DOMCdataSection::__construct ( <span
class="methodparam">string $value )
Constructs a new CDATA node. This works like the <span class="classname">DOMText class.
参数
value
The value of the CDATA node. If not supplied, an empty CDATA node is
created.
范例
示例 #1 Creating a new DOMCdataSection object
<?php
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->appendChild(new DOMElement('root'));
$text = $element->appendChild(new DOMCdataSection('root value'));
echo $dom->saveXML();
?>
以上例程会输出:
<?xml version="1.0" encoding="utf-8"?>
<root><![CDATA[root value]]></root>
参见
- DOMText::__construct
- DOMDocument::createTextNode
简介
Represents nodes with character data. No nodes directly correspond to this class, but other nodes do inherit from it.
类摘要
DOMCharacterData
class DOMCharacterData <span class="ooclass"> extends DOMNode {
/* 属性 */
public string
$data ;
public <span
class="modifier">readonly int $length
;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public void
appendData ( <span
class="methodparam">string $data )
public void
deleteData ( <span
class="methodparam">int $offset ,
int $count
)
public void
insertData ( <span
class="methodparam">int $offset ,
string
$data )
public void
replaceData ( <span
class="methodparam">int $offset ,
int $count
, string
$data )
public string
substringData ( <span
class="methodparam">int $offset ,
int $count
)
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
data
The contents of the node.
length
The length of the contents.
参见
DOMCharacterData::appendData
Append the string to the end of the character data of the node
说明
public void
DOMCharacterData::appendData ( <span
class="methodparam">string $data )
Append the string data to the end of the character data of the node.
参数
data
The string to append.
返回值
没有返回值。
参见
- DOMCharacterData::deleteData
- DOMCharacterData::insertData
- DOMCharacterData::replaceData
- DOMCharacterData::substringData
DOMCharacterData::deleteData
Remove a range of characters from the node
说明
public void
DOMCharacterData::deleteData ( <span
class="methodparam">int $offset ,
int $count
)
Deletes count characters starting from position offset.
参数
offset
The offset from which to start removing.
count
The number of characters to delete. If the sum of offset and count
exceeds the length, then all characters to the end of the data are
deleted.
返回值
没有返回值。
错误/异常
DOM_INDEX_SIZE_ERR
Raised if offset is negative or greater than the number of 16-bit
units in data, or if count is negative.
参见
- DOMCharacterData::appendData
- DOMCharacterData::insertData
- DOMCharacterData::replaceData
- DOMCharacterData::substringData
DOMCharacterData::insertData
Insert a string at the specified 16-bit unit offset
说明
public void
DOMCharacterData::insertData ( <span
class="methodparam">int $offset ,
string
$data )
Inserts string data at position offset.
参数
offset
The character offset at which to insert.
data
The string to insert.
返回值
没有返回值。
错误/异常
DOM_INDEX_SIZE_ERR
Raised if offset is negative or greater than the number of 16-bit
units in data.
参见
- DOMCharacterData::appendData
- DOMCharacterData::deleteData
- DOMCharacterData::replaceData
- DOMCharacterData::substringData
DOMCharacterData::replaceData
Replace a substring within the DOMCharacterData node
说明
public void
DOMCharacterData::replaceData ( <span
class="methodparam">int $offset ,
int $count
, string
$data )
Replace count characters starting from position offset with data.
参数
offset
The offset from which to start replacing.
count
The number of characters to replace. If the sum of offset and count
exceeds the length, then all characters to the end of the data are
replaced.
data
The string with which the range must be replaced.
返回值
没有返回值。
错误/异常
DOM_INDEX_SIZE_ERR
Raised if offset is negative or greater than the number of 16-bit
units in data, or if count is negative.
参见
- DOMCharacterData::appendData
- DOMCharacterData::deleteData
- DOMCharacterData::insertData
- DOMCharacterData::substringData
DOMCharacterData::substringData
Extracts a range of data from the node
说明
public string
DOMCharacterData::substringData ( <span
class="methodparam">int $offset ,
int $count
)
Returns the specified substring.
参数
offset
Start offset of substring to extract.
count
The number of characters to extract.
返回值
The specified substring. If the sum of offset and count exceeds the
length, then all 16-bit units to the end of the data are returned.
错误/异常
DOM_INDEX_SIZE_ERR
Raised if offset is negative or greater than the number of 16-bit
units in data, or if count is negative.
参见
- DOMCharacterData::appendData
- DOMCharacterData::deleteData
- DOMCharacterData::insertData
- DOMCharacterData::replaceData
简介
Represents comment nodes, characters delimited by <!-- and -->.
类摘要
DOMComment
class DOMComment <span class="ooclass"> extends DOMCharacterData {
/* 继承的属性 */
public string
$data ;
public <span
class="modifier">readonly int $length
;
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $value ]
)
/* 继承的方法 */
public void
DOMCharacterData::appendData ( <span
class="methodparam">string $data )
public void
DOMCharacterData::deleteData ( <span
class="methodparam">int $offset ,
int $count
)
public void
DOMCharacterData::insertData ( <span
class="methodparam">int $offset ,
string
$data )
public void
DOMCharacterData::replaceData ( <span
class="methodparam">int $offset ,
int $count
, string
$data )
public string
DOMCharacterData::substringData ( <span
class="methodparam">int $offset ,
int $count
)
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
参见
DOMComment::__construct
Creates a new DOMComment object
说明
public <span
class="methodname">DOMComment::__construct ([ <span
class="methodparam">string $value ]
)
Creates a new DOMComment object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use .
参数
value
The value of the comment.
范例
示例 #1 Creating a new DOMComment
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$comment = $element->appendChild(new DOMComment('root comment'));
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?><root><!--root comment--></root> */
?>
参见
- DOMDocument::createComment
简介
Represents an entire HTML or XML document; serves as the root of the document tree.
类摘要
DOMDocument
class DOMDocument <span class="ooclass"> extends DOMNode {
/* 属性 */
public <span
class="modifier">readonly string
$actualEncoding ;
public <span
class="modifier">readonly <span
class="type">DOMConfiguration $config ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentType $doctype ;
public <span
class="modifier">readonly DOMElement
$documentElement ;
public <span
class="type">stringnull
$documentURI ;
public string
$encoding ;
public bool
$formatOutput ;
public <span
class="modifier">readonly <span
class="type">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 ;
public <span
class="modifier">readonly <span
class="type">stringnull
$xmlEncoding ;
public bool
$xmlStandalone ;
public string
$xmlVersion ;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $version
[, string
$encoding ]] )
public DOMAttr
createAttribute ( <span
class="methodparam">string $name )
public DOMAttr
createAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $qualifiedName )
public <span
class="type">DOMCDATASection <span
class="methodname">createCDATASection ( <span
class="methodparam">string $data )
public <span
class="type">DOMComment <span
class="methodname">createComment ( <span
class="methodparam">string $data )
public <span class="type">DOMDocumentFragment <span class="methodname">createDocumentFragment ( <span class="methodparam">void )
public <span
class="type">DOMElement <span
class="methodname">createElement ( <span
class="methodparam">string $name [,
string
$value ] )
public <span
class="type">DOMElement <span
class="methodname">createElementNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $qualifiedName [, <span
class="methodparam">string $value ]
)
public <span
class="type">DOMEntityReference <span
class="methodname">createEntityReference ( <span
class="methodparam">string $name )
public <span
class="type">DOMProcessingInstruction <span
class="methodname">createProcessingInstruction ( <span
class="methodparam">string $target
[, string
$data ] )
public DOMText
createTextNode ( <span
class="methodparam">string $content )
public <span
class="type">DOMElement <span
class="methodname">getElementById ( <span
class="methodparam">string $elementId
)
public <span
class="type">DOMNodeList <span
class="methodname">getElementsByTagName ( <span
class="methodparam">string $name )
public <span
class="type">DOMNodeList <span
class="methodname">getElementsByTagNameNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
public DOMNode
importNode ( <span
class="methodparam">DOMNode
$importedNode [, <span
class="type">bool $deep =
false ] )
public mixed
load ( <span
class="type">string $filename [, <span
class="methodparam">int $options<span
class="initializer"> = 0 ] )
public bool
loadHTML ( <span
class="methodparam">string $source
[, int
$options = 0 ] )
public bool
loadHTMLFile ( <span
class="methodparam">string $filename
[, int
$options = 0 ] )
public mixed
loadXML ( <span
class="methodparam">string $source
[, int
$options = 0 ] )
public void normalizeDocument ( <span class="methodparam">void )
public bool
registerNodeClass ( <span
class="methodparam">string $baseclass
, string
$extendedclass )
public bool
relaxNGValidate ( <span
class="methodparam">string $filename
)
public bool
relaxNGValidateSource ( <span
class="methodparam">string $source )
public int <span
class="methodname">save ( <span
class="type">string $filename [, <span
class="methodparam">int $options<span
class="initializer"> = 0 ] )
public string
saveHTML ([ <span
class="methodparam">DOMNode $node<span
class="initializer"> = NULL ] )
public int <span
class="methodname">saveHTMLFile ( <span
class="type">string $filename )
public string
saveXML ([ <span
class="methodparam">DOMNode $node [,
int $options<span
class="initializer"> = 0 ]] )
public bool
schemaValidate ( <span
class="methodparam">string $filename
[, int
$flags = 0 ] )
public bool
schemaValidateSource ( <span
class="methodparam">string $source
[, int
$flags ] )
public bool validate ( <span class="methodparam">void )
public int <span
class="methodname">xinclude ([ <span
class="type">int $options =
0 ] )
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
actualEncoding
Deprecated. Actual encoding of the document, is a readonly equivalent
to encoding.
config
Deprecated. Configuration used when <span
class="function">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. This has no
effect if the document was loaded with preserveWhitespace enabled.
implementation
The DOMImplementation object that handles
this document.
preserveWhiteSpace
Do not remove redundant white space. Default to true. Setting this
to false has the same effect as passing LIBXML_NOBLANKS as
option to DOMDocument::load etc.
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.
Caution Enabling entity substitution may facilitate XML External Entity (XXE) attacks.
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:
此 DOM 扩展采用 UTF-8 编码。在 ISO-8859-1 编码下,使用 <span class="function">utf8_encode 和 <span class="function">utf8_decode 来处理,其它编码下使用 iconv 函数处理。
Note:
When using json_encode on a <span class="classname">DOMDocument object the result will be that of encoding an empty object.
参见
DOMDocument::__construct
Creates a new DOMDocument object
说明
public <span
class="methodname">DOMDocument::__construct ([ <span
class="methodparam">string $version
[, string
$encoding ]] )
Creates a new DOMDocument object.
参数
version
The version number of the document as part of the XML declaration.
encoding
The encoding of the document as part of the XML declaration.
范例
示例 #1 Creating a new DOMDocument
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?> */
?>
参见
- DOMImplementation::createDocument
DOMDocument::createAttribute
Create new attribute
说明
public DOMAttr
DOMDocument::createAttribute ( <span
class="methodparam">string $name )
This function creates a new instance of class <span class="classname">DOMAttr. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
参数
name
The name of the attribute.
返回值
The new DOMAttr or false if an
error occurred.
错误/异常
DOM_INVALID_CHARACTER_ERR
Raised if name contains an invalid character.
参见
- DOMNode::appendChild
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createAttributeNS
Create new attribute node with an associated namespace
说明
public DOMAttr
DOMDocument::createAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $qualifiedName )
This function creates a new instance of class <span class="classname">DOMAttr. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
参数
namespaceURI
The URI of the namespace.
qualifiedName
The tag name and prefix of the attribute, as prefix:tagname.
返回值
The new DOMAttr or false if an
error occurred.
错误/异常
DOM_INVALID_CHARACTER_ERR
Raised if qualifiedName contains an invalid character.
DOM_NAMESPACE_ERR
Raised if qualifiedName is a malformed qualified name, or if
qualifiedName has a prefix and namespaceURI is null.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createCDATASection
Create new cdata node
说明
public <span
class="type">DOMCDATASection <span
class="methodname">DOMDocument::createCDATASection ( <span
class="methodparam">string $data )
This function creates a new instance of class <span class="classname">DOMCDATASection. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
参数
data
The content of the cdata.
返回值
The new DOMCDATASection or false if
an error occurred.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createComment
Create new comment node
说明
public <span
class="type">DOMComment <span
class="methodname">DOMDocument::createComment ( <span
class="methodparam">string $data )
This function creates a new instance of class <span class="classname">DOMComment. 此节点出现在文档中,除非是用诸如 DOMNode->appendChild 等函数来将其插入。
参数
data
The content of the comment.
返回值
The new DOMComment or false if an
error occurred.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createDocumentFragment
Create new document fragment
说明
public <span class="type">DOMDocumentFragment <span class="methodname">DOMDocument::createDocumentFragment ( <span class="methodparam">void )
This function creates a new instance of class <span class="classname">DOMDocumentFragment. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
返回值
The new DOMDocumentFragment or
false if an error occurred.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createElement
Create new element node
说明
public <span
class="type">DOMElement <span
class="methodname">DOMDocument::createElement ( <span
class="methodparam">string $name [,
string
$value ] )
This function creates a new instance of class <span class="classname">DOMElement. 此节点出现在文档中,除非是用诸如 DOMNode->appendChild 等函数来将其插入。
参数
name
The tag name of the element.
value
The value of the element. By default, an empty element will be created.
The value can also be set later with
DOMElement::$nodeValue.
The value is used verbatim except that the \< and > entity references will escaped. Note that & has to be manually escaped; otherwise it is regarded as starting an entity reference. Also " won't be escaped.
返回值
Returns a new instance of class <span
class="classname">DOMElement or false if an error occurred.
错误/异常
DOM_INVALID_CHARACTER_ERR
Raised if name contains an invalid character.
范例
示例 #1 Creating a new element and inserting it as root
<?php
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement('test', 'This is the root element!');
// We insert the new element as root (child of the document)
$dom->appendChild($element);
echo $dom->saveXML();
?>
以上例程会输出:
<?xml version="1.0" encoding="utf-8"?>
<test>This is the root element!</test>
示例 #2 Passing text containing an unescaped & as value
<?php
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement('foo', 'me & you');
$dom->appendChild($element);
echo $dom->saveXML();
?>
以上例程的输出类似于:
Warning: DOMDocument::createElement(): unterminated entity reference you in /in/BjTCg on line 4
<?xml version="1.0" encoding="utf-8"?>
<foo/>
注释
Note:
The
valuewill not be escaped. Use <span class="methodname">DOMDocument::createTextNode to create a text node with escaping support.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createElementNS
Create new element node with an associated namespace
说明
public <span
class="type">DOMElement <span
class="methodname">DOMDocument::createElementNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $qualifiedName [, <span
class="methodparam">string $value ]
)
This function creates a new element node with an associated namespace. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
参数
namespaceURI
The URI of the namespace.
qualifiedName
The qualified name of the element, as prefix:tagname.
value
The value of the element. By default, an empty element will be created.
You can also set the value later with
DOMElement::$nodeValue.
返回值
The new DOMElement or false if an
error occurred.
错误/异常
DOM_INVALID_CHARACTER_ERR
Raised if qualifiedName contains an invalid character.
DOM_NAMESPACE_ERR
Raised if qualifiedName is a maformed qualified name.
范例
示例 #1 Creating a new element and inserting it as root
<?php
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElementNS('http://www.example.com/XFoo', 'xfoo:test', 'This is the root element!');
// We insert the new element as root (child of the document)
$dom->appendChild($element);
echo $dom->saveXML();
?>
以上例程会输出:
<?xml version="1.0" encoding="utf-8"?>
<xfoo:test xmlns:xfoo="http://www.example.com/XFoo">This is the root element!</xfoo:test>
示例 #2 A namespace prefix example
<?php
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$root = $doc->createElementNS('http://www.w3.org/2005/Atom', 'element');
$doc->appendChild($root);
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:g', 'http://base.google.com/ns/1.0');
$item = $doc->createElementNS('http://base.google.com/ns/1.0', 'g:item_type', 'house');
$root->appendChild($item);
echo $doc->saveXML(), "\n";
echo $item->namespaceURI, "\n"; // Outputs: http://base.google.com/ns/1.0
echo $item->prefix, "\n"; // Outputs: g
echo $item->localName, "\n"; // Outputs: item_type
?>
以上例程会输出:
<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
<g:item_type>house</g:item_type>
</element>
http://base.google.com/ns/1.0
g
item_type
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createEntityReference
Create new entity reference node
说明
public <span
class="type">DOMEntityReference <span
class="methodname">DOMDocument::createEntityReference ( <span
class="methodparam">string $name )
This function creates a new instance of class <span class="classname">DOMEntityReference. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
参数
name
The content of the entity reference, e.g. the entity reference minus the
leading & and the trailing ; characters.
返回值
The new DOMEntityReference or false
if an error occurred.
错误/异常
DOM_INVALID_CHARACTER_ERR
Raised if name contains an invalid character.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- <span class="methodname">DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createProcessingInstruction
Creates new PI node
说明
public <span
class="type">DOMProcessingInstruction <span
class="methodname">DOMDocument::createProcessingInstruction (
string
$target [, <span
class="type">string $data ] )
This function creates a new instance of class <span class="classname">DOMProcessingInstruction. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
参数
target
The target of the processing instruction.
data
The content of the processing instruction.
返回值
The new DOMProcessingInstruction or
false if an error occurred.
错误/异常
DOM_INVALID_CHARACTER_ERR
Raised if target contains an invalid character.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- DOMDocument::createTextNode
DOMDocument::createTextNode
Create new text node
说明
public DOMText
DOMDocument::createTextNode ( <span
class="methodparam">string $content )
This function creates a new instance of class <span class="classname">DOMText. 此节点出现在文档中,除非是用诸如 <span class="function">DOMNode->appendChild 等函数来将其插入。
参数
content
The content of the text.
返回值
The new DOMText or false if an
error occurred.
参见
- DOMNode::appendChild
- DOMDocument::createAttribute
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- <span class="methodname">DOMDocument::createProcessingInstruction
DOMDocument::getElementById
Searches for an element with a certain id
说明
public <span
class="type">DOMElement <span
class="methodname">DOMDocument::getElementById ( <span
class="methodparam">string $elementId
)
This function is similar to but searches for an element with a given id.
For this function to work, you will need either to set some ID attributes with or a DTD which defines an attribute to be of type ID. In the later case, you will need to validate your document with or DOMDocument::$validateOnParse before using this function.
参数
elementId
The unique id value for an element.
返回值
Returns the DOMElement or null if
the element is not found.
范例
示例 #1 DOMDocument::getElementById() Example
以下范例使用了 book.xml,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books [
<!ELEMENT books (book+)>
<!ELEMENT book (title, author+, xhtml:blurb?)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT blurb (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ATTLIST books xmlns CDATA #IMPLIED>
<!ATTLIST books xmlns:xhtml CDATA #IMPLIED>
<!ATTLIST book id ID #IMPLIED>
<!ATTLIST author email CDATA #IMPLIED>
]>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<books xmlns="http://books.php/" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<book id="php-basics">
<title>PHP Basics</title>
<author email="[email protected]">Jim Smith</author>
<author email="[email protected]">Jane Smith</author>
<xhtml:blurb><![CDATA[
<p><em>PHP Basics</em> provides an introduction to PHP.</p>
]]></xhtml:blurb>
</book>
<book id="php-advanced">
<title>PHP Advanced Programming</title>
<author email="[email protected]">Jon Doe</author>
</book>
</books>
<?php
$doc = new DomDocument;
// We need to validate our document before referring to the id
$doc->validateOnParse = true;
$doc->Load('book.xml');
echo "The element whose id is 'php-basics' is: " . $doc->getElementById('php-basics')->tagName . "\n";
?>
以上例程会输出:
The element whose id is 'php-basics' is: book
参见
- DOMDocument::getElementsByTagName
DOMDocument::getElementsByTagName
Searches for all elements with given local tag name
说明
public <span
class="type">DOMNodeList <span
class="methodname">DOMDocument::getElementsByTagName ( <span
class="methodparam">string $name )
This function returns a new instance of class <span class="classname">DOMNodeList containing all the elements with a given local tag name.
参数
name
The local name (without namespace) of the tag to match on. The special
value * matches all tags.
返回值
A new DOMNodeList object containing all the matched elements.
范例
示例 #1 Basic Usage Example
<?php
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>Patterns of Enterprise Application Architecture</book>
<book>Design Patterns: Elements of Reusable Software Design</book>
<book>Clean Code</book>
</books>
XML;
$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
echo $book->nodeValue, PHP_EOL;
}
?>
以上例程会输出:
Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
参见
- DOMDocument::getElementsByTagNameNS
DOMDocument::getElementsByTagNameNS
Searches for all elements with given tag name in specified namespace
说明
public <span
class="type">DOMNodeList <span
class="methodname">DOMDocument::getElementsByTagNameNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
Returns a DOMNodeList of all elements with a given local name and a namespace URI.
参数
namespaceURI
The namespace URI of the elements to match on. The special value *
matches all namespaces.
localName
The local name of the elements to match on. The special value *
matches all local names.
返回值
A new DOMNodeList object containing all the matched elements.
范例
示例 #1 Get all the XInclude elements
<?php
$xml = <<<EOD
<?xml version="1.0" ?>
<chapter xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Books of the other guy..</title>
<para>
<xi:include href="book.xml">
<xi:fallback>
<error>xinclude: book.xml not found</error>
</xi:fallback>
</xi:include>
<include>
This is another namespace
</include>
</para>
</chapter>
EOD;
$dom = new DOMDocument;
// load the XML string defined above
$dom->loadXML($xml);
foreach ($dom->getElementsByTagNameNS('http://www.w3.org/2001/XInclude', '*') as $element) {
echo 'local name: ', $element->localName, ', prefix: ', $element->prefix, "\n";
}
?>
以上例程会输出:
local name: include, prefix: xi
local name: fallback, prefix: xi
参见
- DOMDocument::getElementsByTagName
DOMDocument::importNode
Import node into current document
说明
public DOMNode
DOMDocument::importNode ( <span
class="methodparam">DOMNode
$importedNode [, <span
class="type">bool $deep =
false ] )
This function returns a copy of the node to import and associates it with the current document.
参数
importedNode
The node to import.
deep
If set to true, this method will recursively import the subtree
under the importedNode.
Note:
To copy the nodes attributes
deepneeds to be set totrue
返回值
The copied node or false, if it cannot be copied.
错误/异常
DOMException is thrown if node cannot be imported.
范例
示例 #1 DOMDocument::importNode example
Copying nodes between documents.
<?php
$orgdoc = new DOMDocument;
$orgdoc->loadXML("<root><element><child>text in child</child></element></root>");
// The node we want to import to a new document
$node = $orgdoc->getElementsByTagName("element")->item(0);
// Create a new document
$newdoc = new DOMDocument;
$newdoc->formatOutput = true;
// Add some markup
$newdoc->loadXML("<root><someelement>text in some element</someelement></root>");
echo "The 'new document' before copying nodes into it:\n";
echo $newdoc->saveXML();
// Import the node, and all its children, to the document
$node = $newdoc->importNode($node, true);
// And then append it to the "<root>" node
$newdoc->documentElement->appendChild($node);
echo "\nThe 'new document' after copying the nodes into it:\n";
echo $newdoc->saveXML();
?>
以上例程会输出:
The 'new document' before copying nodes into it:
<?xml version="1.0"?>
<root>
<someelement>text in some element</someelement>
</root>
The 'new document' after copying the nodes into it:
<?xml version="1.0"?>
<root>
<someelement>text in some element</someelement>
<element>
<child>text in child</child>
</element>
</root>
DOMDocument::load
Load XML from a file
说明
public mixed
DOMDocument::load ( <span
class="methodparam">string $filename
[, int
$options = 0 ] )
Loads an XML document from a file.
Warning
Unix style paths with forward slashes can cause significant performance degradation on Windows systems; be sure to call <span class="function">realpath in such a case.
参数
filename
The path to the XML document.
options
Bitwise OR
of the
libxml option constants.
返回值
成功时返回 true, 或者在失败时返回 false。 If called
statically, returns a DOMDocument
或者在失败时返回 false.
错误/异常
If an empty string is passed as the filename or an empty file is
named, a warning will be generated. This warning is not generated by
libxml and cannot be handled using libxml's error handling functions.
此方法可以被静态调用,但会抛出一个 E_STRICT 错误。
范例
示例 #1 Creating a Document
<?php
$doc = new DOMDocument();
$doc->load('book.xml');
echo $doc->saveXML();
?>
参见
- DOMDocument::loadXML
- DOMDocument::save
- DOMDocument::saveXML
DOMDocument::loadHTML
Load HTML from a string
说明
public bool
DOMDocument::loadHTML ( <span
class="methodparam">string $source
[, int
$options = 0 ] )
The function parses the HTML contained in the string source. Unlike
loading XML, HTML does not have to be well-formed to load. This function
may also be called statically to load and create a <span
class="classname">DOMDocument object. The static invocation may
be used when no DOMDocument properties
need to be set prior to loading.
参数
source
The HTML string.
options
Since PHP 5.4.0 and Libxml 2.6.0, you may also use the options
parameter to specify
additional Libxml parameters.
返回值
成功时返回 true, 或者在失败时返回 false。 If called
statically, returns a DOMDocument
或者在失败时返回 false.
错误/异常
If an empty string is passed as the source, a warning will be
generated. This warning is not generated by libxml and cannot be handled
using libxml's error handling functions.
此方法可以被静态调用,但会抛出一个 E_STRICT 错误。
尽管非正确格式化的 HTML 仍应该被成功调入,但此函数会在遇到错误标记时产生
E_WARNING
错误。libxml 错误处理函数可以用来处理这类错误。
范例
示例 #1 Creating a Document
<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body>Test<br></body></html>");
echo $doc->saveHTML();
?>
参见
- DOMDocument::loadHTMLFile
- DOMDocument::saveHTML
- DOMDocument::saveHTMLFile
DOMDocument::loadHTMLFile
Load HTML from a file
说明
public bool
DOMDocument::loadHTMLFile ( <span
class="methodparam">string $filename
[, int
$options = 0 ] )
The function parses the HTML document in the file named filename.
Unlike loading XML, HTML does not have to be well-formed to load.
参数
filename
The path to the HTML file.
options
Since PHP 5.4.0 and Libxml 2.6.0, you may also use the options
parameter to specify
additional Libxml parameters.
返回值
成功时返回 true, 或者在失败时返回 false。 If called
statically, returns a DOMDocument
或者在失败时返回 false.
错误/异常
If an empty string is passed as the filename or an empty file is
named, a warning will be generated. This warning is not generated by
libxml and cannot be handled using
libxml's error handling functions.
此方法可以被静态调用,但会抛出一个 E_STRICT 错误。
尽管非正确格式化的 HTML 仍应该被成功调入,但此函数会在遇到错误标记时产生
E_WARNING
错误。libxml 错误处理函数可以用来处理这类错误。
范例
示例 #1 Creating a Document
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("filename.html");
echo $doc->saveHTML();
?>
参见
- DOMDocument::loadHTML
- DOMDocument::saveHTML
- DOMDocument::saveHTMLFile
DOMDocument::loadXML
Load XML from a string
说明
public mixed
DOMDocument::loadXML ( <span
class="methodparam">string $source
[, int
$options = 0 ] )
Loads an XML document from a string.
参数
source
The string containing the XML.
options
Bitwise OR
of the
libxml option constants.
返回值
成功时返回 true, 或者在失败时返回 false。 If called
statically, returns a DOMDocument
或者在失败时返回 false.
错误/异常
If an empty string is passed as the source, a warning will be
generated. This warning is not generated by libxml and cannot be handled
using libxml's error handling functions.
此方法可以被静态调用,但会抛出一个 E_STRICT 错误。
范例
示例 #1 Creating a Document
<?php
$doc = new DOMDocument();
$doc->loadXML('<root><node/></root>');
echo $doc->saveXML();
?>
示例 #2 Static invocation of loadXML
<?php
// Issues an E_STRICT error
$doc = DOMDocument::loadXML('<root><node/></root>');
echo $doc->saveXML();
?>
参见
- DOMDocument::load
- DOMDocument::save
- DOMDocument::saveXML
DOMDocument::normalizeDocument
Normalizes the document
说明
public void DOMDocument::normalizeDocument ( <span class="methodparam">void )
This method acts as if you saved and then loaded the document, putting the document in a "normal" form.
返回值
没有返回值。
参见
- » The DOM Specification
- DOMNode::normalize
DOMDocument::registerNodeClass
Register extended class used to create base node type
说明
public bool
DOMDocument::registerNodeClass ( <span
class="methodparam">string $baseclass
, string
$extendedclass )
This method allows you to register your own extended DOM class to be used afterward by the PHP DOM extension.
This method is not part of the DOM standard.
参数
baseclass
The DOM class that you want to extend. You can find a list of these
classes in the
chapter introduction.
extendedclass
Your extended class name. If null is provided, any previously
registered class extending baseclass will be removed.
返回值
成功时返回 true, 或者在失败时返回 false。
范例
示例 #1 Adding a new method to DOMElement to ease our code
<?php
class myElement extends DOMElement {
function appendElement($name) {
return $this->appendChild(new myElement($name));
}
}
class myDocument extends DOMDocument {
function setRoot($name) {
return $this->appendChild(new myElement($name));
}
}
$doc = new myDocument();
$doc->registerNodeClass('DOMElement', 'myElement');
// From now on, adding an element to another costs only one method call !
$root = $doc->setRoot('root');
$child = $root->appendElement('child');
$child->setAttribute('foo', 'bar');
echo $doc->saveXML();
?>
以上例程会输出:
<?xml version="1.0"?>
<root><child foo="bar"/></root>
示例 #2 Retrieving elements as custom class
<?php
class myElement extends DOMElement {
public function __toString() {
return $this->nodeValue;
}
}
$doc = new DOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");
$doc->registerNodeClass("DOMElement", "myElement");
$element = $doc->getElementsByTagName("child")->item(0);
var_dump(get_class($element));
// And take advantage of the __toString method..
echo $element;
?>
以上例程会输出:
string(9) "myElement"
text in child
示例 #3 Retrieving owner document
When instantiating a custom DOMDocument
the ownerDocument property will refer to the instantiated class,
meaning there is no need (and in fact not possible) to use <span
class="function">DOMDocument::registerNodeClass with <span
class="classname">DOMDocument
<?php
class myDOMDocument extends DOMDocument {
}
class myOtherDOMDocument extends DOMDocument {
}
// Create myDOMDocument with some XML
$doc = new myDOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");
$child = $doc->getElementsByTagName("child")->item(0);
// The current owner of the node is myDOMDocument
var_dump(get_class($child->ownerDocument));
// Import a node from myDOMDocument
$newdoc = new myOtherDOMDocument;
$child = $newdoc->importNode($child);
// The new owner of the node has changed to myOtherDOMDocument
var_dump(get_class($child->ownerDocument));
?>
以上例程会输出:
string(13) "myDOMDocument"
string(18) "myOtherDOMDocument"
示例 #4 Custom objects are transient
Caution
Objects of the registered node classes are transient, i.e. they are destroyed when they are no longer referenced from PHP code, and recreated when being retrieved again. That implies that custom property values will be lost after recreation.
<?php
class MyDOMElement extends DOMElement
{
public $myProp = 'default value';
}
$doc = new DOMDocument();
$doc->registerNodeClass('DOMElement', 'MyDOMElement');
$node = $doc->createElement('a');
$node->myProp = 'modified value';
$doc->appendChild($node);
echo $doc->childNodes[0]->myProp, PHP_EOL;
unset($node);
echo $doc->childNodes[0]->myProp, PHP_EOL;
?>
以上例程会输出:
modified value
default value
DOMDocument::relaxNGValidate
Performs relaxNG validation on the document
说明
public bool
DOMDocument::relaxNGValidate ( <span
class="methodparam">string $filename
)
Performs » relaxNG validation on the document based on the given RNG schema.
参数
filename
The RNG file.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- DOMDocument::relaxNGValidateSource
- DOMDocument::schemaValidate
- DOMDocument::schemaValidateSource
- DOMDocument::validate
DOMDocument::relaxNGValidateSource
Performs relaxNG validation on the document
说明
public bool
DOMDocument::relaxNGValidateSource (
string
$source )
Performs » relaxNG validation on the document based on the given RNG source.
参数
source
A string containing the RNG schema.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- DOMDocument::relaxNGValidate
- DOMDocument::schemaValidate
- DOMDocument::schemaValidateSource
- DOMDocument::validate
DOMDocument::save
Dumps the internal XML tree back into a file
说明
public int <span
class="methodname">DOMDocument::save ( <span
class="methodparam">string $filename
[, int
$options = 0 ] )
Creates an XML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.
参数
filename
The path to the saved XML document.
options
Additional Options. Currently only
LIBXML_NOEMPTYTAG is
supported.
返回值
Returns the number of bytes written or false if an error occurred.
范例
示例 #1 Saving a DOM tree into a file
<?php
$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo 'Wrote: ' . $doc->save("/tmp/test.xml") . ' bytes'; // Wrote: 72 bytes
?>
参见
- DOMDocument::saveXML
- DOMDocument::load
- DOMDocument::loadXML
DOMDocument::saveHTML
Dumps the internal document into a string using HTML formatting
说明
public string
DOMDocument::saveHTML ([ <span
class="methodparam">DOMNode $node<span
class="initializer"> = NULL ] )
Creates an HTML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.
参数
node
Optional parameter to output a subset of the document.
返回值
Returns the HTML, or false if an error occurred.
范例
示例 #1 Saving a HTML tree into a string
<?php
$doc = new DOMDocument('1.0');
$root = $doc->createElement('html');
$root = $doc->appendChild($root);
$head = $doc->createElement('head');
$head = $root->appendChild($head);
$title = $doc->createElement('title');
$title = $head->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo $doc->saveHTML();
?>
参见
- DOMDocument::saveHTMLFile
- DOMDocument::loadHTML
- DOMDocument::loadHTMLFile
DOMDocument::saveHTMLFile
Dumps the internal document into a file using HTML formatting
说明
public int <span
class="methodname">DOMDocument::saveHTMLFile ( <span
class="methodparam">string $filename
)
Creates an HTML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.
参数
filename
The path to the saved HTML document.
返回值
Returns the number of bytes written or false if an error occurred.
范例
示例 #1 Saving a HTML tree into a file
<?php
$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;
$root = $doc->createElement('html');
$root = $doc->appendChild($root);
$head = $doc->createElement('head');
$head = $root->appendChild($head);
$title = $doc->createElement('title');
$title = $head->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo 'Wrote: ' . $doc->saveHTMLFile("/tmp/test.html") . ' bytes'; // Wrote: 129 bytes
?>
参见
- DOMDocument::saveHTML
- DOMDocument::loadHTML
- DOMDocument::loadHTMLFile
DOMDocument::saveXML
Dumps the internal XML tree back into a string
说明
public string
DOMDocument::saveXML ([ <span
class="methodparam">DOMNode $node [,
int $options<span
class="initializer"> = 0 ]] )
Creates an XML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.
参数
node
Use this parameter to output only a specific node without XML
declaration rather than the entire document.
options
Additional Options. Currently only
LIBXML_NOEMPTYTAG is
supported.
返回值
Returns the XML, or false if an error occurred.
错误/异常
DOM_WRONG_DOCUMENT_ERR
Raised if node is from another document.
范例
示例 #1 Saving a DOM tree into a string
<?php
$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo "Saving all the document:\n";
echo $doc->saveXML() . "\n";
echo "Saving only the title part:\n";
echo $doc->saveXML($title);
?>
以上例程会输出:
Saving all the document:
<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
Saving only the title part:
<title>This is the title</title>
参见
- DOMDocument::save
- DOMDocument::load
- DOMDocument::loadXML
DOMDocument::schemaValidate
Validates a document based on a schema. Only XML Schema 1.0 is supported.
说明
public bool
DOMDocument::schemaValidate ( <span
class="methodparam">string $filename
[, int
$flags = 0 ] )
Validates a document based on the given schema file.
参数
filename
The path to the schema.
flags
A bitmask of Libxml schema validation flags. Currently the only
supported value is
LIBXML_SCHEMA_CREATE.
Available since PHP 5.5.2 and Libxml 2.6.14.
返回值
成功时返回 true, 或者在失败时返回 false。
更新日志
| 版本 | 说明 |
|---|---|
| 5.5.2 | Added the flags parameter |
参见
- DOMDocument::schemaValidateSource
- DOMDocument::relaxNGValidate
- DOMDocument::relaxNGValidateSource
- DOMDocument::validate
DOMDocument::schemaValidateSource
Validates a document based on a schema
说明
public bool
DOMDocument::schemaValidateSource (
string
$source [, <span
class="type">int $flags ] )
Validates a document based on a schema defined in the given string.
参数
source
A string containing the schema.
flags
A bitmask of Libxml schema validation flags. Currently the only
supported value is
LIBXML_SCHEMA_CREATE.
Available since PHP 5.5.2 and Libxml 2.6.14.
返回值
成功时返回 true, 或者在失败时返回 false。
更新日志
| 版本 | 说明 |
|---|---|
| 5.5.2 | Added the flags parameter |
参见
- DOMDocument::schemaValidate
- DOMDocument::relaxNGValidate
- DOMDocument::relaxNGValidateSource
- DOMDocument::validate
DOMDocument::validate
Validates the document based on its DTD
说明
public bool DOMDocument::validate ( <span class="methodparam">void )
Validates the document based on its DTD.
You can also use the validateOnParse property of <span class="classname">DOMDocument to make a DTD validation.
返回值
成功时返回 true, 或者在失败时返回 false。 If the document
has no DTD attached, this method will return false.
范例
示例 #1 Example of DTD validation
<?php
$dom = new DOMDocument;
$dom->load('book.xml');
if ($dom->validate()) {
echo "This document is valid!\n";
}
?>
You can also validate your XML file while loading it:
<?php
$dom = new DOMDocument;
$dom->validateOnParse = true;
$dom->load('book.xml');
?>
参见
- DOMDocument::schemaValidate
- DOMDocument::schemaValidateSource
- DOMDocument::relaxNGValidate
- DOMDocument::relaxNGValidateSource
DOMDocument::xinclude
Substitutes XIncludes in a DOMDocument Object
说明
public int <span
class="methodname">DOMDocument::xinclude ([ <span
class="methodparam">int $options<span
class="initializer"> = 0 ] )
This method substitutes » XIncludes in a DOMDocument object.
Note:
Due to libxml2 automatically resolving entities, this method will produce unexpected results if the included XML file have an attached DTD.
参数
options
libxml parameters.
Available since PHP 5.1.0 and Libxml 2.6.7.
返回值
Returns the number of XIncludes in the document, -1 if some processing
failed, or false if there were no substitutions.
范例
示例 #1 DOMDocument::xinclude() example
<?php
$xml = <<<EOD
<?xml version="1.0" ?>
<chapter xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Books of the other guy..</title>
<para>
<xi:include href="book.xml">
<xi:fallback>
<error>xinclude: book.xml not found</error>
</xi:fallback>
</xi:include>
</para>
</chapter>
EOD;
$dom = new DOMDocument;
// let's have a nice output
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
// load the XML string defined above
$dom->loadXML($xml);
// substitute xincludes
$dom->xinclude();
echo $dom->saveXML();
?>
以上例程的输出类似于:
<?xml version="1.0"?>
<chapter xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Books of the other guy..</title>
<para>
<row xml:base="/home/didou/book.xml">
<entry>The Grapes of Wrath</entry>
<entry>John Steinbeck</entry>
<entry>en</entry>
<entry>0140186409</entry>
</row>
<row xml:base="/home/didou/book.xml">
<entry>The Pearl</entry>
<entry>John Steinbeck</entry>
<entry>en</entry>
<entry>014017737X</entry>
</row>
<row xml:base="/home/didou/book.xml">
<entry>Samarcande</entry>
<entry>Amine Maalouf</entry>
<entry>fr</entry>
<entry>2253051209</entry>
</row>
</para>
</chapter>
类摘要
DOMDocumentFragment
class DOMDocumentFragment <span class="ooclass"> extends DOMNode {
/* 属性 */
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public bool
appendXML ( <span
class="methodparam">string $data )
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
DOMDocumentFragment::appendXML
Append raw XML data
说明
public bool
DOMDocumentFragment::appendXML ( <span
class="methodparam">string $data )
Appends raw XML data to a DOMDocumentFragment.
This method is not part of the DOM standard. It was created as a simpler approach for appending an XML DocumentFragment in a DOMDocument.
If you want to stick to the standards, you will have to create a temporary DOMDocument with a dummy root and then loop through the child nodes of the root of your XML data to append them.
参数
data
XML to append.
返回值
成功时返回 true, 或者在失败时返回 false。
范例
示例 #1 Appending XML data to your document
<?php
$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();
?>
以上例程会输出:
<?xml version="1.0"?>
<root><foo>text</foo><bar>text2</bar></root>
简介
Each DOMDocument has a doctype
attribute whose value is either null or a <span
class="classname">DOMDocumentType object.
类摘要
DOMDocumentType
class DOMDocumentType <span class="ooclass"> extends DOMNode {
/* 属性 */
public <span
class="modifier">readonly string
$publicId ;
public <span
class="modifier">readonly string
$systemId ;
public <span
class="modifier">readonly string
$name ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMap $entities ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMap $notations ;
public <span
class="modifier">readonly string
$internalSubset ;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
publicId
The public identifier of the external subset.
systemId
The system identifier of the external subset. This may be an absolute
URI or not.
name
The name of DTD; i.e., the name immediately following the DOCTYPE
keyword.
entities
A DOMNamedNodeMap containing the general
entities, both external and internal, declared in the DTD.
notations
A DOMNamedNodeMap containing the
notations declared in the DTD.
internalSubset
The internal subset as a string, or null if there is none. This does not
contain the delimiting square brackets.
类摘要
DOMElement
class DOMElement <span class="ooclass"> extends DOMNode {
/* 属性 */
public <span
class="modifier">readonly bool
$schemaTypeInfo ;
public <span
class="modifier">readonly string
$tagName ;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">string $name [,
string
$value [, <span
class="type">string $namespaceURI ]] )
public string
getAttribute ( <span
class="methodparam">string $name )
public DOMAttr
getAttributeNode ( <span
class="methodparam">string $name )
public DOMAttr
getAttributeNodeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
public string
getAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
public <span
class="type">DOMNodeList <span
class="methodname">getElementsByTagName ( <span
class="methodparam">string $name )
public <span
class="type">DOMNodeList <span
class="methodname">getElementsByTagNameNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
public bool
hasAttribute ( <span
class="methodparam">string $name )
public bool
hasAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
public bool
removeAttribute ( <span
class="methodparam">string $name )
public bool
removeAttributeNode ( <span
class="methodparam">DOMAttr $oldnode
)
public bool
removeAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
public DOMAttr
setAttribute ( <span
class="methodparam">string $name ,
string
$value )
public DOMAttr
setAttributeNode ( <span
class="methodparam">DOMAttr $attr )
public DOMAttr
setAttributeNodeNS ( <span
class="methodparam">DOMAttr $attr )
public void
setAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $qualifiedName , <span
class="methodparam">string $value )
public void
setIdAttribute ( <span
class="methodparam">string $name ,
bool $isId
)
public void
setIdAttributeNode ( <span
class="methodparam">DOMAttr $attr ,
bool $isId
)
public void
setIdAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName , <span
class="methodparam">bool $isId )
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
schemaTypeInfo
Not implemented yet, always return null
tagName
The element name
注释
Note:
此 DOM 扩展采用 UTF-8 编码。在 ISO-8859-1 编码下,使用 <span class="function">utf8_encode 和 <span class="function">utf8_decode 来处理,其它编码下使用 iconv 函数处理。
DOMElement::__construct
Creates a new DOMElement object
说明
public <span
class="methodname">DOMElement::__construct ( <span
class="methodparam">string $name [,
string
$value [, <span
class="type">string $namespaceURI ]] )
Creates a new DOMElement object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use or .
参数
name
The tag name of the element. When also passing in namespaceURI, the
element name may take a prefix to be associated with the URI.
value
The value of the element.
namespaceURI
A namespace URI to create the element within a specific namespace.
范例
示例 #1 Creating a new DOMElement
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$element_ns = new DOMElement('pr:node1', 'thisvalue', 'http://xyz');
$element->appendChild($element_ns);
echo $dom->saveXML(); /* <?xml version="1.0" encoding="utf-8"?>
<root><pr:node1 xmlns:pr="http://xyz">thisvalue</pr:node1></root> */
?>
参见
- DOMDocument::createElement
- DOMDocument::createElementNS
DOMElement::getAttribute
Returns value of attribute
说明
public string
DOMElement::getAttribute ( <span
class="methodparam">string $name )
Gets the value of the attribute with name name for the current node.
参数
name
The name of the attribute.
返回值
The value of the attribute, or an empty string if no attribute with the
given name is found.
参见
- DOMElement::hasAttribute
- DOMElement::setAttribute
- DOMElement::removeAttribute
DOMElement::getAttributeNode
Returns attribute node
说明
public DOMAttr
DOMElement::getAttributeNode ( <span
class="methodparam">string $name )
Returns the attribute node with name name for the current element.
参数
name
The name of the attribute.
返回值
The attribute node. Note that for XML namespace declarations (xmlns and xmlns:* attributes) an instance of <span class="classname">DOMNameSpaceNode is returned instead of a <span class="classname">DOMAttr.
参见
- DOMElement::hasAttribute
- DOMElement::setAttributeNode
- DOMElement::removeAttributeNode
DOMElement::getAttributeNodeNS
Returns attribute node
说明
public DOMAttr
DOMElement::getAttributeNodeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
Returns the attribute node in namespace namespaceURI with local name
localName for the current node.
参数
namespaceURI
The namespace URI.
localName
The local name.
返回值
The attribute node. Note that for XML namespace declarations (xmlns and xmlns:* attributes) an instance of <span class="classname">DOMNameSpaceNode is returned instead of a <span class="classname">DOMAttr object.
参见
- DOMElement::hasAttributeNS
- DOMElement::setAttributeNodeNS
- DOMElement::removeAttributeNode
DOMElement::getAttributeNS
Returns value of attribute
说明
public string
DOMElement::getAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
Gets the value of the attribute in namespace namespaceURI with local
name localName for the current node.
参数
namespaceURI
The namespace URI.
localName
The local name.
返回值
The value of the attribute, or an empty string if no attribute with the
given localName and namespaceURI is found.
参见
- DOMElement::hasAttributeNS
- DOMElement::setAttributeNS
- DOMElement::removeAttributeNS
DOMElement::getElementsByTagName
Gets elements by tagname
说明
public <span
class="type">DOMNodeList <span
class="methodname">DOMElement::getElementsByTagName ( <span
class="methodparam">string $name )
This function returns a new instance of the class <span
class="classname">DOMNodeList of all descendant elements with a
given tag name, in the order in which they are encountered in a
preorder traversal of this element tree.
参数
name
The tag name. Use * to return all elements within the element tree.
返回值
This function returns a new instance of the class <span class="classname">DOMNodeList of all matched elements.
参见
- DOMElement::getElementsByTagNameNS
DOMElement::getElementsByTagNameNS
Get elements by namespaceURI and localName
说明
public <span
class="type">DOMNodeList <span
class="methodname">DOMElement::getElementsByTagNameNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
This function fetch all the descendant elements with a given localName
and namespaceURI.
参数
namespaceURI
The namespace URI.
localName
The local name. Use * to return all elements within the element tree.
返回值
This function returns a new instance of the class <span class="classname">DOMNodeList of all matched elements in the order in which they are encountered in a preorder traversal of this element tree.
参见
- DOMElement::getElementsByTagName
DOMElement::hasAttribute
Checks to see if attribute exists
说明
public bool
DOMElement::hasAttribute ( <span
class="methodparam">string $name )
Indicates whether attribute named name exists as a member of the
element.
参数
name
The attribute name.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- DOMElement::hasAttributeNS
- DOMElement::getAttribute
- DOMElement::setAttribute
- DOMElement::removeAttribute
DOMElement::hasAttributeNS
Checks to see if attribute exists
说明
public bool
DOMElement::hasAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
Indicates whether attribute in namespace namespaceURI named
localName exists as a member of the element.
参数
namespaceURI
The namespace URI.
localName
The local name.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- DOMElement::hasAttribute
- DOMElement::getAttributeNS
- DOMElement::setAttributeNS
- DOMElement::removeAttributeNS
DOMElement::removeAttribute
Removes attribute
说明
public bool
DOMElement::removeAttribute ( <span
class="methodparam">string $name )
Removes attribute named name from the element.
参数
name
The name of the attribute.
返回值
成功时返回 true, 或者在失败时返回 false。
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
参见
- DOMElement::hasAttribute
- DOMElement::getAttribute
- DOMElement::setAttribute
DOMElement::removeAttributeNode
Removes attribute
说明
public bool
DOMElement::removeAttributeNode ( <span
class="methodparam">DOMAttr $oldnode
)
Removes attribute oldnode from the element.
参数
oldnode
The attribute node.
返回值
成功时返回 true, 或者在失败时返回 false。
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
DOM_NOT_FOUND_ERROR
Raised if oldnode is not an attribute of the element.
参见
- DOMElement::hasAttribute
- DOMElement::getAttributeNode
- DOMElement::setAttributeNode
DOMElement::removeAttributeNS
Removes attribute
说明
public bool
DOMElement::removeAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
Removes attribute localName in namespace namespaceURI from the
element.
参数
namespaceURI
The namespace URI.
localName
The local name.
返回值
成功时返回 true, 或者在失败时返回 false。
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
参见
- DOMElement::hasAttributeNS
- DOMElement::getAttributeNS
- DOMElement::setAttributeNS
DOMElement::setAttribute
Adds new attribute
说明
public DOMAttr
DOMElement::setAttribute ( <span
class="methodparam">string $name ,
string
$value )
Sets an attribute with name name to the given value. If the attribute
does not exist, it will be created.
参数
name
The name of the attribute.
value
The value of the attribute.
返回值
The new DOMAttr or false if an
error occurred.
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
范例
示例 #1 Setting an attribute
<?php
$doc = new DOMDocument("1.0");
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
$newnode->setAttribute("align", "left");
?>
参见
- DOMElement::hasAttribute
- DOMElement::getAttribute
- DOMElement::removeAttribute
DOMElement::setAttributeNode
Adds new attribute node to element
说明
public DOMAttr
DOMElement::setAttributeNode ( <span
class="methodparam">DOMAttr $attr )
Adds new attribute node attr to element.
参数
attr
The attribute node.
返回值
Returns old node if the attribute has been replaced or null.
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
参见
- DOMElement::hasAttribute
- DOMElement::getAttributeNode
- DOMElement::removeAttributeNode
DOMElement::setAttributeNodeNS
Adds new attribute node to element
说明
public DOMAttr
DOMElement::setAttributeNodeNS ( <span
class="methodparam">DOMAttr $attr )
Adds new attribute node attr to element.
参数
attr
The attribute node.
返回值
Returns the old node if the attribute has been replaced.
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
参见
- DOMElement::hasAttributeNS
- DOMElement::getAttributeNodeNS
- DOMElement::removeAttributeNode
DOMElement::setAttributeNS
Adds new attribute
说明
public void
DOMElement::setAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $qualifiedName , <span
class="methodparam">string $value )
Sets an attribute with namespace namespaceURI and name name to the
given value. If the attribute does not exist, it will be created.
参数
namespaceURI
The namespace URI.
qualifiedName
The qualified name of the attribute, as prefix:tagname.
value
The value of the attribute.
返回值
没有返回值。
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
DOM_NAMESPACE_ERR
Raised if qualifiedName is a malformed qualified name, or if
qualifiedName has a prefix and namespaceURI is null.
参见
- DOMElement::hasAttributeNS
- DOMElement::getAttributeNS
- DOMElement::removeAttributeNS
DOMElement::setIdAttribute
Declares the attribute specified by name to be of type ID
说明
public void
DOMElement::setIdAttribute ( <span
class="methodparam">string $name ,
bool $isId
)
Declares the attribute name to be of type ID.
参数
name
The name of the attribute.
isId
Set it to true if you want name to be of type ID, false
otherwise.
返回值
没有返回值。
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
DOM_NOT_FOUND
Raised if name is not an attribute of this element.
参见
- DOMDocument::getElementById
- DOMElement::setIdAttributeNode
- DOMElement::setIdAttributeNS
DOMElement::setIdAttributeNode
Declares the attribute specified by node to be of type ID
说明
public void
DOMElement::setIdAttributeNode ( <span
class="methodparam">DOMAttr $attr ,
bool $isId
)
Declares the attribute specified by attr to be of type ID.
参数
attr
The attribute node.
isId
Set it to true if you want name to be of type ID, false
otherwise.
返回值
没有返回值。
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
DOM_NOT_FOUND
Raised if name is not an attribute of this element.
参见
- DOMDocument::getElementById
- DOMElement::setIdAttribute
- DOMElement::setIdAttributeNS
DOMElement::setIdAttributeNS
Declares the attribute specified by local name and namespace URI to be of type ID
说明
public void
DOMElement::setIdAttributeNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName , <span
class="methodparam">bool $isId )
Declares the attribute specified by localName and namespaceURI to be
of type ID.
参数
namespaceURI
The namespace URI of the attribute.
localName
The local name of the attribute, as prefix:tagname.
isId
Set it to true if you want name to be of type ID, false
otherwise.
返回值
没有返回值。
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if the node is readonly.
DOM_NOT_FOUND
Raised if name is not an attribute of this element.
参见
- DOMDocument::getElementById
- DOMElement::setIdAttribute
- DOMElement::setIdAttributeNode
简介
This interface represents a known entity, either parsed or unparsed, in an XML document.
类摘要
DOMEntity
class DOMEntity <span class="ooclass"> extends DOMNode {
/* 属性 */
public <span
class="modifier">readonly string
$publicId ;
public <span
class="modifier">readonly string
$systemId ;
public <span
class="modifier">readonly string
$notationName ;
public string
$actualEncoding ;
public <span
class="modifier">readonly string
$encoding ;
public <span
class="modifier">readonly string
$version ;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
publicId
The public identifier associated with the entity if specified, and
null otherwise.
systemId
The system identifier associated with the entity if specified, and
null otherwise. This may be an absolute URI or not.
notationName
For unparsed entities, the name of the notation for the entity. For
parsed entities, this is null.
actualEncoding
An attribute specifying the encoding used for this entity at the time of
parsing, when it is an external parsed entity. This is null if it
an entity from the internal subset or if it is not known.
encoding
An attribute specifying, as part of the text declaration, the encoding
of this entity, when it is an external parsed entity. This is null
otherwise.
version
An attribute specifying, as part of the text declaration, the version
number of this entity, when it is an external parsed entity. This is
null otherwise.
类摘要
DOMEntityReference
class DOMEntityReference <span class="ooclass"> extends DOMNode {
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">string $name )
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
DOMEntityReference::__construct
Creates a new DOMEntityReference object
说明
public <span
class="methodname">DOMEntityReference::__construct ( <span
class="methodparam">string $name )
Creates a new DOMEntityReference object.
参数
name
The name of the entity reference.
范例
示例 #1 Creating a new DOMEntityReference
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$entity = $element->appendChild(new DOMEntityReference('nbsp'));
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?><root> </root> */
?>
参见
- DOMDocument::createEntityReference
简介
DOM operations raise exceptions under particular circumstances, i.e., when an operation is impossible to perform for logical reasons.
See also 异常处理.
类摘要
DOMException
class DOMException <span class="ooclass"> extends Exception {
/* 属性 */
public <span
class="modifier">readonly int $code ;
/* 继承的属性 */
protected string
$message ;
protected int
$code ;
protected string
$file ;
protected int
$line ;
/* 继承的方法 */
final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )
public string Exception::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )
}
属性
code
An integer indicating the type of error generated
简介
The DOMImplementation class provides a number of methods for performing operations that are independent of any particular instance of the document object model.
类摘要
DOMImplementation
class DOMImplementation {
/* 属性 */
/* 方法 */
__construct ( <span class="methodparam">void )
public <span
class="type">DOMDocument <span
class="methodname">createDocument ([ <span
class="methodparam">string
$namespaceURI = null [,
string
$qualifiedName = null
[, DOMDocumentType
$doctype = null ]]] )
public <span
class="type">DOMDocumentType <span
class="methodname">createDocumentType ([ <span
class="methodparam">string
$qualifiedName = null
[, string
$publicId = null [,
string
$systemId = null ]]]
)
public bool
hasFeature ( <span
class="methodparam">string $feature ,
string
$version )
}
DOMImplementation::__construct
Creates a new DOMImplementation object
说明
DOMImplementation::__construct ( <span class="methodparam">void )
Creates a new DOMImplementation object.
DOMImplementation::createDocument
Creates a DOMDocument object of the specified type with its document element
说明
public <span
class="type">DOMDocument <span
class="methodname">DOMImplementation::createDocument ([ <span
class="methodparam">string
$namespaceURI = null [,
string
$qualifiedName = null
[, DOMDocumentType
$doctype = null ]]] )
Creates a DOMDocument object of the specified type with its document element.
参数
namespaceURI
The namespace URI of the document element to create.
qualifiedName
The qualified name of the document element to create.
doctype
The type of document to create or null.
返回值
A new DOMDocument object. If
namespaceURI, qualifiedName, and doctype are null, the returned
DOMDocument is empty with no document
element
错误/异常
DOM_WRONG_DOCUMENT_ERR
Raised if doctype has already been used with a different document or
was created from a different implementation.
DOM_NAMESPACE_ERR
Raised if there is an error with the namespace, as determined by
namespaceURI and qualifiedName.
此方法可以被静态调用,但会抛出一个 E_STRICT 错误。
参见
- DOMDocument::__construct
- <span class="methodname">DOMImplementation::createDocumentType
DOMImplementation::createDocumentType
Creates an empty DOMDocumentType object
说明
public <span
class="type">DOMDocumentType <span
class="methodname">DOMImplementation::createDocumentType ([
string
$qualifiedName = null
[, string
$publicId = null [,
string
$systemId = null ]]]
)
Creates an empty DOMDocumentType object. Entity declarations and notations are not made available. Entity reference expansions and default attribute additions do not occur.
参数
qualifiedName
The qualified name of the document type to create.
publicId
The external subset public identifier.
systemId
The external subset system identifier.
返回值
A new DOMDocumentType node with its
ownerDocument set to null.
错误/异常
DOM_NAMESPACE_ERR
Raised if there is an error with the namespace, as determined by
qualifiedName.
此方法可以被静态调用,但会抛出一个 E_STRICT 错误。
范例
示例 #1 Creating a document with an attached DTD
<?php
// Creates an instance of the DOMImplementation class
$imp = new DOMImplementation;
// Creates a DOMDocumentType instance
$dtd = $imp->createDocumentType('graph', '', 'graph.dtd');
// Creates a DOMDocument instance
$dom = $imp->createDocument("", "", $dtd);
// Set other properties
$dom->encoding = 'UTF-8';
$dom->standalone = false;
// Create an empty element
$element = $dom->createElement('graph');
// Append the element
$dom->appendChild($element);
// Retrieve and print the document
echo $dom->saveXML();
?>
以上例程会输出:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE graph SYSTEM "graph.dtd">
<graph/>
参见
- DOMImplementation::createDocument
DOMImplementation::hasFeature
Test if the DOM implementation implements a specific feature
说明
public bool
DOMImplementation::hasFeature ( <span
class="methodparam">string $feature ,
string
$version )
Test if the DOM implementation implements a specific feature.
You can find a list of all features in the » Conformance section of the DOM specification.
参数
feature
The feature to test.
version
The version number of the feature to test. In level 2, this can be
either 2.0 or 1.0.
返回值
成功时返回 true, 或者在失败时返回 false。
错误/异常
此方法可以被静态调用,但会抛出一个 E_STRICT 错误。
范例
示例 #1 Testing your DOM Implementation
<?php
$features = array(
'Core' => 'Core module',
'XML' => 'XML module',
'HTML' => 'HTML module',
'Views' => 'Views module',
'Stylesheets' => 'Style Sheets module',
'CSS' => 'CSS module',
'CSS2' => 'CSS2 module',
'Events' => 'Events module',
'UIEvents' => 'User interface Events module',
'MouseEvents' => 'Mouse Events module',
'MutationEvents' => 'Mutation Events module',
'HTMLEvents' => 'HTML Events module',
'Range' => 'Range module',
'Traversal' => 'Traversal module'
);
foreach ($features as $key => $name) {
if (DOMImplementation::hasFeature($key, '2.0')) {
echo "Has feature $name\n";
} else {
echo "Missing feature $name\n";
}
}
?>
参见
- DOMNode::isSupported
类摘要
DOMNamedNodeMap
class DOMNamedNodeMap <span class="oointerface">implements <span class="interfacename">Traversable <span class="oointerface">, Countable {
/* 属性 */
public <span
class="modifier">readonly int $length
;
/* 方法 */
public int <span class="methodname">count ( void )
DOMNode <span
class="methodname">getNamedItem ( <span
class="type">string $name )
DOMNode <span
class="methodname">getNamedItemNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
DOMNode item (
int $index
)
}
属性
length
The number of nodes in the map. The range of valid child node indices is
0 to length - 1 inclusive.
DOMNamedNodeMap::count
Get number of nodes in the map
说明
public int <span class="methodname">DOMNamedNodeMap::count ( <span class="methodparam">void )
Gets the number of nodes in the map.
参数
此函数没有参数。
返回值
Returns the number of nodes in the map, which is identical to the length property.
DOMNamedNodeMap::getNamedItem
Retrieves a node specified by name
说明
DOMNode <span
class="methodname">DOMNamedNodeMap::getNamedItem ( <span
class="methodparam">string $name )
Retrieves a node specified by its nodeName.
参数
name
The nodeName of the node to retrieve.
返回值
A node (of any type) with the specified nodeName, or null if no
node is found.
参见
- DOMNamedNodeMap::getNamedItemNS
DOMNamedNodeMap::getNamedItemNS
Retrieves a node specified by local name and namespace URI
说明
DOMNode <span
class="methodname">DOMNamedNodeMap::getNamedItemNS ( <span
class="methodparam">string
$namespaceURI , <span
class="type">string $localName )
Retrieves a node specified by localName and namespaceURI.
参数
namespaceURI
The namespace URI of the node to retrieve.
localName
The local name of the node to retrieve.
返回值
A node (of any type) with the specified local name and namespace URI, or
null if no node is found.
参见
- DOMNamedNodeMap::getNamedItem
DOMNamedNodeMap::item
Retrieves a node specified by index
说明
DOMNode <span
class="methodname">DOMNamedNodeMap::item ( <span
class="methodparam">int $index )
Retrieves a node specified by index within the <span
class="classname">DOMNamedNodeMap object.
参数
index
Index into this map.
返回值
The node at the indexth position in the map, or null if that is
not a valid index (greater than or equal to the number of nodes in this
map).
类摘要
DOMNode
class DOMNode {
/* 属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public DOMNode
appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">C14N ([ <span
class="type">bool $exclusive [, <span
class="methodparam">bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">C14NFile ( <span
class="type">string $uri [, <span
class="methodparam">bool $exclusive<span
class="initializer"> = false [, <span
class="methodparam">bool $with_comments<span
class="initializer"> = false [, <span
class="methodparam">array $xpath [,
array
$ns_prefixes ]]]] )
public DOMNode
cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">getLineNo ( <span class="methodparam">void )
public string getNodePath ( <span class="methodparam">void )
public bool hasAttributes ( <span class="methodparam">void )
public bool hasChildNodes ( <span class="methodparam">void )
public DOMNode
insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void normalize ( <span class="methodparam">void )
public DOMNode
removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
nodeName
Returns the most accurate name for the current node type
nodeValue
The value of this node, depending on its type. Contrary to the W3C
specification, the node value of <span
class="classname">DOMElement nodes is equal to
DOMNode::textContent
instead of null.
nodeType
Gets the type of the node. One of the predefined
XML_xxx_NODE constants
parentNode
The parent of this node. If there is no such node, this returns
null.
childNodes
A DOMNodeList that contains all children
of this node. If there are no children, this is an empty <span
class="classname">DOMNodeList.
firstChild
The first child of this node. If there is no such node, this returns
null.
lastChild
The last child of this node. If there is no such node, this returns
null.
previousSibling
The node immediately preceding this node. If there is no such node, this
returns null.
nextSibling
The node immediately following this node. If there is no such node, this
returns null.
attributes
A DOMNamedNodeMap containing the
attributes of this node (if it is a <span
class="classname">DOMElement) or null otherwise.
ownerDocument
The DOMDocument object associated with
this node, or null if this node is a <span
class="classname">DOMDOcument
namespaceURI
The namespace URI of this node, or null if it is unspecified.
prefix
The namespace prefix of this node, or null if it is unspecified.
localName
Returns the local part of the qualified name of this node.
baseURI
The absolute base URI of this node or null if the implementation
wasn't able to obtain an absolute URI.
textContent
The text content of this node and its descendants.
注释
Note:
此 DOM 扩展采用 UTF-8 编码。在 ISO-8859-1 编码下,使用 <span class="function">utf8_encode 和 <span class="function">utf8_decode 来处理,其它编码下使用 iconv 函数处理。
更新日志
| 版本 | 说明 |
|---|---|
| 5.6.1 | The textContent property has been made writable (formerly it has been readonly). |
参见
DOMNode::appendChild
Adds new child at the end of the children
说明
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
This function appends a child to an existing list of children or creates a new list of children. The child can be created with e.g. <span class="methodname">DOMDocument::createElement, <span class="methodname">DOMDocument::createTextNode etc. or simply by using any other node.
When using an existing node it will be moved.
参数
newnode
The appended child.
返回值
The node added.
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly or if the previous parent of the node
being inserted is readonly.
DOM_HIERARCHY_REQUEST_ERR
Raised if this node is of a type that does not allow children of the
type of the newnode node, or if the node to append is one of this
node's ancestors or this node itself.
DOM_WRONG_DOCUMENT_ERR
Raised if newnode was created from a different document than the one
that created this node.
范例
The following example will add a new element node to a fresh document.
示例 #1 Adding a child
<?php
$doc = new DOMDocument;
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
echo $doc->saveXML();
?>
示例 #2 Nested children
<?php
$doc = new DOMDocument;
$headNode = $doc->createElement("head");
$doc->appendChild($headNode);
$titleNode = $doc->createElement("title");
$headNode->appendChild($titleNode);
echo $doc->saveXML();
?>
参见
- DOMNode::insertBefore
- DOMNode::removeChild
- DOMNode::replaceChild
DOMNode::C14N
Canonicalize nodes to a string
说明
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
Canonicalize nodes to a string
参数
exclusive
Enable exclusive parsing of only the nodes matched by the provided xpath
or namespace prefixes.
with_comments
Retain comments in output.
xpath
An array of xpaths to filter the nodes by.
ns_prefixes
An array of namespace prefixes to filter the nodes by.
返回值
Returns canonicalized nodes as a string 或者在失败时返回 false
参见
- DOMNode::C14NFile
DOMNode::C14NFile
Canonicalize nodes to a file
说明
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
Canonicalize nodes to a file.
参数
uri
Path to write the output to.
exclusive
Enable exclusive parsing of only the nodes matched by the provided xpath
or namespace prefixes.
with_comments
Retain comments in output.
xpath
An array of xpaths to filter the nodes by.
ns_prefixes
An array of namespace prefixes to filter the nodes by.
返回值
Number of bytes written 或者在失败时返回 false
参见
- DOMNode::C14N
DOMNode::cloneNode
Clones a node
说明
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
Creates a copy of the node.
参数
deep
Indicates whether to copy all descendant nodes. This parameter is
defaulted to false.
返回值
The cloned node.
DOMNode::getLineNo
Get line number for a node
说明
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
Gets line number for where the node is defined.
参数
此函数没有参数。
返回值
Always returns the line number where the node was defined in.
范例
示例 #1 DOMNode::getLineNo example
<?php
// XML dump for below example
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<node />
</root>
XML;
// Create a new DOMDocument instance
$dom = new DOMDocument;
// Load the XML
$dom->loadXML($xml);
// Print where the line where the 'node' element was defined in
printf('The <node> tag is defined on line %d', $dom->getElementsByTagName('node')->item(0)->getLineNo());
?>
以上例程会输出:
The <node> tag is defined in line 3
DOMNode::getNodePath
Get an XPath for a node
说明
public string DOMNode::getNodePath ( <span class="methodparam">void )
Gets an XPath location path for the node.
参数
此函数没有参数。
返回值
Returns a string containing the XPath, or
null in case of an error.
范例
示例 #1 DOMNode::getNodePath example
<?php
// Create a new DOMDocument instance
$dom = new DOMDocument;
// Load the XML
$dom->loadXML('
<fruits>
<apples>
<apple>braeburn</apple>
<apple>granny smith</apple>
</apples>
<pears>
<pear>conference</pear>
</pears>
</fruits>
');
// Print XPath for each element
foreach ($dom->getElementsByTagName('*') as $node) {
echo $node->getNodePath() . "\n";
}
?>
以上例程会输出:
/fruits
/fruits/apples
/fruits/apples/apple[1]
/fruits/apples/apple[2]
/fruits/pears
/fruits/pears/pear
参见
- DOMXPath
DOMNode::hasAttributes
Checks if node has attributes
说明
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
This method checks if the node has attributes. The tested node has to be
an XML_ELEMENT_NODE.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- DOMNode::hasChildNodes
DOMNode::hasChildNodes
Checks if node has children
说明
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
This function checks if the node has children.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- DOMNode::hasAttributes
DOMNode::insertBefore
Adds a new child before a reference node
说明
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
This function inserts a new node right before the reference node. If you plan to do further modifications on the appended child you must use the returned node.
When using an existing node it will be moved.
参数
newnode
The new node.
refnode
The reference node. If not supplied, newnode is appended to the
children.
返回值
The inserted node.
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly or if the previous parent of the node
being inserted is readonly.
DOM_HIERARCHY_REQUEST_ERR
Raised if this node is of a type that does not allow children of the
type of the newnode node, or if the node to append is one of this
node's ancestors or this node itself.
DOM_WRONG_DOCUMENT_ERR
Raised if newnode was created from a different document than the one
that created this node.
DOM_NOT_FOUND
Raised if refnode is not a child of this node.
参见
- DOMNode::appendChild
DOMNode::isDefaultNamespace
Checks if the specified namespaceURI is the default namespace or not
说明
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
Tells whether namespaceURI is the default namespace.
参数
namespaceURI
The namespace URI to look for.
返回值
Return true if namespaceURI is the default namespace,
false otherwise.
DOMNode::isSameNode
Indicates if two nodes are the same node
说明
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
This function indicates if two nodes are the same node. The comparison is not based on content
参数
node
The compared node.
返回值
成功时返回 true, 或者在失败时返回 false。
DOMNode::isSupported
Checks if feature is supported for specified version
说明
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
Checks if the asked feature is supported for the specified version.
参数
feature
The feature to test. See the example of <span
class="methodname">DOMImplementation::hasFeature for a list of
features.
version
The version number of the feature to test.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- DOMImplementation::hasFeature
DOMNode::lookupNamespaceUri
Gets the namespace URI of the node based on the prefix
说明
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
Gets the namespace URI of the node based on the prefix.
参数
prefix
The prefix of the namespace.
返回值
The namespace URI of the node.
参见
- DOMNode::lookupPrefix
DOMNode::lookupPrefix
Gets the namespace prefix of the node based on the namespace URI
说明
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
Gets the namespace prefix of the node based on the namespace URI.
参数
namespaceURI
The namespace URI.
返回值
The prefix of the namespace.
参见
- DOMNode::lookupNamespaceUri
DOMNode::normalize
Normalizes the node
说明
public void DOMNode::normalize ( <span class="methodparam">void )
Remove empty text nodes and merge adjacent text nodes in this node and all its children.
返回值
没有返回值。
参见
- » The DOM Specification
- DOMDocument::normalizeDocument
DOMNode::removeChild
Removes child from list of children
说明
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
This functions removes a child from a list of children.
参数
oldnode
The removed child.
返回值
If the child could be removed the function returns the old child.
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
DOM_NOT_FOUND
Raised if oldnode is not a child of this node.
范例
The following example will delete the chapter element of our XML document.
示例 #1 Removing a child
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$book = $doc->documentElement;
// we retrieve the chapter and remove it from the book
$chapter = $book->getElementsByTagName('chapter')->item(0);
$oldchapter = $book->removeChild($chapter);
echo $doc->saveXML();
?>
以上例程会输出:
<?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>
</book>
参见
- DOMNode::appendChild
- DOMNode::replaceChild
DOMNode::replaceChild
Replaces a child
说明
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
This function replaces the child oldnode with the passed new node. If
the newnode is already a child it will not be added a second time. If
the replacement succeeds the old node is returned.
参数
newnode
The new node. It must be a member of the target document, i.e. created
by one of the DOMDocument->createXXX() methods or imported in the
document by
.
oldnode
The old node.
返回值
The old node or false if an error occur.
错误/异常
DOM_NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly or if the previous parent of the node
being inserted is readonly.
DOM_HIERARCHY_REQUEST_ERR
Raised if this node is of a type that does not allow children of the
type of the newnode node, or if the node to put in is one of this
node's ancestors or this node itself.
DOM_WRONG_DOCUMENT_ERR
Raised if newnode was created from a different document than the one
that created this node.
DOM_NOT_FOUND
Raised if oldnode is not a child of this node.
参见
- DOMNode::appendChild
- DOMNode::removeChild
类摘要
DOMNodeList
class DOMNodeList <span class="oointerface">implements <span class="interfacename">Traversable <span class="oointerface">, Countable {
/* 属性 */
public <span
class="modifier">readonly int $length
;
/* 方法 */
public int <span class="methodname">count ( void )
DOMNode item (
int $index
)
}
属性
length
The number of nodes in the list. The range of valid child node indices
is 0 to length - 1 inclusive.
更新日志
| 版本 | 说明 |
|---|---|
| 7.2.0 | The Countable interface is implemented and returns the value of the length property. |
参见
DOMNodeList::count
Get number of nodes in the list
说明
public int <span class="methodname">DOMNodeList::count ( <span class="methodparam">void )
Gets the number of nodes in the list.
参数
此函数没有参数。
返回值
Returns the number of nodes in the list, which is identical to the length property.
DOMNodeList::item
Retrieves a node specified by index
说明
DOMNode <span
class="methodname">DOMNodeList::item ( <span
class="methodparam">int $index )
Retrieves a node specified by index within the <span
class="classname">DOMNodeList object.
小贴士
If you need to know the number of nodes in the collection, use the length property of the DOMNodeList object.
参数
index
Index of the node into the collection.
返回值
The node at the indexth position in the <span
class="classname">DOMNodeList, or null if that is not a
valid index.
范例
示例 #1 Traversing all the entries of the table
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$items = $doc->getElementsByTagName('entry');
for ($i = 0; $i < $items->length; $i++) {
echo $items->item($i)->nodeValue . "\n";
}
?>
Alternatively, you can use foreach, which is a much more convenient way:
<?php
foreach ($items as $item) {
echo $item->nodeValue . "\n";
}
?>
以上例程会输出:
Title
Author
Language
ISBN
The Grapes of Wrath
John Steinbeck
en
0140186409
The Pearl
John Steinbeck
en
014017737X
Samarcande
Amine Maalouf
fr
2253051209
类摘要
DOMNotation
class DOMNotation <span class="ooclass"> extends DOMNode {
/* 属性 */
public <span
class="modifier">readonly string
$publicId ;
public <span
class="modifier">readonly string
$systemId ;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
publicId
systemId
类摘要
DOMProcessingInstruction
class DOMProcessingInstruction <span class="ooclass"> extends DOMNode {
/* 属性 */
public <span
class="modifier">readonly string
$target ;
public string
$data ;
/* 继承的属性 */
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">string $name [,
string
$value ] )
/* 继承的方法 */
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
target
data
DOMProcessingInstruction::__construct
Creates a new DOMProcessingInstruction object
说明
public <span
class="methodname">DOMProcessingInstruction::__construct (
string
$name [, <span
class="type">string $value ] )
Creates a new DOMProcessingInstruction object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use .
参数
name
The tag name of the processing instruction.
value
The value of the processing instruction.
范例
示例 #1 Creating a new <span class="classname">DOMProcessingInstruction object
<?php
$dom = new DOMDocument('1.0', 'UTF-8');
$html = $dom->appendChild(new DOMElement('html'));
$body = $html->appendChild(new DOMElement('body'));
$pinode = new DOMProcessingInstruction('php', 'echo "Hello World"; ');
$body->appendChild($pinode);
echo $dom->saveXML();
?>
以上例程会输出:
<?xml version="1.0" encoding="UTF-8"?>
<html><body><?php echo "Hello World"; ?></body></html>
参见
- <span class="methodname">DOMDocument::createProcessingInstruction
简介
The DOMText class inherits from <span class="classname">DOMCharacterData and represents the textual content of a DOMElement or <span class="classname">DOMAttr.
类摘要
DOMText
class DOMText extends DOMCharacterData {
/* 属性 */
public <span
class="modifier">readonly string
$wholeText ;
/* 继承的属性 */
public string
$data ;
public <span
class="modifier">readonly int $length
;
public <span
class="modifier">readonly string
$nodeName ;
public string
$nodeValue ;
public <span
class="modifier">readonly int
$nodeType ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$parentNode ;
public <span
class="modifier">readonly DOMNodeList
$childNodes ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$firstChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$lastChild ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$previousSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNodenull
$nextSibling ;
public <span
class="modifier">readonly <span
class="type">DOMNamedNodeMapnull
$attributes ;
public <span
class="modifier">readonly <span
class="type">DOMDocumentnull
$ownerDocument ;
public <span
class="modifier">readonly <span
class="type">stringnull
$namespaceURI ;
public string
$prefix ;
public <span
class="modifier">readonly string
$localName ;
public <span
class="modifier">readonly <span
class="type">stringnull
$baseURI ;
public string
$textContent ;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">string $value ]
)
public bool isElementContentWhitespace ( <span class="methodparam">void )
public bool isWhitespaceInElementContent ( <span class="methodparam">void )
public DOMText
splitText ( <span
class="methodparam">int $offset )
/* 继承的方法 */
public void
DOMCharacterData::appendData ( <span
class="methodparam">string $data )
public void
DOMCharacterData::deleteData ( <span
class="methodparam">int $offset ,
int $count
)
public void
DOMCharacterData::insertData ( <span
class="methodparam">int $offset ,
string
$data )
public void
DOMCharacterData::replaceData ( <span
class="methodparam">int $offset ,
int $count
, string
$data )
public string
DOMCharacterData::substringData ( <span
class="methodparam">int $offset ,
int $count
)
public DOMNode
DOMNode::appendChild ( <span
class="methodparam">DOMNode $newnode
)
public <span
class="type">stringfalse <span
class="methodname">DOMNode::C14N ([ <span
class="methodparam">bool $exclusive
[, bool
$with_comments [, <span
class="type">array $xpath [, <span
class="methodparam">array
$ns_prefixes ]]]] )
public <span
class="type">intfalse <span
class="methodname">DOMNode::C14NFile ( <span
class="methodparam">string $uri [,
bool
$exclusive = false [,
bool
$with_comments = false
[, array
$xpath [, <span
class="type">array $ns_prefixes ]]]] )
public DOMNode
DOMNode::cloneNode ([ <span
class="methodparam">bool $deep ] )
public int <span class="methodname">DOMNode::getLineNo ( <span class="methodparam">void )
public string DOMNode::getNodePath ( <span class="methodparam">void )
public bool DOMNode::hasAttributes ( <span class="methodparam">void )
public bool DOMNode::hasChildNodes ( <span class="methodparam">void )
public DOMNode
DOMNode::insertBefore ( <span
class="methodparam">DOMNode $newnode
[, DOMNode
$refnode ] )
public bool
DOMNode::isDefaultNamespace ( <span
class="methodparam">string
$namespaceURI )
public bool
DOMNode::isSameNode ( <span
class="methodparam">DOMNode $node )
public bool
DOMNode::isSupported ( <span
class="methodparam">string $feature ,
string
$version )
public string
DOMNode::lookupNamespaceUri ( <span
class="methodparam">string $prefix )
public string
DOMNode::lookupPrefix ( <span
class="methodparam">string
$namespaceURI )
public void DOMNode::normalize ( <span class="methodparam">void )
public DOMNode
DOMNode::removeChild ( <span
class="methodparam">DOMNode $oldnode
)
public DOMNode
DOMNode::replaceChild ( <span
class="methodparam">DOMNode $newnode
, DOMNode
$oldnode )
}
属性
wholeText
Holds all the text of logically-adjacent (not separated by Element,
Comment or Processing Instruction) Text nodes.
DOMText::__construct
Creates a new DOMText object
说明
public <span
class="methodname">DOMText::__construct ([ <span
class="methodparam">string $value ]
)
Creates a new DOMText object.
参数
value
The value of the text node. If not supplied an empty text node is
created.
范例
示例 #1 Creating a new DOMText
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$text = $element->appendChild(new DOMText('root value'));
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?><root>root value</root> */
?>
参见
- DOMDocument::createTextNode
DOMText::isElementContentWhitespace
Returns whether this text node contains whitespace in element content
说明
public bool DOMText::isElementContentWhitespace ( void )
参数
此函数没有参数。
返回值
成功时返回 true, 或者在失败时返回 false。
DOMText::isWhitespaceInElementContent
Indicates whether this text node contains whitespace
说明
public bool DOMText::isWhitespaceInElementContent ( void )
Indicates whether this text node contains only whitespace or it is empty. The text node is determined to contain whitespace in element content during the load of the document.
返回值
Returns true if node contains zero or more whitespace characters
and nothing else. Returns false otherwise.
DOMText::splitText
Breaks this node into two nodes at the specified offset
说明
public DOMText
DOMText::splitText ( <span
class="methodparam">int $offset )
Breaks this node into two nodes at the specified offset, keeping both
in the tree as siblings.
After being split, this node will contain all the content up to the
offset. If the original node had a parent node, the new node is
inserted as the next sibling of the original node. When the offset is
equal to the length of this node, the new node has no data.
参数
offset
The offset at which to split, starting from 0.
返回值
The new node of the same type, which contains all the content at and
after the offset.
简介
Supports XPath 1.0
类摘要
DOMXPath
class DOMXPath {
/* 属性 */
public <span
class="type">DOMDocument $document ;
/* 方法 */
public <span
class="methodname">__construct ( <span
class="methodparam">DOMDocument $doc
)
public mixed
evaluate ( <span
class="methodparam">string
$expression [, <span
class="type">DOMNode $contextnode [, <span
class="methodparam">bool
$registerNodeNS = true
]] )
public <span
class="type">DOMNodeList query (
string
$expression [, <span
class="type">DOMNode $contextnode [, <span
class="methodparam">bool
$registerNodeNS = true
]] )
public bool
registerNamespace ( <span
class="methodparam">string $prefix ,
string
$namespaceURI )
public void
registerPhpFunctions ([ <span
class="methodparam">mixed $restrict
] )
}
属性
document
DOMXPath::__construct
Creates a new DOMXPath object
说明
public <span
class="methodname">DOMXPath::__construct ( <span
class="methodparam">DOMDocument $doc
)
Creates a new DOMXPath object.
参数
doc
The DOMDocument associated with the <span
class="classname">DOMXPath.
DOMXPath::evaluate
Evaluates the given XPath expression and returns a typed result if possible
说明
public mixed
DOMXPath::evaluate ( <span
class="methodparam">string
$expression [, <span
class="type">DOMNode $contextnode [, <span
class="methodparam">bool
$registerNodeNS = true
]] )
Executes the given XPath expression and returns a typed result if
possible.
参数
expression
The XPath expression to execute.
contextnode
The optional contextnode can be specified for doing relative XPath
queries. By default, the queries are relative to the root element.
registerNodeNS
The optional registerNodeNS can be specified to disable automatic
registration of the context node.
返回值
Returns a typed result if possible or a <span
class="classname">DOMNodeList containing all nodes matching the
given XPath expression.
If the expression is malformed or the contextnode is invalid, <span
class="methodname">DOMXPath::evaluate returns false.
范例
示例 #1 Getting the count of all the english books
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
$tbody = $doc->getElementsByTagName('tbody')->item(0);
// our query is relative to the tbody node
$query = 'count(row/entry[. = "en"])';
$entries = $xpath->evaluate($query, $tbody);
echo "There are $entries english books\n";
?>
以上例程会输出:
There are 2 english books
参见
- DOMXPath::query
DOMXPath::query
Evaluates the given XPath expression
说明
public <span
class="type">DOMNodeList <span
class="methodname">DOMXPath::query ( <span
class="methodparam">string
$expression [, <span
class="type">DOMNode $contextnode [, <span
class="methodparam">bool
$registerNodeNS = true
]] )
Executes the given XPath expression.
参数
expression
The XPath expression to execute.
contextnode
The optional contextnode can be specified for doing relative XPath
queries. By default, the queries are relative to the root element.
registerNodeNS
The optional registerNodeNS can be specified to disable automatic
registration of the context node.
返回值
Returns a DOMNodeList containing all
nodes matching the given XPath expression. Any expression which does
not return nodes will return an empty <span
class="classname">DOMNodeList.
If the expression is malformed or the contextnode is invalid, <span
class="methodname">DOMXPath::query returns false.
范例
示例 #1 Getting all the english books
<?php
$doc = new DOMDocument;
// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
// We start from the root element
$query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';
$entries = $xpath->query($query);
foreach ($entries as $entry) {
echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
" by {$entry->previousSibling->nodeValue}\n";
}
?>
以上例程会输出:
Found The Grapes of Wrath, by John Steinbeck
Found The Pearl, by John Steinbeck
We can also use the contextnode parameter to shorten our expression:
<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
$tbody = $doc->getElementsByTagName('tbody')->item(0);
// our query is relative to the tbody node
$query = 'row/entry[. = "en"]';
$entries = $xpath->query($query, $tbody);
foreach ($entries as $entry) {
echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
" by {$entry->previousSibling->nodeValue}\n";
}
?>
参见
- DOMXPath::evaluate
DOMXPath::registerNamespace
Registers the namespace with the DOMXPath object
说明
public bool
DOMXPath::registerNamespace ( <span
class="methodparam">string $prefix ,
string
$namespaceURI )
Registers the namespaceURI and prefix with the DOMXPath object.
参数
prefix
The prefix.
namespaceURI
The URI of the namespace.
返回值
成功时返回 true, 或者在失败时返回 false。
DOMXPath::registerPhpFunctions
Register PHP functions as XPath functions
说明
public void
DOMXPath::registerPhpFunctions ([ <span
class="methodparam">mixed $restrict
] )
This method enables the ability to use PHP functions within XPath expressions.
参数
restrict
Use this parameter to only allow certain functions to be called from
XPath.
This parameter can be either a string (a function name) or an array of function names.
返回值
没有返回值。
范例
The following examples use book.xml which contains the following:
示例 #1 book.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>PHP Basics</title>
<author>Jim Smith</author>
<author>Jane Smith</author>
</book>
<book>
<title>PHP Secrets</title>
<author>Jenny Smythe</author>
</book>
<book>
<title>XML basics</title>
<author>Joe Black</author>
</book>
</books>
示例 #2 <span class="methodname">DOMXPath::registerPHPFunctions with php:functionString
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
// Register the php: namespace (required)
$xpath->registerNamespace("php", "http://php.net/xpath");
// Register PHP functions (no restrictions)
$xpath->registerPHPFunctions();
// Call substr function on the book title
$nodes = $xpath->query('//book[php:functionString("substr", title, 0, 3) = "PHP"]');
echo "Found {$nodes->length} books starting with 'PHP':\n";
foreach ($nodes as $node) {
$title = $node->getElementsByTagName("title")->item(0)->nodeValue;
$author = $node->getElementsByTagName("author")->item(0)->nodeValue;
echo "$title by $author\n";
}
?>
以上例程的输出类似于:
Found 2 books starting with 'PHP':
PHP Basics by Jim Smith
PHP Secrets by Jenny Smythe
示例 #3 <span class="methodname">DOMXPath::registerPHPFunctions with php:function
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
// Register the php: namespace (required)
$xpath->registerNamespace("php", "http://php.net/xpath");
// Register PHP functions (has_multiple only)
$xpath->registerPHPFunctions("has_multiple");
function has_multiple($nodes) {
// Return true if more than one author
return count($nodes) > 1;
}
// Filter books with multiple authors
$books = $xpath->query('//book[php:function("has_multiple", author)]');
echo "Books with multiple authors:\n";
foreach ($books as $book) {
echo $book->getElementsByTagName("title")->item(0)->nodeValue . "\n";
}
?>
以上例程的输出类似于:
Books with multiple authors:
PHP Basics
参见
- DOMXPath::registerNamespace
- DOMXPath::query
- DOMXPath::evaluate