XMLReader

目录

简介

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.

类摘要

XMLReader

class XMLReader {

/* 常量 */

const int XMLReader::NONE = 0 ;

const int XMLReader::ELEMENT = 1 ;

const int XMLReader::ATTRIBUTE = 2 ;

const int XMLReader::TEXT = 3 ;

const int XMLReader::CDATA = 4 ;

const int XMLReader::ENTITY_REF = 5 ;

const int XMLReader::ENTITY = 6 ;

const int XMLReader::PI = 7 ;

const int XMLReader::COMMENT = 8 ;

const int XMLReader::DOC = 9 ;

const int XMLReader::DOC_TYPE = 10 ;

const int XMLReader::DOC_FRAGMENT = 11 ;

const int XMLReader::NOTATION = 12 ;

const int XMLReader::WHITESPACE = 13 ;

const int XMLReader::SIGNIFICANT_WHITESPACE = 14 ;

const int XMLReader::END_ELEMENT = 15 ;

const int XMLReader::END_ENTITY = 16 ;

const int XMLReader::XML_DECLARATION = 17 ;

const int XMLReader::LOADDTD = 1 ;

const int XMLReader::DEFAULTATTRS = 2 ;

const int XMLReader::VALIDATE = 3 ;

const int XMLReader::SUBST_ENTITIES = 4 ;

/* 属性 */

public readonly int $attributeCount ;

public readonly string $baseURI ;

public readonly int $depth ;

public readonly bool $hasAttributes ;

public readonly bool $hasValue ;

public readonly bool $isDefault ;

public readonly bool $isEmptyElement ;

public readonly string $localName ;

public readonly string $name ;

public readonly string $namespaceURI ;

public readonly int $nodeType ;

public readonly string $prefix ;

public readonly string $value ;

public readonly string $xmlLang ;

/* 方法 */

public bool close ( void )

public DOMNode expand ([ DOMNode $basenode ] )

public string getAttribute ( string $name )

public string getAttributeNo ( int $index )

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

public bool getParserProperty ( int $property )

public bool isValid ( void )

public string lookupNamespace ( string $prefix )

public bool moveToAttribute ( string $name )

public bool moveToAttributeNo ( int $index )

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

public bool moveToElement ( void )

public bool moveToFirstAttribute ( void )

public bool moveToNextAttribute ( void )

public bool next ([ string $localname ] )

public bool open ( string $URI [, string $encoding = NULL [, int $options = 0 ]] )

public bool read ( void )

public string readInnerXml ( void )

public string readOuterXml ( void )

public string readString ( void )

public bool setParserProperty ( int $property , bool $value )

public bool setRelaxNGSchema ( string $filename )

public bool setRelaxNGSchemaSource ( string $source )

public bool setSchema ( string $filename )

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

}

属性

attributeCount
The number of attributes on the node

baseURI
The base URI of the node

depth
Depth of the node in the tree, starting at 0

hasAttributes
Indicates if node has attributes

hasValue
Indicates if node has a text value

isDefault
Indicates if attribute is defaulted from DTD

isEmptyElement
Indicates if node is an empty element tag

localName
The local name of the node

name
The qualified name of the node

namespaceURI
The URI of the namespace associated with the node

nodeType
The node type for the node

prefix
The prefix of the namespace associated with the node

value
The text value of the node

xmlLang
The xml:lang scope which the node resides

预定义常量

XMLReader Node Types

XMLReader::NONE
No node type

XMLReader::ELEMENT
Start element

XMLReader::ATTRIBUTE
Attribute node

XMLReader::TEXT
Text node

XMLReader::CDATA
CDATA node

XMLReader::ENTITY_REF
Entity Reference node

XMLReader::ENTITY
Entity Declaration node

XMLReader::PI
Processing Instruction node

XMLReader::COMMENT
Comment node

XMLReader::DOC
Document node

XMLReader::DOC_TYPE
Document Type node

XMLReader::DOC_FRAGMENT
Document Fragment node

XMLReader::NOTATION
Notation node

XMLReader::WHITESPACE
Whitespace node

XMLReader::SIGNIFICANT_WHITESPACE
Significant Whitespace node

XMLReader::END_ELEMENT
End Element

XMLReader::END_ENTITY
End Entity

XMLReader::XML_DECLARATION
XML Declaration node

XMLReader Parser Options

XMLReader::LOADDTD
Load DTD but do not validate

XMLReader::DEFAULTATTRS
Load DTD and default attributes but do not validate

XMLReader::VALIDATE
Load DTD and validate while parsing

XMLReader::SUBST_ENTITIES
Substitute entities and expand references

XMLReader::close

Close the XMLReader input

说明

public bool XMLReader::close ( void )

Closes the input the XMLReader object is currently parsing.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::open
  • XMLReader::xml

XMLReader::expand

Returns a copy of the current node as a DOM object

说明

public DOMNode XMLReader::expand ([ DOMNode $basenode ] )

This method copies the current node and returns the appropriate DOM object.

参数

