XXXI. DOM Functions

Въведение

The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.

For PHP 4, use DOM XML.

Забележка: DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.

Инсталация

Не е необходимо инсталиране, за да се използват тези функции. Те са част от ядрото на PHP.

Предефинирани класове

The API of the module follows the DOM Level 3 standard as closely as possible. Consequently, the API is fully object-oriented. It is a good idea to have the DOM standard available when using this module.

This module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.

DOMAttr

Extends DOMNode. The DOMAttr interface represents an attribute in an DOMElement object.

Конструктор

  • - construct a new DOMAttr object

Методи

  • - Checks if attribute is a defined ID

Свойства

Таблица 1.

DOMCharacterData

Extends DOMNode.

Методи

  • - Append a string to the end of the character data of the node

  • - Remove a range of characters from the node

  • - Insert a string at the specified 16-bit unit offset

  • - Replace a substring within the DOMCharacterData node

  • - Extracts a range of data from the node

Свойства

Таблица 2.

DOMComment

Extends DOMCharacterData.

Конструктор

  • - construct a new DOMComment object

DOMDocument

Extends DOMNode.

Конструктор

  • - construct a new DOMDocument object

Методи

  • - Create new attribute

  • - Create new attribute node with an associated namespace

  • - Create new cdata node

  • - Create new comment node

  • - Create new document fragment

  • - Create new element node

  • - Create new element node with an associated namespace

  • - Create new entity reference node

  • - Creates new PI node

  • - Create new text node

  • - Searches for an element with a certain id

  • - Searches for all elements with given tag name

  • - Searches for all elements with given tag name in specified namespace

  • - Import node into current document

  • - Load XML from a file

  • - Load HTML from a string

  • - Load HTML from a file

  • - Load XML from a string

  • - Normalizes the document

  • - Performs relaxNG validation on the document

  • - Performs relaxNG validation on the document

  • - Register extended class used to create base node type (not DOM standard)

  • - Dumps the internal XML tree back into a file

  • - Dumps the internal document into a string using HTML formatting

  • - Dumps the internal document back into a file using HTML formatting

  • - Dumps the internal XML tree back into a string

  • - Validates a document based on a schema

  • - Validates a document based on a schema

  • - Validates the document based on its DTD

  • - Substitutes XIncludes in a DOMDocument Object

Свойства

Таблица 3.

DOMDocumentFragment

Extends DOMNode.

Методи

  • - Append raw XML data (not DOM standard)

DOMDocumentType

Extends DOMNode

Each DOMDocument has a doctype attribute whose value is either NULL or a DOMDocumentType object.

Свойства

Таблица 4.

DOMElement

Extends DOMNode.

Конструктор

  • - construct a new DOMElement object

Методи

  • - Returns value of attribute

  • - Returns attribute node

  • - Returns attribute node

  • - Returns value of attribute

  • - Gets elements by tagname

  • - Get elements by namespaceURI and localName

  • - Checks to see if attribute exists

  • - Checks to see if attribute exists

  • - Removes attribute

  • - Removes attribute

  • - Removes attribute

  • - Adds new attribute

  • - Adds new attribute node to element

  • - Adds new attribute node to element

  • - Adds new attribute

  • - Declares the ID attribute

  • - Declares the ID attribute

  • - Declares the ID attribute

Свойства

Таблица 5.

DOMEntity

Extends DOMNode

This interface represents a known entity, either parsed or unparsed, in an XML document.

Свойства

Таблица 6.

DOMEntityReference

Extends DOMNode.

Конструктор

  • - construct a new DOMEntityReference object

DOMException

DOM operations raise exceptions under particular circumstances, i.e., when an operation is impossible to perform for logical reasons.

See also .

Свойства

Таблица 7.

DOMImplementation

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

Конструктор

  • - construct a new DOMImplementation object

Методи

  • - Creates a DOM Document object of the specified type with its document element

  • - Creates an empty DOMDocumentType object

  • - Test if the DOM implementation implements a specific feature

DOMNamedNodeMap

Методи

  • - Retrieves a node specified by name

  • - Retrieves a node specified by local name and namespace URI

  • - Retrieves a node specified by index

DOMNode

