class XMLReader (View source)

The XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.

Constants

NONE

No node type

ELEMENT

Start element

ATTRIBUTE

Attribute node

TEXT

Text node

CDATA

CDATA node

ENTITY_REF

Entity Reference node

ENTITY

Entity Declaration node

PI

Processing Instruction node

COMMENT

Comment node

DOC

Document node

DOC_TYPE

Document Type node

DOC_FRAGMENT

Document Fragment node

NOTATION

Notation node

WHITESPACE

Whitespace node

SIGNIFICANT_WHITESPACE

Significant Whitespace node

END_ELEMENT

End Element

END_ENTITY

End Entity

XML_DECLARATION

XML Declaration node

LOADDTD

Load DTD but do not validate

DEFAULTATTRS

Load DTD and default attributes but do not validate

VALIDATE

Load DTD and validate while parsing

SUBST_ENTITIES

Substitute entities and expand references

Properties

int read-only $attributeCount

The number of attributes on the node

string read-only $baseURI

The base URI of the node

int read-only $depth

Depth of the node in the tree, starting at 0

bool read-only $hasAttributes

Indicates if node has attributes

bool read-only $hasValue

Indicates if node has a text value

bool read-only $isDefault

Indicates if attribute is defaulted from DTD

bool read-only $isEmptyElement

Indicates if node is an empty element tag

string read-only $localName

The local name of the node

string read-only $name

The qualified name of the node

string read-only $namespaceURI

The URI of the namespace associated with the node

int read-only $nodeType

The node type for the node

string read-only $prefix

The prefix of the namespace associated with the node

string read-only $value

The text value of the node

string read-only $xmlLang

The xml:lang scope which the node resides

Methods

bool
close()

Close the XMLReader input

string|null
getAttribute(string $name)

Get the value of a named attribute

string|null
getAttributeNo(int $index)

Get the value of an attribute by index

string|null
getAttributeNs(string $name, string $namespace)

Get the value of an attribute by localname and URI

bool
getParserProperty(int $property)

Indicates if specified property has been set

bool
isValid()

Indicates if the parsed document is valid

string|null
lookupNamespace(string $prefix)

Lookup namespace for a prefix

bool
moveToAttributeNo(int $index)

Move cursor to an attribute by index

bool
moveToAttribute(string $name)

Move cursor to a named attribute

bool
moveToAttributeNs(string $name, string $namespace)

Move cursor to a named attribute

bool
moveToElement()

Position cursor on the parent Element of current Attribute

bool
moveToFirstAttribute()

Position cursor on the first Attribute

bool
moveToNextAttribute()

Position cursor on the next Attribute

static XMLReader|bool
open(string $uri, string $encoding = null, int $flags = 0)

Set the URI containing the XML to parse

bool
read()

Move to next node in document

bool
next(string $name = null)

Move cursor to next node skipping all subtrees

string
readInnerXml()

Retrieve XML from current node

string
readOuterXml()

Retrieve XML from current node, including it self

string
readString()

Reads the contents of the current node as a string

bool
setSchema(string $filename)

Validate document against XSD

bool
setParserProperty(int $property, bool $value)

Set parser options

bool
setRelaxNGSchema(string $filename)

Set the filename or URI for a RelaxNG Schema

bool
setRelaxNGSchemaSource(string $source)

Set the data containing a RelaxNG Schema

static XMLReader|bool
XML(string $source, string $encoding = null, int $flags = 0)

Set the data containing the XML to parse

DOMNode|false
expand(null|DOMNode $baseNode = null)

Returns a copy of the current node as a DOM object

static XMLReader
fromUri(string $uri, string|null $encoding = null, int $flags = 0)

No description

static XMLReader
fromStream($stream, string|null $encoding = null, int $flags = 0, string|null $documentUri = null)

No description

static XMLReader
fromString(string $source, string|null $encoding = null, int $flags = 0)

No description

Details

bool close()

Since: 5.1.2

Close the XMLReader input

Return Value

bool

TRUE on success or FALSE on failure.

string|null getAttribute(string $name)

Since: 5.1.2

Get the value of a named attribute

Parameters

string $name

The name of the attribute.

Return Value

string|null

The value of the attribute, or NULL if no attribute with the given name is found or not positioned on an element node.

string|null getAttributeNo(int $index)

Since: 5.1.2

Get the value of an attribute by index

Parameters

int $index

The position of the attribute.

Return Value

string|null

The value of the attribute, or NULL if no attribute exists at index or not positioned of element.

string|null getAttributeNs(string $name, string $namespace)

Since: 5.1.2

Get the value of an attribute by localname and URI

Parameters

string $name

The local name.

string $namespace

The namespace URI.

Return Value

string|null

The value of the attribute, or NULL if no attribute with the given localName and namespaceURI is found or not positioned of element.

bool getParserProperty(int $property)

Since: 5.1.2

Indicates if specified property has been set

Parameters

int $property

One of the parser option constants.

Return Value

bool