basenode
A DOMNode defining the target DOMDocument for the created DOM object.

返回值

The resulting DOMNode or FALSE on error.

更新日志

版本说明
5.3.0The parameter basenode was added.

XMLReader::getAttribute

Get the value of a named attribute

说明

public string XMLReader::getAttribute ( string $name )

Returns the value of a named attribute or NULL if the attribute does not exist or not positioned on an element node.

参数

name
The name of the attribute.

返回值

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

更新日志

版本说明
5.1.3Return NULL if no attribute found. Previously, returned an empty string.

参见

  • XMLReader::getAttributeNo
  • XMLReader::getAttributeNs

XMLReader::getAttributeNo

Get the value of an attribute by index

说明

public string XMLReader::getAttributeNo ( int $index )

Returns the value of an attribute based on its position or an empty string if attribute does not exist or not positioned on an element node.

参数

index
The position of the attribute.

返回值

The value of the attribute, or an empty string (before PHP 5.6) or NULL (from PHP 5.6 onwards) if no attribute exists at index or is not positioned on the element.

更新日志

版本说明
5.6.0XMLReader::getAttributeNo now returns NULL if the attribute doesn't exist.

参见

  • XMLReader::getAttribute
  • XMLReader::getAttributeNs

XMLReader::getAttributeNs

Get the value of an attribute by localname and URI

说明

public string XMLReader::getAttributeNs ( string $localName , string $namespaceURI )

Returns the value of an attribute by name and namespace URI or an empty string if attribute does not exist or not positioned on an element node.

参数

localName
The local name.

namespaceURI
The namespace URI.

返回值

The value of the attribute, or an empty string (before PHP 5.6) or NULL (from PHP 5.6 onwards) if no attribute with the given localName and namespaceURI is found or not positioned of element.

更新日志

版本说明
5.6.0XMLReader::getAttributeNS now returns NULL if the attribute doesn't exist.

参见

  • XMLReader::getAttribute
  • XMLReader::getAttributeNo

XMLReader::getParserProperty

Indicates if specified property has been set

说明

public bool XMLReader::getParserProperty ( int $property )

Indicates if specified property has been set.

参数

property
One of the parser option constants.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::setParserProperty

XMLReader::isValid

Indicates if the parsed document is valid

说明

public bool XMLReader::isValid ( void )

Returns a boolean indicating if the document being parsed is currently valid.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Validating XML

<?php
$xml = XMLReader::open('test.xml');

// The validate parser option must be enabled for 
// this method to work properly
$xml->setParserProperty(XMLReader::VALIDATE, true);

var_dump($xml->isValid());
?>

注释

Note: This checks the current node, not the entire document.

参见

  • XMLReader::setParserProperty
  • XMLReader::setRelaxNGSchema
  • XMLReader::setRelaxNGSchemaSource
  • XMLReader::setSchema

XMLReader::lookupNamespace

Lookup namespace for a prefix

说明

public string XMLReader::lookupNamespace ( string $prefix )

Lookup in scope namespace for a given prefix.

参数

prefix
String containing the prefix.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

XMLReader::moveToAttribute

Move cursor to a named attribute

说明

public bool XMLReader::moveToAttribute ( string $name )

Positions cursor on the named attribute.

参数

name
The name of the attribute.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::moveToElement
  • XMLReader::moveToAttributeNo
  • XMLReader::moveToAttributeNs
  • XMLReader::moveToFirstAttribute

XMLReader::moveToAttributeNo

Move cursor to an attribute by index

说明

public bool XMLReader::moveToAttributeNo ( int $index )

Positions cursor on attribute based on its position.

参数

index
The position of the attribute.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::moveToElement
  • XMLReader::moveToAttribute
  • XMLReader::moveToAttributeNs
  • XMLReader::moveToFirstAttribute

XMLReader::moveToAttributeNs

Move cursor to a named attribute

说明

public bool XMLReader::moveToAttributeNs ( string $localName , string $namespaceURI )

Positions cursor on the named attribute in specified namespace.

参数

localName
The local name.

namespaceURI
The namespace URI.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::moveToElement
  • XMLReader::moveToAttribute
  • XMLReader::moveToAttributeNo
  • XMLReader::moveToFirstAttribute

XMLReader::moveToElement

Position cursor on the parent Element of current Attribute

说明

public bool XMLReader::moveToElement ( void )

Moves cursor to the parent Element of current Attribute.

返回值

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

参见

  • XMLReader::moveToAttribute
  • XMLReader::moveToAttributeNo
  • XMLReader::moveToAttributeNs
  • XMLReader::moveToFirstAttribute

XMLReader::moveToFirstAttribute

Position cursor on the first Attribute

说明

public bool XMLReader::moveToFirstAttribute ( void )

Moves cursor to the first Attribute.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::moveToElement
  • XMLReader::moveToAttribute
  • XMLReader::moveToAttributeNo
  • XMLReader::moveToAttributeNs
  • XMLReader::moveToNextAttribute

XMLReader::moveToNextAttribute

Position cursor on the next Attribute

说明

