Document Object Model

目录

简介

DOMAttr represents an attribute in the DOMElement object.

类摘要

DOMAttr

class DOMAttr extends DOMNode {

/* 属性 */

public readonly string $name ;

public readonly DOMElement $ownerElement ;

public readonly bool $schemaTypeInfo ;

public readonly bool $specified ;

public string $value ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public __construct ( string $name [, string $value ] )

public bool isId ( void )

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

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 DOMAttr::__construct ( 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 ( 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 DOMText for textural representation of CData constructs.

类摘要

DOMCdataSection

class DOMCdataSection extends DOMText {

/* 继承的属性 */

public readonly string $wholeText ;

public string $data ;

public readonly int $length ;

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* Methods */

public __construct ( string $value )

/* Inherited methods */

public bool DOMText::isElementContentWhitespace ( void )

public bool DOMText::isWhitespaceInElementContent ( void )

public DOMText DOMText::splitText ( int $offset )

public void DOMCharacterData::appendData ( string $data )

public void DOMCharacterData::deleteData ( int $offset , int $count )

public void DOMCharacterData::insertData ( int $offset , string $data )

public void DOMCharacterData::replaceData ( int $offset , int $count , string $data )

public string DOMCharacterData::substringData ( int $offset , int $count )

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

DOMCdataSection::__construct

Constructs a new DOMCdataSection object

说明

public DOMCdataSection::__construct ( string $value )

Constructs a new CDATA node. This works like the 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 extends DOMNode {

/* 属性 */

public string $data ;

public readonly int $length ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public void appendData ( string $data )

public void deleteData ( int $offset , int $count )

public void insertData ( int $offset , string $data )

public void replaceData ( int $offset , int $count , string $data )

public string substringData ( int $offset , int $count )

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

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 ( 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 ( 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 ( 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 ( 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 ( 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 extends DOMCharacterData {

/* 继承的属性 */

public string $data ;

public readonly int $length ;

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public __construct ([ string $value ] )

/* 继承的方法 */

public void DOMCharacterData::appendData ( string $data )

public void DOMCharacterData::deleteData ( int $offset , int $count )

public void DOMCharacterData::insertData ( int $offset , string $data )

public void DOMCharacterData::replaceData ( int $offset , int $count , string $data )

public string DOMCharacterData::substringData ( int $offset , int $count )

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

参见

DOMComment::__construct

Creates a new DOMComment object

说明

public DOMComment::__construct ([ 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 extends DOMNode {

/* 属性 */

public readonly string $actualEncoding ;

public readonly DOMConfiguration $config ;

public readonly DOMDocumentType $doctype ;

public readonly DOMElement $documentElement ;

public string $documentURI ;

public string $encoding ;

public bool $formatOutput ;

public readonly 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 readonly string $xmlEncoding ;

public bool $xmlStandalone ;

public string $xmlVersion ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public __construct ([ string $version [, string $encoding ]] )

public DOMAttr createAttribute ( string $name )

public DOMAttr createAttributeNS ( string $namespaceURI , string $qualifiedName )

public DOMCDATASection createCDATASection ( string $data )

public DOMComment createComment ( string $data )

public DOMDocumentFragment createDocumentFragment ( void )

public DOMElement createElement ( string $name [, string $value ] )

public DOMElement createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] )

public DOMEntityReference createEntityReference ( string $name )

public DOMProcessingInstruction createProcessingInstruction ( string $target [, string $data ] )

public DOMText createTextNode ( string $content )

public DOMElement getElementById ( string $elementId )

public DOMNodeList getElementsByTagName ( string $name )

public DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )

public DOMNode importNode ( DOMNode $importedNode [, bool $deep = FALSE ] )

public mixed load ( string $filename [, int $options = 0 ] )

public bool loadHTML ( string $source [, int $options = 0 ] )

public bool loadHTMLFile ( string $filename [, int $options = 0 ] )

public mixed loadXML ( string $source [, int $options = 0 ] )

public void normalizeDocument ( void )

public bool registerNodeClass ( string $baseclass , string $extendedclass )

public bool relaxNGValidate ( string $filename )

public bool relaxNGValidateSource ( string $source )

public int save ( string $filename [, int $options = 0 ] )

public string saveHTML ([ DOMNode $node = NULL ] )

public int saveHTMLFile ( string $filename )

public string saveXML ([ DOMNode $node [, int $options = 0 ]] )

public bool schemaValidate ( string $filename [, int $flags = 0 ] )

public bool schemaValidateSource ( string $source [, int $flags ] )

public bool validate ( void )

public int xinclude ([ int $options = 0 ] )

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

actualEncoding
Deprecated. Actual encoding of the document, is a readonly equivalent to encoding.

config
Deprecated. Configuration used when DOMDocument::normalizeDocument is invoked.

doctype
The Document Type Declaration associated with this document.

documentElement
This is a convenience attribute that allows direct access to the child node that is the document element of the document.

documentURI
The location of the document or NULL if undefined.

encoding
Encoding of the document, as specified by the XML declaration. This attribute is not present in the final DOM Level 3 specification, but is the only way of manipulating XML document encoding in this implementation.

formatOutput
Nicely formats output with indentation and extra space.

implementation
The DOMImplementation object that handles this document.

preserveWhiteSpace
Do not remove redundant white space. Default to TRUE.

recover
Proprietary. Enables recovery mode, i.e. trying to parse non-well formed documents. This attribute is not part of the DOM specification and is specific to libxml.

resolveExternals
Set it to TRUE to load external entities from a doctype declaration. This is useful for including character entities in your XML document.

standalone
Deprecated. Whether or not the document is standalone, as specified by the XML declaration, corresponds to xmlStandalone.

strictErrorChecking
Throws DOMException on errors. Default to TRUE.

substituteEntities
Proprietary. Whether or not to substitute entities. This attribute is not part of the DOM specification and is specific to libxml.

validateOnParse
Loads and validates against the DTD. Default to FALSE.

version
Deprecated. Version of XML, corresponds to xmlVersion.

xmlEncoding
An attribute specifying, as part of the XML declaration, the encoding of this document. This is NULL when unspecified or when it is not known, such as when the Document was created in memory.

xmlStandalone
An attribute specifying, as part of the XML declaration, whether this document is standalone. This is FALSE when unspecified.

xmlVersion
An attribute specifying, as part of the XML declaration, the version number of this document. If there is no declaration and if this document supports the "XML" feature, the value is "1.0".

注释

Note:

The DOM extension uses UTF-8 encoding. Use class="function">utf8_encode and class="function">utf8_decode to work with texts in ISO-8859-1 encoding or iconv for other encodings.

Note:

When using json_encode on a class="classname">DOMDocument object the result will be that of encoding an empty object.

参见

DOMDocument::__construct

Creates a new DOMDocument object

说明

public DOMDocument::__construct ([ 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 ( string $name )

This function creates a new instance of class DOMAttr. 此节点出现在文档中,除非是用诸如 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createAttributeNS

Create new attribute node with an associated namespace

说明

public DOMAttr DOMDocument::createAttributeNS ( string $namespaceURI , string $qualifiedName )

This function creates a new instance of class DOMAttr. 此节点出现在文档中,除非是用诸如 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createCDATASection

Create new cdata node

说明

public DOMCDATASection DOMDocument::createCDATASection ( string $data )

This function creates a new instance of class DOMCDATASection. 此节点出现在文档中,除非是用诸如 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createComment

Create new comment node

说明

public DOMComment DOMDocument::createComment ( string $data )

This function creates a new instance of class 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createDocumentFragment

Create new document fragment

说明

public DOMDocumentFragment DOMDocument::createDocumentFragment ( void )

This function creates a new instance of class DOMDocumentFragment. 此节点出现在文档中,除非是用诸如 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createElement

Create new element node

说明

public DOMElement DOMDocument::createElement ( string $name [, string $value ] )

This function creates a new instance of class 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 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 value will not be escaped. Use 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createElementNS

Create new element node with an associated namespace

说明

public DOMElement DOMDocument::createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] )

This function creates a new element node with an associated namespace. 此节点出现在文档中,除非是用诸如 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createEntityReference

Create new entity reference node

说明

public DOMEntityReference DOMDocument::createEntityReference ( string $name )

This function creates a new instance of class DOMEntityReference. 此节点出现在文档中,除非是用诸如 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
  • DOMDocument::createProcessingInstruction
  • DOMDocument::createTextNode

DOMDocument::createProcessingInstruction

Creates new PI node

说明

public DOMProcessingInstruction DOMDocument::createProcessingInstruction ( string $target [, string $data ] )

This function creates a new instance of class DOMProcessingInstruction. 此节点出现在文档中,除非是用诸如 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 ( string $content )

This function creates a new instance of class DOMText. 此节点出现在文档中,除非是用诸如 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
  • DOMDocument::createProcessingInstruction

DOMDocument::getElementById

Searches for an element with a certain id

说明

public DOMElement DOMDocument::getElementById ( 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="jim.smith@basics.php">Jim Smith</author>
    <author email="jane.smith@basics.php">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="jon.doe@advanced.php">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 DOMNodeList DOMDocument::getElementsByTagName ( string $name )

This function returns a new instance of class 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 DOMNodeList DOMDocument::getElementsByTagNameNS ( string $namespaceURI , 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 ( DOMNode $importedNode [, 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 deep needs to be set to TRUE

返回值

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 ( 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 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 ( 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 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();
?>

更新日志

版本说明
5.4.0Added options parameter.

参见

  • DOMDocument::loadHTMLFile
  • DOMDocument::saveHTML
  • DOMDocument::saveHTMLFile

DOMDocument::loadHTMLFile

Load HTML from a file

说明

public bool DOMDocument::loadHTMLFile ( 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();
?>

更新日志

版本说明
5.4.0Added options parameter.

参见

  • DOMDocument::loadHTML
  • DOMDocument::saveHTML
  • DOMDocument::saveHTMLFile

DOMDocument::loadXML

Load XML from a string

说明

public mixed DOMDocument::loadXML ( 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 ( void )

This method acts as if you saved and then loaded the document, putting the document in a "normal" form.

返回值

没有返回值。

参见

DOMDocument::registerNodeClass

Register extended class used to create base node type

说明

public bool DOMDocument::registerNodeClass ( 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

更新日志

版本说明
5.2.2Prior to 5.2.2, a previously registered extendedclass had to be unregistered before being able to register a new class extending the same baseclass.

范例

示例 #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 DOMDocument::registerNodeClass with 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"

DOMDocument::relaxNGValidate

Performs relaxNG validation on the document

说明

public bool DOMDocument::relaxNGValidate ( 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 DOMDocument::save ( 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.

更新日志

版本说明
5.1.0Added the options parameter

范例

示例 #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 ([ DOMNode $node = 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.

更新日志

版本说明
5.3.6The node parameter was added.

范例

示例 #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 DOMDocument::saveHTMLFile ( 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 ([ DOMNode $node [, 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.

参数

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.

更新日志

版本说明
5.1.0Added the options parameter

范例

示例 #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

说明

public bool DOMDocument::schemaValidate ( 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.2Added 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 [, 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.2Added the flags parameter

参见

  • DOMDocument::schemaValidate
  • DOMDocument::relaxNGValidate
  • DOMDocument::relaxNGValidateSource
  • DOMDocument::validate

DOMDocument::validate

Validates the document based on its DTD

说明

public bool DOMDocument::validate ( void )

Validates the document based on its DTD.

You can also use the validateOnParse property of 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 DOMDocument::xinclude ([ int $options = 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 extends DOMNode {

/* 属性 */

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public bool appendXML ( string $data )

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

DOMDocumentFragment::appendXML

Append raw XML data

说明

public bool DOMDocumentFragment::appendXML ( 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 DOMDocumentType object.

类摘要

DOMDocumentType

class DOMDocumentType extends DOMNode {

/* 属性 */

public readonly string $publicId ;

public readonly string $systemId ;

public readonly string $name ;

public readonly DOMNamedNodeMap $entities ;

public readonly DOMNamedNodeMap $notations ;

public readonly string $internalSubset ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

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 extends DOMNode {

/* 属性 */

public readonly bool $schemaTypeInfo ;

public readonly string $tagName ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public __construct ( string $name [, string $value [, string $namespaceURI ]] )

public string getAttribute ( string $name )

public DOMAttr getAttributeNode ( string $name )

public DOMAttr getAttributeNodeNS ( string $namespaceURI , string $localName )

public string getAttributeNS ( string $namespaceURI , string $localName )

public DOMNodeList getElementsByTagName ( string $name )

public DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )

public bool hasAttribute ( string $name )

public bool hasAttributeNS ( string $namespaceURI , string $localName )

public bool removeAttribute ( string $name )

public bool removeAttributeNode ( DOMAttr $oldnode )

public bool removeAttributeNS ( string $namespaceURI , string $localName )

public DOMAttr setAttribute ( string $name , string $value )

public DOMAttr setAttributeNode ( DOMAttr $attr )

public DOMAttr setAttributeNodeNS ( DOMAttr $attr )

public void setAttributeNS ( string $namespaceURI , string $qualifiedName , string $value )

public void setIdAttribute ( string $name , bool $isId )

public void setIdAttributeNode ( DOMAttr $attr , bool $isId )

public void setIdAttributeNS ( string $namespaceURI , string $localName , bool $isId )

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

schemaTypeInfo
Not implemented yet, always return NULL

tagName
The element name

注释

Note:

The DOM extension uses UTF-8 encoding. Use class="function">utf8_encode and class="function">utf8_decode to work with texts in ISO-8859-1 encoding or iconv for other encodings.

DOMElement::__construct

Creates a new DOMElement object

说明

public DOMElement::__construct ( string $name [, string $value [, 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 ( 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 ( 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 DOMNameSpaceNode is returned instead of a DOMAttr.

参见

  • DOMElement::hasAttribute
  • DOMElement::setAttributeNode
  • DOMElement::removeAttributeNode

DOMElement::getAttributeNodeNS

Returns attribute node

说明

public DOMAttr DOMElement::getAttributeNodeNS ( string $namespaceURI , 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 DOMNameSpaceNode is returned instead of a DOMAttr object.

参见

  • DOMElement::hasAttributeNS
  • DOMElement::setAttributeNodeNS
  • DOMElement::removeAttributeNode

DOMElement::getAttributeNS

Returns value of attribute

说明

public string DOMElement::getAttributeNS ( string $namespaceURI , 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 DOMNodeList DOMElement::getElementsByTagName ( string $name )

This function returns a new instance of the class 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 DOMNodeList of all matched elements.

参见

  • DOMElement::getElementsByTagNameNS

DOMElement::getElementsByTagNameNS

Get elements by namespaceURI and localName

说明

public DOMNodeList DOMElement::getElementsByTagNameNS ( string $namespaceURI , 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 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 ( 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 ( string $namespaceURI , 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 ( 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 ( 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 ( string $namespaceURI , 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 ( 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 ( 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 ( 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 ( string $namespaceURI , string $qualifiedName , 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 ( 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 ( 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 ( string $namespaceURI , string $localName , 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 extends DOMNode {

/* 属性 */

public readonly string $publicId ;

public readonly string $systemId ;

public readonly string $notationName ;

public string $actualEncoding ;

public readonly string $encoding ;

public readonly string $version ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

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 extends DOMNode {

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public __construct ( string $name )

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

DOMEntityReference::__construct

Creates a new DOMEntityReference object

说明

public DOMEntityReference::__construct ( 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>&nbsp;</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 extends Exception {

/* 属性 */

public readonly int $code ;

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string Exception::getMessage ( void )

final public Throwable Exception::getPrevious ( void )

final public int Exception::getCode ( void )

final public string Exception::getFile ( void )

final public int Exception::getLine ( void )

final public array Exception::getTrace ( void )

final public string Exception::getTraceAsString ( void )

public string Exception::__toString ( void )

final private void Exception::__clone ( void )

}

属性

code
An integer indicating the type of error generated

简介

The DOMImplementation interface provides a number of methods for performing operations that are independent of any particular instance of the document object model.

类摘要

DOMImplementation

class DOMImplementation {

/* 属性 */

/* 方法 */

__construct ( void )

public DOMDocument createDocument ([ string $namespaceURI = NULL [, string $qualifiedName = NULL [, DOMDocumentType $doctype = NULL ]]] )

public DOMDocumentType createDocumentType ([ string $qualifiedName = NULL [, string $publicId = NULL [, string $systemId = NULL ]]] )

public bool hasFeature ( string $feature , string $version )

}

DOMImplementation::__construct

Creates a new DOMImplementation object

说明

DOMImplementation::__construct ( void )

Creates a new DOMImplementation object.

DOMImplementation::createDocument

Creates a DOMDocument object of the specified type with its document element

说明

public DOMDocument DOMImplementation::createDocument ([ 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
  • DOMImplementation::createDocumentType

DOMImplementation::createDocumentType

Creates an empty DOMDocumentType object

说明

public DOMDocumentType 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 ( 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 implements Traversable , Countable {

/* 属性 */

public readonly int $length ;

/* 方法 */

public int count ( void )

DOMNode getNamedItem ( string $name )

DOMNode getNamedItemNS ( string $namespaceURI , 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 DOMNamedNodeMap::count ( 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 DOMNamedNodeMap::getNamedItem ( 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 DOMNamedNodeMap::getNamedItemNS ( string $namespaceURI , 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 DOMNamedNodeMap::item ( int $index )

Retrieves a node specified by index within the 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 readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public DOMNode appendChild ( DOMNode $newnode )

public string C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode cloneNode ([ bool $deep ] )

public int getLineNo ( void )

public string getNodePath ( void )

public bool hasAttributes ( void )

public bool hasChildNodes ( void )

public DOMNode insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool isDefaultNamespace ( string $namespaceURI )

public bool isSameNode ( DOMNode $node )

public bool isSupported ( string $feature , string $version )

public string lookupNamespaceUri ( string $prefix )

public string lookupPrefix ( string $namespaceURI )

public void normalize ( void )

public DOMNode removeChild ( DOMNode $oldnode )

public DOMNode replaceChild ( 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 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 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 DOMElement) or NULL otherwise.

ownerDocument
The DOMDocument object associated with this node, or NULL if this node is a 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:

The DOM extension uses UTF-8 encoding. Use class="function">utf8_encode and class="function">utf8_decode to work with texts in ISO-8859-1 encoding or iconv for other encodings.

更新日志

版本说明
5.6.1The 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 ( 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. DOMDocument::createElement, DOMDocument::createTextNode etc. or simply by using any other node.

参数

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 string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, 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 int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, 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 ([ 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 DOMNode::getLineNo ( 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 ( 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 ( 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 ( 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 ( 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.

参数

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 ( 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 ( 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 ( string $feature , string $version )

Checks if the asked feature is supported for the specified version.

参数

feature
The feature to test. See the example of 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 ( 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 ( 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 ( void )

Remove empty text nodes and merge adjacent text nodes in this node and all its children.

返回值

没有返回值。

参见

DOMNode::removeChild

Removes child from list of children

说明

public DOMNode DOMNode::removeChild ( 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 ( 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 implements Traversable , Countable {

/* 属性 */

public readonly int $length ;

/* 方法 */

public int 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.0The Countable interface is implemented and returns the value of the length property.

参见

DOMNodeList::count

Get number of nodes in the list

说明

public int DOMNodeList::count ( 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 DOMNodeList::item ( int $index )

Retrieves a node specified by index within the 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 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 extends DOMNode {

/* 属性 */

public readonly string $publicId ;

public readonly string $systemId ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

publicId

systemId

类摘要

DOMProcessingInstruction

class DOMProcessingInstruction extends DOMNode {

/* 属性 */

public readonly string $target ;

public string $data ;

/* 继承的属性 */

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public __construct ( string $name [, string $value ] )

/* 继承的方法 */

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

target

data

DOMProcessingInstruction::__construct

Creates a new DOMProcessingInstruction object

说明

public DOMProcessingInstruction::__construct ( string $name [, 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 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>

参见

  • DOMDocument::createProcessingInstruction

简介

The DOMText class inherits from DOMCharacterData and represents the textual content of a DOMElement or DOMAttr.

类摘要

DOMText

class DOMText extends DOMCharacterData {

/* 属性 */

public readonly string $wholeText ;

/* 继承的属性 */

public string $data ;

public readonly int $length ;

public readonly string $nodeName ;

public string $nodeValue ;

public readonly int $nodeType ;

public readonly DOMNode $parentNode ;

public readonly DOMNodeList $childNodes ;

public readonly DOMNode $firstChild ;

public readonly DOMNode $lastChild ;

public readonly DOMNode $previousSibling ;

public readonly DOMNode $nextSibling ;

public readonly DOMNamedNodeMap $attributes ;

public readonly DOMDocument $ownerDocument ;

public readonly string $namespaceURI ;

public string $prefix ;

public readonly string $localName ;

public readonly string $baseURI ;

public string $textContent ;

/* 方法 */

public __construct ([ string $value ] )

public bool isElementContentWhitespace ( void )

public bool isWhitespaceInElementContent ( void )

public DOMText splitText ( int $offset )

/* 继承的方法 */

public void DOMCharacterData::appendData ( string $data )

public void DOMCharacterData::deleteData ( int $offset , int $count )

public void DOMCharacterData::insertData ( int $offset , string $data )

public void DOMCharacterData::replaceData ( int $offset , int $count , string $data )

public string DOMCharacterData::substringData ( int $offset , int $count )

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

public int DOMNode::C14NFile ( string $uri [, bool $exclusive = FALSE [, bool $with_comments = FALSE [, array $xpath [, array $ns_prefixes ]]]] )

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

public int DOMNode::getLineNo ( void )

public string DOMNode::getNodePath ( void )

public bool DOMNode::hasAttributes ( void )

public bool DOMNode::hasChildNodes ( void )

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

public bool DOMNode::isSameNode ( DOMNode $node )

public bool DOMNode::isSupported ( string $feature , string $version )

public string DOMNode::lookupNamespaceUri ( string $prefix )

public string DOMNode::lookupPrefix ( string $namespaceURI )

public void DOMNode::normalize ( void )

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

}

属性

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 DOMText::__construct ([ 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 ( 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 DOMDocument $document ;

/* 方法 */

public __construct ( DOMDocument $doc )

public mixed evaluate ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = TRUE ]] )

public DOMNodeList query ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = TRUE ]] )

public bool registerNamespace ( string $prefix , string $namespaceURI )

public void registerPhpFunctions ([ mixed $restrict ] )

}

属性

document

DOMXPath::__construct

Creates a new DOMXPath object

说明

public DOMXPath::__construct ( DOMDocument $doc )

Creates a new DOMXPath object.

参数

doc
The DOMDocument associated with the DOMXPath.

DOMXPath::evaluate

Evaluates the given XPath expression and returns a typed result if possible

说明

public mixed DOMXPath::evaluate ( string $expression [, DOMNode $contextnode [, 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 DOMNodeList containing all nodes matching the given XPath expression.

If the expression is malformed or the contextnode is invalid, DOMXPath::evaluate returns FALSE.

更新日志

版本说明
5.3.3The registerNodeNS parameter was added.

范例

示例 #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 DOMNodeList DOMXPath::query ( string $expression [, DOMNode $contextnode [, 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 DOMNodeList.

If the expression is malformed or the contextnode is invalid, DOMXPath::query returns FALSE.

更新日志

版本说明
5.3.3The registerNodeNS parameter was added.

范例

示例 #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 ( 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 ([ 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 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 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