Методи

  • - Adds new child at the end of the children

  • - Clones a node

  • - Checks if node has attributes

  • - Checks if node has children

  • - Adds a new child before a reference node

  • - Checks if the specified namespaceURI is the default namespace

  • - Indicates if two nodes are the same node

  • - Checks if feature is supported for specified version

  • - Returns namespace URI of the node based on the prefix

  • - Returns name space prefix of the node based on namespaceURI

  • - Normalizes the node

  • - Removes child from list of children

  • - Replaces a child

Свойства

Таблица 8.

DOMNodeList

Методи

  • - Retrieves a node specified by index

Свойства

Таблица 9.

DOMNotation

Extends DOMNode

Свойства

Таблица 10.

DOMProcessingInstruction

Extends DOMNode.

Конструктор

  • - construct a new DOMProcessingInstruction object

Свойства

Таблица 11.

DOMText

Extends DOMCharacterData.

Конструктор

  • - construct a new DOMText object

Методи

  • - Indicates whether this text node contains whitespace

  • - Breaks the node into two nodes at the specified offset

Свойства

Таблица 12.

DOMXPath

Конструктор

  • - construct a new DOMXPath object

Методи

  • - Registers the namespace with the DOMXpath object

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

  • - Evaluates the given XPath expression

Свойства

Таблица 13.

Примери

Many examples in this reference require an XML file. We will use book.xml that contains the following:

Пример 1. book.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
]>
<book id="listing">
 <title>My lists</title>
 <chapter id="books">
  <title>My books</title>
  <para>
   <informaltable>
    <tgroup cols="4">
     <thead>
      <row>
       <entry>Title</entry>
       <entry>Author</entry>
       <entry>Language</entry>
       <entry>ISBN</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>The Grapes of Wrath</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>0140186409</entry>
      </row>
      <row>
       <entry>The Pearl</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>014017737X</entry>
      </row>
      <row>
       <entry>Samarcande</entry>
       <entry>Amine Maalouf</entry>
       <entry>fr</entry>
       <entry>2253051209</entry>
      </row>
      <!-- TODO: I have a lot of remaining books to add.. -->
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </chapter>
</book>

Предефинирани константи

Константите по-долу са дефинирани в това разширение и ще бъдат налични единствено, когато това разширение е компилирано в PHP или динамично пуснато по време на изпълнение.

Таблица 14. XML constants

Таблица 15. DOMException constants