public bool XMLReader::moveToNextAttribute ( void )

Moves cursor to the next Attribute if positioned on an Attribute or moves to first attribute if positioned on an Element.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::moveToElement
  • XMLReader::moveToAttribute
  • XMLReader::moveToAttributeNo
  • XMLReader::moveToAttributeNs
  • XMLReader::moveToFirstAttribute

XMLReader::next

Move cursor to next node skipping all subtrees

说明

public bool XMLReader::next ([ string $localname ] )

Positions cursor on the next node skipping all subtrees.

参数

localname
The name of the next node to move to.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::moveToNextAttribute
  • XMLReader::moveToElement
  • XMLReader::moveToAttribute

XMLReader::open

Set the URI containing the XML to parse

说明

public bool XMLReader::open ( string $URI [, string $encoding = NULL [, int $options = 0 ]] )

Set the URI containing the XML document to be parsed.

参数

URI
URI pointing to the document.

encoding
The document encoding or NULL.

options
A bitmask of the LIBXML_* constants.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE。 If called statically, returns an XMLReader 或者在失败时返回 FALSE.

错误/异常

此方法可以被静态调用,但会抛出一个 E_STRICT 错误。

更新日志

版本说明
5.2.0encoding and options were added.

参见

  • XMLReader::xml
  • XMLReader::close

XMLReader::read

Move to next node in document

说明

public bool XMLReader::read ( void )

Moves cursor to the next node in the document.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::moveToElement
  • XMLReader::moveToAttribute
  • XMLReader::next

XMLReader::readInnerXml

Retrieve XML from current node

说明

public string XMLReader::readInnerXml ( void )

Reads the contents of the current node, including child nodes and markup.

参数

此函数没有参数。

返回值

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

注释

Caution

此函数仅在 PHP 与 libxml 20620 或以上版本编译时可用。

参见

  • XMLReader::readString
  • XMLReader::readOuterXml
  • XMLReader::expand

XMLReader::readOuterXml

Retrieve XML from current node, including itself

说明

public string XMLReader::readOuterXml ( void )

Reads the contents of the current node, including the node itself.

参数

此函数没有参数。

返回值

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

注释

Caution

此函数仅在 PHP 与 libxml 20620 或以上版本编译时可用。

参见

  • XMLReader::readString
  • XMLReader::readInnerXml
  • XMLReader::expand

XMLReader::readString

Reads the contents of the current node as a string

说明

public string XMLReader::readString ( void )

Reads the contents of the current node as a string.

参数

此函数没有参数。

返回值

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

注释

Caution

此函数仅在 PHP 与 libxml 20620 或以上版本编译时可用。

参见

  • XMLReader::readOuterXml
  • XMLReader::readInnerXml
  • XMLReader::expand

XMLReader::setParserProperty

Set parser options

说明

public bool XMLReader::setParserProperty ( int $property , bool $value )

Set parser options. The options must be set after XMLReader::open or XMLReader::xml are called and before the first XMLReader::read call.

参数

property
One of the parser option constants.

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

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

XMLReader::setRelaxNGSchema

Set the filename or URI for a RelaxNG Schema

说明

public bool XMLReader::setRelaxNGSchema ( string $filename )

Set the filename or URI for the RelaxNG Schema to use for validation.

参数

filename
filename or URI pointing to a RelaxNG Schema.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::setRelaxNGSchemaSource
  • XMLReader::setSchema
  • XMLReader::isValid

XMLReader::setRelaxNGSchemaSource

Set the data containing a RelaxNG Schema

说明

public bool XMLReader::setRelaxNGSchemaSource ( string $source )

Set the data containing a RelaxNG Schema to use for validation.

参数

source
String containing the RelaxNG Schema.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XMLReader::setRelaxNGSchema
  • XMLReader::setSchema
  • XMLReader::isValid

XMLReader::setSchema

Validate document against XSD

说明

public bool XMLReader::setSchema ( string $filename )

Use W3C XSD schema to validate the document as it is processed. Activation is only possible before the first Read().

参数

filename
The filename of the XSD schema.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

错误/异常

Issues E_WARNING if libxml was built without schema support, the schema contains errors or if XMLReader::read has already been called.

注释

Caution

此函数仅在 PHP 与 libxml 20620 或以上版本编译时可用。

参见

  • XMLReader::setRelaxNGSchema
  • XMLReader::setRelaxNGSchemaSource
  • XMLReader::isValid

XMLReader::XML

Set the data containing the XML to parse

说明

public bool XMLReader::xml ( string $source [, string $encoding [, int $options = 0 ]] )

Set the data containing the XML to parse.

参数

source
String containing the XML to be parsed.

encoding
The document encoding or NULL.

options
A bitmask of the LIBXML_* constants.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE。 If called statically, returns an XMLReader 或者在失败时返回 FALSE.

错误/异常

此方法可以被静态调用,但会抛出一个 E_STRICT 错误。

更新日志

版本说明
5.2.0encoding and options were added.

参见

  • XMLReader::open
  • XMLReader::close