Class SimpleDOM

Description

Located in /SimpleDOM.php (line 69)

SimpleXMLElement
   |
   --SimpleDOM
Method Summary
static SimpleDOM loadHTML (string $source, [mixed &$errors = null])
static SimpleDOM loadHTMLFile (string $filename, [mixed &$errors = null])
static void sort ( &$nodes)
SimpleDOM addClass (string $class)
DOMElement asDOM ()
mixed asPrettyXML ([string $filepath = null])
SimpleDOM cloneChildrenFrom ( $src, [bool $deep = true])
SimpleDOM copyAttributesFrom ( $src, [bool $overwrite = true])
integer deleteNodes (string $xpath)
void deleteSelf ()
mixed firstOf (string $xpath)
array getElementsByClassName (string $class)
bool hasClass (string $class)
string innerHTML ()
string innerXML ()
SimpleDOM insertCDATA (string $content, [string $mode = 'append'])
SimpleDOM insertComment (string $content, [string $mode = 'append'])
bool insertPI (string $target, [string|array $data = null], [ $mode = 'before'])
SimpleDOM insertText (string $content, [string $mode = 'append'])
SimpleDOM insertXML (string $xml, [string $mode = 'append'])
SimpleDOM moveTo ( $dst)
string outerXML ()
SimpleDOM removeClass (string $class)
array removeNodes (string $xpath)
array replaceNodes (string $xpath,  $new)
SimpleDOM setAttributes ( $attr, [string $ns = null])
void sortedXPath (string $xpath)
string XSLT (string $filepath, [bool $use_xslcache = true])
Methods
static method loadHTML (line 83)

Create a SimpleDOM object from a HTML string

  • access: public
static SimpleDOM loadHTML (string $source, [mixed &$errors = null])
  • string $source: HTML source
  • mixed &$errors: Passed by reference. Will be replaced by an array of LibXMLError objects if applicable
static method loadHTMLFile (line 96)

Create a SimpleDOM object from a HTML file

  • access: public
static SimpleDOM loadHTMLFile (string $filename, [mixed &$errors = null])
  • string $filename: Path/URL to HTML file
  • mixed &$errors: Passed by reference. Will be replaced by an array of LibXMLError objects if applicable
static method sort (line 935)

Sort an array of nodes

Note that nodes are sorted in place, nothing is returned

static void sort ( &$nodes)
  • array &$nodes: Array of SimpleXMLElement
addClass (line 735)

Add given class to current node

  • return: Current node
  • access: public
SimpleDOM addClass (string $class)
  • string $class: Class name
asDOM (line 778)

Return the current element as a DOMElement

  • access: public
DOMElement asDOM ()
asPrettyXML (line 794)

Return the current node slightly prettified

Elements will be indented, empty elements will be minified. The result isn't mean to be perfect, I'm sure there are better prettifiers out there.

  • return: If $filepath is set, will return TRUE if the file was succesfully written or FALSE otherwise. If $filepath isn't set, it returns the result as a string
  • access: public
mixed asPrettyXML ([string $filepath = null])
  • string $filepath: If set, save the result to this file
cloneChildrenFrom (line 466)

Clone all children from a node and add them to current node

This method takes a snapshot of the children nodes then append them in order to avoid infinite recursion if the destination node is a descendant of or the source node itself

  • return: Current node
  • access: public
SimpleDOM cloneChildrenFrom ( $src, [bool $deep = true])
  • SimpleXMLElement $src: Source node
  • bool $deep: If TRUE, clone descendant nodes as well
copyAttributesFrom (line 440)

Copy all attributes from a node to current node

  • return: Current node
  • access: public
SimpleDOM copyAttributesFrom ( $src, [bool $overwrite = true])
  • SimpleXMLElement $src: Source node
  • bool $overwrite: If TRUE, overwrite existing attributes. Otherwise, ignore duplicate attributes
deleteNodes (line 348)

Delete all elements matching a XPath expression

  • return: Number of nodes removed
  • access: public
integer deleteNodes (string $xpath)
  • string $xpath: XPath expression
deleteSelf (line 287)

Delete this node from document

This is a convenience method. The same result can be achieved with

  1.  $node->parentNode()->removeChild($node);

  • access: public
void deleteSelf ()
firstOf (line 501)

Return the first node of the result of an XPath expression

  • return: SimpleDOM object if any node was returned, NULL otherwise
  • access: public
mixed firstOf (string $xpath)
  • string $xpath: XPath expression
getElementsByClassName (line 706)

Return all elements with the given class name

Should work like DOM0's method

  • return: Array of SimpleDOM nodes
  • access: public
array getElementsByClassName (string $class)
  • string $class: Class name
hasClass (line 724)

Test whether current node has given class

  • access: public
bool hasClass (string $class)
  • string $class: Class name
innerHTML (line 658)

Return the content of current node as a string

Roughly emulates the innerHTML property found in browsers, although it is not meant to perfectly match any specific implementation.

  • return: Content of current node
  • todo: Write a test for HTML entities that can't be represented in the document's encoding
  • access: public
string innerHTML ()
innerXML (line 677)

Return the XML content of current node as a string

  • return: Content of current node
  • access: public
string innerXML ()
insertAfterSelf (line 269)

Add a new sibling after this node