Съдържание
DOMAttr->__construct() --  Creates a new DOMAttr object
DOMAttr->isId() --  Checks if attribute is a defined ID
DOMCharacterData->appendData() --  Append the string to the end of the character data of the node
DOMCharacterData->deleteData() --  Remove a range of characters from the node
DOMCharacterData->insertData() --  Insert a string at the specified 16-bit unit offset
DOMCharacterData->replaceData() --  Replace a substring within the DOMCharacterData node
DOMCharacterData->substringData() --  Extracts a range of data from the node
DOMComment->__construct() --  Creates a new DOMComment object
DOMDocument->__construct() --  Creates a new DOMDocument object
DOMDocument->createAttribute() -- Create new attribute
DOMDocument->createAttributeNS() --  Create new attribute node with an associated namespace
DOMDocument->createCDATASection() -- Create new cdata node
DOMDocument->createComment() -- Create new comment node
DOMDocument->createDocumentFragment() -- Create new document fragment
DOMDocument->createElement() -- Create new element node
DOMDocument->createElementNS() --  Create new element node with an associated namespace
DOMDocument->createEntityReference() -- Create new entity reference node
DOMDocument->createProcessingInstruction() -- Creates new PI node
DOMDocument->createTextNode() -- Create new text node
DOMDocument->getElementById() -- Searches for an element with a certain id
DOMDocument->getElementsByTagName() -- Searches for all elements with given tag name
DOMDocument->getElementsByTagNameNS() --  Searches for all elements with given tag name in specified namespace
DOMDocument->importNode() -- Import node into current document
DOMDocument->load() --  Load XML from a file
DOMDocument->loadHTML() --  Load HTML from a string
DOMDocument->loadHTMLFile() --  Load HTML from a file
DOMDocument->loadXML() --  Load XML from a string
DOMDocument->normalizeDocument() -- Normalizes the document
DOMDocument->registerNodeClass() -- Register extended class used to create base node type
DOMDocument->relaxNGValidate() --  Performs relaxNG validation on the document
DOMDocument->relaxNGValidateSource() --  Performs relaxNG validation on the document
DOMDocument->save() --  Dumps the internal XML tree back into a file
DOMDocument->saveHTML() --  Dumps the internal document into a string using HTML formatting
DOMDocument->saveHTMLFile() --  Dumps the internal document into a file using HTML formatting
DOMDocument->saveXML() --  Dumps the internal XML tree back into a string
DOMDocument->schemaValidate() --  Validates a document based on a schema
DOMDocument->schemaValidateSource() --  Validates a document based on a schema
DOMDocument->validate() --  Validates the document based on its DTD
DOMDocument->xinclude() --  Substitutes XIncludes in a DOMDocument Object
DOMDocumentFragment->appendXML() -- Append raw XML data
DOMElement->__construct() --  Creates a new DOMElement object
DOMElement->getAttribute() -- Returns value of attribute
DOMElement->getAttributeNode() -- Returns attribute node
DOMElement->getAttributeNodeNS() --  Returns attribute node
DOMElement->getAttributeNS() -- Returns value of attribute
DOMElement->getElementsByTagName() -- Gets elements by tagname
DOMElement->getElementsByTagNameNS() -- Get elements by namespaceURI and localName
DOMElement->hasAttribute() -- Checks to see if attribute exists
DOMElement->hasAttributeNS() --  Checks to see if attribute exists
DOMElement->removeAttribute() -- Removes attribute
DOMElement->removeAttributeNode() -- Removes attribute
DOMElement->removeAttributeNS() -- Removes attribute
DOMElement->setAttribute() -- Adds new attribute
DOMElement->setAttributeNode() -- Adds new attribute node to element
DOMElement->setAttributeNodeNS() -- Adds new attribute node to element
DOMElement->setAttributeNS() -- Adds new attribute
DOMElement->setIdAttribute() -- Declares the attribute specified by name to be of type ID
DOMElement->setIdAttributeNode() -- Declares the attribute specified by node to be of type ID
DOMElement->setIdAttributeNS() -- Declares the attribute specified by local name and namespace URI to be of type ID
DOMEntityReference->__construct() --  Creates a new DOMEntityReference object
DOMImplementation->__construct() --  Creates a new DOMImplementation object
DOMImplementation->createDocument() --  Creates a DOMDocument object of the specified type with its document element
DOMImplementation->createDocumentType() --  Creates an empty DOMDocumentType object
DOMImplementation->hasFeature() --  Test if the DOM implementation implements a specific feature
DOMNamedNodeMap->getNamedItem() --  Retrieves a node specified by name
DOMNamedNodeMap->getNamedItemNS() --  Retrieves a node specified by local name and namespace URI
DOMNamedNodeMap->item() -- Retrieves a node specified by index
DOMNode->appendChild() --  Adds new child at the end of the children
DOMNode->cloneNode() --  Clones a node
DOMNode->hasAttributes() --  Checks if node has attributes
DOMNode->hasChildNodes() --  Checks if node has children
DOMNode->insertBefore() --  Adds a new child before a reference node
DOMNode->isDefaultNamespace() -- Checks if the specified namespaceURI is the default namespace or not
DOMNode->isSameNode() --  Indicates if two nodes are the same node
DOMNode->isSupported() --  Checks if feature is supported for specified version
DOMNode->lookupNamespaceURI() --  Gets the namespace URI of the node based on the prefix
DOMNode->lookupPrefix() --  Gets the namespace prefix of the node based on the namespace URI
DOMNode->normalize() --  Normalizes the node
DOMNode->removeChild() --  Removes child from list of children
DOMNode->replaceChild() --  Replaces a child
DOMNodelist->item() --  Retrieves a node specified by index
DOMProcessingInstruction->__construct() --  Creates a new DOMProcessingInstruction object
DOMText->__construct() --  Creates a new DOMText object
DOMText->isWhitespaceInElementContent() --  Indicates whether this text node contains whitespace
DOMText->splitText() --  Breaks this node into two nodes at the specified offset
DOMXPath->__construct() --  Creates a new DOMXPath object
DOMXPath->evaluate() --  Evaluates the given XPath expression and returns a typed result if possible.
DOMXPath->query() --  Evaluates the given XPath expression
DOMXPath->registerNamespace() --  Registers the namespace with the DOMXPath object
dom_import_simplexml --  Gets a DOMElement object from a SimpleXMLElement object