Create a SimpleDOM object from a HTML string
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
Create a SimpleDOM object from a HTML file
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
Sort an array of nodes
Note that nodes are sorted in place, nothing is returned
static
void
sort
( &$nodes)
-
array
&$nodes: Array of SimpleXMLElement
Add given class to current node
-
string
$class: Class name
Return the current element as a DOMElement
DOMElement
asDOM
()
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.
mixed
asPrettyXML
([string $filepath = null])
-
string
$filepath: If set, save the result to this file
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
SimpleDOM
cloneChildrenFrom
(
$src, [
bool $deep =
true])
-
SimpleXMLElement
$src: Source node
-
bool
$deep: If TRUE, clone descendant nodes as well
Copy all attributes from a node to current node
SimpleDOM
copyAttributesFrom
(
$src, [
bool $overwrite =
true])
-
SimpleXMLElement
$src: Source node
-
bool
$overwrite: If TRUE, overwrite existing attributes. Otherwise, ignore duplicate attributes
Delete all elements matching a XPath expression
integer
deleteNodes
(string $xpath)
-
string
$xpath: XPath expression
Delete this node from document
This is a convenience method. The same result can be achieved with
$node->parentNode()->removeChild($node);
void
deleteSelf
()
Return the first node of the result of an XPath expression
mixed
firstOf
(string $xpath)
-
string
$xpath: XPath expression
Return all elements with the given class name
Should work like DOM0's method
array
getElementsByClassName
(string $class)
-
string
$class: Class name
Test whether current node has given class
bool
hasClass
(string $class)
-
string
$class: Class name
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.
string
innerHTML
()
Return the XML content of current node as a string
string
innerXML
()
Add a new sibling after this node
This is a convenience method. The same result can be achieved with
$node->parentNode()->insertBefore($new, $node->nextSibling());
-
SimpleXMLElement
$new: New node
Add a new sibling before this node
This is a convenience method. The same result can be achieved with
$node->parentNode()->insertBefore($new, $node);
-
SimpleXMLElement
$new: New node
Insert a CDATA section
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
Insert a comment node
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
Insert a Processing Instruction
The content of the PI can be passed either as string or as an associative array.
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
Insert a text node
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
Insert raw XML data
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
Move current node to a new parent
ATTENTION! using references to the old node will screw up the original document
-
SimpleXMLElement
$dst: Target parent
Return the XML representing this node and its child nodes
NOTE: unlike asXML() it doesn't return the XML prolog
string
outerXML
()
Remove given class from current node
-
string
$class: Class name
Remove all elements matching a XPath expression
array
removeNodes
(string $xpath)
-
string
$xpath: XPath expression
Remove this node from document
This is a convenience method. The same result can be achieved with
$node->parentNode()->removeChild($node);
Remove all elements matching a XPath expression
array
replaceNodes
(string $xpath, $new)
-
string
$xpath: XPath expression
-
SimpleXMLElement
$new: Replacement node
Replace this node
This is a convenience method. The same result can be achieved with
$node->parentNode()->replaceChild($new, $node);
-
SimpleXMLElement
$new: New node
Set several attributes at once
SimpleDOM
setAttributes
(
$attr, [
string $ns =
null])
-
array
$attr: Attributes as name => value pairs
-
string
$ns: Namespace for the attributes
Sort this node's children
ATTENTION: text nodes are not supported. If current node has text nodes, they may be lost in the process
Run an XPath query and sort the result
This method accepts any number of arguments in a way similar to array_multisort()
// Retrieve all <x/> nodes, sorted by @foo ascending, @bar descending
// Same, but sort @foo numerically and @bar as strings
$root->sortedXPath('//x', '@foo', SORT_NUMERIC, '@bar', SORT_STRING, SORT_DESC);
void
sortedXPath
(string $xpath)
-
string
$xpath: XPath expression
Transform current node and return the result
Will take advantage of PECL's xslcache if available
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 ( )