This is a convenience method. The same result can be achieved with

  1.  $node->parentNode()->insertBefore($new$node->nextSibling());

  • return: The inserted node
  • access: public
SimpleDOM insertAfterSelf ( $new)
  • SimpleXMLElement $new: New node
insertBeforeSelf (line 250)

Add a new sibling before this node

This is a convenience method. The same result can be achieved with

  1.  $node->parentNode()->insertBefore($new$node);

  • return: The inserted node
  • access: public
SimpleDOM insertBeforeSelf ( $new)
  • SimpleXMLElement $new: New node
insertCDATA (line 520)

Insert a CDATA section

  • return: Current node
  • access: public
SimpleDOM insertCDATA (string $content, [string $mode = 'append'])
  • string $content: CDATA content
  • string $mode: Where to add this node: 'append' to current node, 'before' current node or 'after' current node
insertComment (line 534)

Insert a comment node

  • return: Current node
  • access: public
SimpleDOM insertComment (string $content, [string $mode = 'append'])
  • string $content: Comment content
  • string $mode: Where to add this node: 'append' to current node, 'before' current node or 'after' current node
insertPI (line 594)

Insert a Processing Instruction

The content of the PI can be passed either as string or as an associative array.

  • return: TRUE on success, FALSE on failure
  • access: public
bool insertPI (string $target, [string|array $data = null], [ $mode = 'before'])
  • string $target: Target of the processing instruction
  • string|array $data: Content of the processing instruction
  • $mode
insertText (line 548)

Insert a text node

  • return: Current node
  • access: public
SimpleDOM insertText (string $content, [string $mode = 'append'])
  • string $content: CDATA content
  • string $mode: Where to add this node: 'append' to current node, 'before' current node or 'after' current node
insertXML (line 563)

Insert raw XML data

  • return: Current node
  • access: public
SimpleDOM insertXML (string $xml, [string $mode = 'append'])
  • string $xml: XML to insert
  • string $mode: Where to add this tag: 'append' to current node, 'before' current node or 'after' current node
moveTo (line 490)

Move current node to a new parent

ATTENTION! using references to the old node will screw up the original document

  • return: Current node
  • access: public
SimpleDOM moveTo ( $dst)
  • SimpleXMLElement $dst: Target parent
outerXML (line 692)

Return the XML representing this node and its child nodes

NOTE: unlike asXML() it doesn't return the XML prolog

  • return: Content of current node
  • access: public
string outerXML ()
removeClass (line 759)

Remove given class from current node

  • return: Current node
  • access: public
SimpleDOM removeClass (string $class)
  • string $class: Class name
removeNodes (line 381)

Remove all elements matching a XPath expression

  • return: Array of removed nodes on success or FALSE on failure
  • access: public
array removeNodes (string $xpath)
  • string $xpath: XPath expression
removeSelf (line 309)

Remove this node from document

This is a convenience method. The same result can be achieved with

  1.  $node->parentNode()->removeChild($node);

  • return: The removed node
  • access: public
SimpleDOM removeSelf ()
replaceNodes (line 416)

Remove all elements matching a XPath expression

  • return: Array of replaced nodes on success or FALSE on failure
  • access: public
array replaceNodes (string $xpath,  $new)
  • string $xpath: XPath expression
  • SimpleXMLElement $new: Replacement node
replaceSelf (line 333)

Replace this node

This is a convenience method. The same result can be achieved with

  1.  $node->parentNode()->replaceChild($new$node);

  • return: Replaced node on success
  • access: public
SimpleDOM replaceSelf ( $new)
  • SimpleXMLElement $new: New node
setAttributes (line 638)

Set several attributes at once

  • return: Current node
  • access: public
SimpleDOM setAttributes ( $attr, [string $ns = null])
  • array $attr: Attributes as name => value pairs
  • string $ns: Namespace for the attributes
sortChildren (line 907)

Sort this node's children

ATTENTION: text nodes are not supported. If current node has text nodes, they may be lost in the process

  • return: This node
  • access: public
SimpleDOM sortChildren ()
sortedXPath (line 888)

Run an XPath query and sort the result

This method accepts any number of arguments in a way similar to array_multisort()

  1.  // Retrieve all <x/> nodes, sorted by @foo ascending, @bar descending
  2.  $root->sortedXPath('//x''@foo''@bar'SORT_DESC);
  3.  
  4.  // Same, but sort @foo numerically and @bar as strings
  5.  $root->sortedXPath('//x''@foo'SORT_NUMERIC'@bar'SORT_STRINGSORT_DESC);

  • access: public
void sortedXPath (string $xpath)
  • string $xpath: XPath expression
XSLT (line 852)

Transform current node and return the result

Will take advantage of PECL's xslcache if available

  • return: Result
  • access: public
string XSLT (string $filepath, [bool $use_xslcache = true])
  • string $filepath: Path to stylesheet
  • bool $use_xslcache: If TRUE, use the XSL Cache extension if available

Inherited Methods

Inherited From SimpleXMLElement (Internal Class)

constructor __construct ( )
addAttribute ( )
addChild ( )
asXML ( )
attributes ( )
children ( )
count ( )
getDocNamespaces ( )
getName ( )
getNamespaces ( )
registerXPathNamespace ( )
saveXML ( )
xpath ( )
__toString ( )

Documentation generated on Thu, 01 Apr 2010 08:34:34 +0200 by phpDocumentor 1.4.3