TRUE on success or FALSE on failure.

bool isValid()

Since: 5.1.2

Indicates if the parsed document is valid

Return Value

bool

TRUE on success or FALSE on failure.

string|null lookupNamespace(string $prefix)

Since: 5.1.2

Lookup namespace for a prefix

Parameters

string $prefix

String containing the prefix.

Return Value

string|null

TRUE on success or FALSE on failure.

bool moveToAttributeNo(int $index)

Since: 5.1.2

Move cursor to an attribute by index

Parameters

int $index

The position of the attribute.

Return Value

bool

TRUE on success or FALSE on failure.

bool moveToAttribute(string $name)

Since: 5.1.2

Move cursor to a named attribute

Parameters

string $name

The name of the attribute.

Return Value

bool

TRUE on success or FALSE on failure.

bool moveToAttributeNs(string $name, string $namespace)

Since: 5.1.2

Move cursor to a named attribute

Parameters

string $name

The local name.

string $namespace

The namespace URI.

Return Value

bool

TRUE on success or FALSE on failure.

bool moveToElement()

Since: 5.1.2

Position cursor on the parent Element of current Attribute

Return Value

bool

TRUE if successful and FALSE if it fails or not positioned on Attribute when this method is called.

bool moveToFirstAttribute()

Since: 5.1.2

Position cursor on the first Attribute

Return Value

bool

TRUE on success or FALSE on failure.

bool moveToNextAttribute()

Since: 5.1.2

Position cursor on the next Attribute

Return Value

bool

TRUE on success or FALSE on failure.

static XMLReader|bool open(string $uri, string $encoding = null, int $flags = 0)

Since: 5.1.2

Set the URI containing the XML to parse

Parameters

string $uri

URI pointing to the document.

string $encoding

[optional]

The document encoding or NULL.

int $flags

[optional]

A bitmask of the LIBXML_* constants.

Return Value

XMLReader|bool

TRUE on success or FALSE on failure. If called statically, returns an XMLReader or FALSE on failure.

bool read()

Since: 5.1.2

Move to next node in document

Return Value

bool

TRUE on success or FALSE on failure.

bool next(string $name = null)

Since: 5.1.2

Move cursor to next node skipping all subtrees

Parameters

string $name

[optional]

The name of the next node to move to.

Return Value

bool

TRUE on success or FALSE on failure.

string readInnerXml()

Retrieve XML from current node

Return Value

string

the contents of the current node as a string. Empty string on failure.

string readOuterXml()

Retrieve XML from current node, including it self

Return Value

string

the contents of current node, including itself, as a string. Empty string on failure.

string readString()

Reads the contents of the current node as a string

Return Value

string

the content of the current node as a string. Empty string on failure.

bool setSchema(string $filename)

Validate document against XSD

Parameters

string $filename

The filename of the XSD schema.

Return Value

bool

TRUE on success or FALSE on failure.

bool setParserProperty(int $property, bool $value)

Since: 5.1.2

Set parser options

Parameters

int $property

One of the parser option constants.

bool $value

If set to TRUE the option will be enabled otherwise will be disabled.

Return Value

bool

TRUE on success or FALSE on failure.

bool setRelaxNGSchema(string $filename)

Set the filename or URI for a RelaxNG Schema

Parameters

string $filename

filename or URI pointing to a RelaxNG Schema.

Return Value

bool

TRUE on success or FALSE on failure.

bool setRelaxNGSchemaSource(string $source)

Since: 5.1.2

Set the data containing a RelaxNG Schema

Parameters

string $source

String containing the RelaxNG Schema.

Return Value

bool

TRUE on success or FALSE on failure.

static XMLReader|bool XML(string $source, string $encoding = null, int $flags = 0)

Since: 5.1.2

Set the data containing the XML to parse

Parameters

string $source

String containing the XML to be parsed.

string $encoding

[optional]

The document encoding or NULL.

int $flags

[optional]

A bitmask of the LIBXML_* constants.

Return Value

XMLReader|bool

TRUE on success or FALSE on failure. If called statically, returns an XMLReader or FALSE on failure.

DOMNode|false expand(null|DOMNode $baseNode = null)

Since: 5.1.2

Returns a copy of the current node as a DOM object

Parameters

null|DOMNode $baseNode [optional]

Return Value

DOMNode|false

The resulting DOMNode or FALSE on error.

static XMLReader fromUri(string $uri, string|null $encoding = null, int $flags = 0)

Since: 8.4

No description

Parameters

string $uri
string|null $encoding
int $flags

Return Value

XMLReader

static XMLReader fromStream($stream, string|null $encoding = null, int $flags = 0, string|null $documentUri = null)

Since: 8.4

No description

Parameters

$stream
string|null $encoding
int $flags
string|null $documentUri

Return Value

XMLReader

static XMLReader fromString(string $source, string|null $encoding = null, int $flags = 0)

Since: 8.4

No description

Parameters

string $source
string|null $encoding
int $flags

Return Value

XMLReader