Book/xmlreader-Phpdoc专题
XMLReader
目录
- 简介
- 安装/配置
- XMLReader — The XMLReader class
- XMLReader::close — Close the XMLReader input
- XMLReader::expand — Returns a copy of the current node as a DOM object
- XMLReader::getAttribute — Get the value of a named attribute
- XMLReader::getAttributeNo — Get the value of an attribute by index
- XMLReader::getAttributeNs — Get the value of an attribute by localname and URI
- XMLReader::getParserProperty — Indicates if specified property has been set
- XMLReader::isValid — Indicates if the parsed document is valid
- XMLReader::lookupNamespace — Lookup namespace for a prefix
- XMLReader::moveToAttribute — Move cursor to a named attribute
- XMLReader::moveToAttributeNo — Move cursor to an attribute by index
- XMLReader::moveToAttributeNs — Move cursor to a named attribute
- XMLReader::moveToElement — Position cursor on the parent Element of current Attribute
- XMLReader::moveToFirstAttribute — Position cursor on the first Attribute
- XMLReader::moveToNextAttribute — Position cursor on the next Attribute
- XMLReader::next — Move cursor to next node skipping all subtrees
- XMLReader::open — Set the URI containing the XML to parse
- XMLReader::read — Move to next node in document
- XMLReader::readInnerXml — Retrieve XML from current node
- XMLReader::readOuterXml — Retrieve XML from current node, including itself
- XMLReader::readString — Reads the contents of the current node as a string
- XMLReader::setParserProperty — Set parser options
- XMLReader::setRelaxNGSchema — Set the filename or URI for a RelaxNG Schema
- XMLReader::setRelaxNGSchemaSource — Set the data containing a RelaxNG Schema
- XMLReader::setSchema — Validate document against XSD
- XMLReader::XML — Set the data containing the XML to parse
简介
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 <span
class="modifier">readonly int
$attributeCount
;
public <span
class="modifier">readonly string
$baseURI
;
public <span
class="modifier">readonly int $depth
;
public <span
class="modifier">readonly bool
$hasAttributes
;
public <span
class="modifier">readonly bool
$hasValue
;
public <span
class="modifier">readonly bool
$isDefault
;
public <span
class="modifier">readonly bool
$isEmptyElement
;
public <span
class="modifier">readonly string
$localName
;
public <span
class="modifier">readonly string
$name
;
public <span
class="modifier">readonly string
$namespaceURI
;
public <span
class="modifier">readonly int
$nodeType
;
public <span
class="modifier">readonly string
$prefix
;
public <span
class="modifier">readonly string
$value
;
public <span
class="modifier">readonly string
$xmlLang
;
/* 方法 */
public bool close ( <span class="methodparam">void )
public <span
class="type">DOMNodefalse <span
class="methodname">expand ([ <span
class="type">DOMNode<span
class="type">null $baseNode
=
null
] )
public <span
class="type">stringnull <span
class="methodname">getAttribute ( <span
class="type">string $name
)
public <span
class="type">stringnull <span
class="methodname">getAttributeNo ( <span
class="methodparam">int $index
)
public <span
class="type">stringnull <span
class="methodname">getAttributeNs ( <span
class="methodparam">string $name
,
string
$namespace
)
public bool
getParserProperty ( <span
class="methodparam">int $property
)
public bool isValid ( <span class="methodparam">void )
public <span
class="type">stringnull <span
class="methodname">lookupNamespace ( <span
class="methodparam">string $prefix
)
public bool
moveToAttribute ( <span
class="methodparam">string $name
)
public bool
moveToAttributeNo ( <span
class="methodparam">int $index
)
public bool
moveToAttributeNs ( <span
class="methodparam">string $name
,
string
$namespace
)
public bool moveToElement ( <span class="methodparam">void )
public bool moveToFirstAttribute ( <span class="methodparam">void )
public bool moveToNextAttribute ( <span class="methodparam">void )
public bool
next ([ <span
class="type">string<span
class="type">null $name
=
null
] )
public <span
class="modifier">static <span
class="type">boolXMLReader <span
class="methodname">open ( <span
class="type">string $uri
[, <span
class="methodparam"><span
class="type">stringnull
$encoding
= null
[,
int $flags
<span
class="initializer"> = 0 ]] )
public bool read ( <span class="methodparam">void )
public string readInnerXml ( <span class="methodparam">void )
public string readOuterXml ( <span class="methodparam">void )
public string readString ( <span class="methodparam">void )
public bool
setParserProperty ( <span
class="methodparam">int $property
,
bool $value
)
public bool
setRelaxNGSchema ( <span
class="methodparam"><span
class="type">stringnull
$filename
)
public bool
setRelaxNGSchemaSource ( <span
class="methodparam"><span
class="type">stringnull
$source
)
public bool
setSchema ( <span
class="methodparam"><span
class="type">stringnull
$filename
)
public <span
class="modifier">static <span
class="type">boolXMLReader <span
class="methodname">XML ( <span
class="type">string $source
[, <span
class="methodparam"><span
class="type">stringnull
$encoding
= null
[,
int $flags
<span
class="initializer"> = 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 ( <span class="methodparam">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 <span
class="type">DOMNodefalse <span
class="methodname">XMLReader::expand ([ <span
class="methodparam"><span
class="type">DOMNodenull
$baseNode
= null
] )
This method copies the current node and returns the appropriate DOM object.
参数
baseNode
A DOMNode defining the target <span
class="classname">DOMDocument for the created DOM object.
返回值
The resulting DOMNode or false
on
error.
XMLReader::getAttribute
Get the value of a named attribute
说明
public <span
class="type">stringnull <span
class="methodname">XMLReader::getAttribute ( <span
class="methodparam">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.
更新日志
版本 | 说明 |
---|---|
8.0.0 | This function can no longer return false . |
返回值
The value of the attribute, or null
if no attribute with the given
name
is found or not positioned on an element node.
参见
- XMLReader::getAttributeNo
- XMLReader::getAttributeNs
XMLReader::getAttributeNo
Get the value of an attribute by index
说明
public <span
class="type">stringnull <span
class="methodname">XMLReader::getAttributeNo ( <span
class="methodparam">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.0 | XMLReader::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 <span
class="type">stringnull <span
class="methodname">XMLReader::getAttributeNs ( <span
class="methodparam">string $name
,
string
$namespace
)
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.
参数
name
The local name.
namespace
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 name
and namespace
is found or not positioned of element.
更新日志
版本 | 说明 |
---|---|
8.0.0 | This function can no longer return false . |
5.6.0 | XMLReader::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 ( <span
class="methodparam">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 ( <span class="methodparam">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 <span
class="type">stringnull <span
class="methodname">XMLReader::lookupNamespace ( <span
class="methodparam">string $prefix
)
Lookup in scope namespace for a given prefix.
参数
prefix
String containing the prefix.
返回值
The value of the namespace, or null
if no namespace exists.
更新日志
版本 | 说明 |
---|---|
8.0.0 | This function can no longer return false . |
XMLReader::moveToAttribute
Move cursor to a named attribute
说明
public bool
XMLReader::moveToAttribute ( <span
class="methodparam">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 ( <span
class="methodparam">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 ( <span
class="methodparam">string $name
,
string
$namespace
)
Positions cursor on the named attribute in specified namespace.
参数
name
The local name.
namespace
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 ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 ([ <span
class="methodparam"><span
class="type">stringnull
$name
= null
] )
Positions cursor on the next node skipping all subtrees.
参数
name
The name of the next node to move to.
返回值
成功时返回 true
, 或者在失败时返回 false
。
更新日志
版本 | 说明 |
---|---|
8.0.0 | name is nullable now. |
参见
- XMLReader::moveToNextAttribute
- XMLReader::moveToElement
- XMLReader::moveToAttribute
XMLReader::open
Set the URI containing the XML to parse
说明
public <span
class="modifier">static <span
class="type">boolXMLReader <span
class="methodname">XMLReader::open ( <span
class="methodparam">string $uri
[,
<span
class="type">stringnull
$encoding
= null
[,
int $flags
<span
class="initializer"> = 0 ]] )
Set the URI containing the XML document to be parsed.
参数
uri
URI pointing to the document.
encoding
The document encoding or null
.
flags
A bitmask of the
LIBXML_* constants.
返回值
成功时返回 true
, 或者在失败时返回 false
。 If called
statically, returns an XMLReader
或者在失败时返回 false
.
错误/异常
This method may be called statically, but prior to PHP 8.0.0, will issue
an E_DEPRECATED
error in this case.
更新日志
版本 | 说明 |
---|---|
8.0.0 | XMLReader::open is now declared as static method, but can still be called on an XMLReader instance. |
参见
- XMLReader::xml
- XMLReader::close
XMLReader::read
Move to next node in document
说明
public bool XMLReader::read ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span class="methodparam">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 ( <span
class="methodparam">int $property
,
bool $value
)
Set parser options. The options must be set after <span class="methodname">XMLReader::open or <span class="methodname">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 ( <span
class="methodparam"><span
class="type">stringnull
$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 (
<span
class="type">stringnull
$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 ( <span
class="methodparam"><span
class="type">stringnull
$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 <span
class="function">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 <span
class="modifier">static <span
class="type">boolXMLReader <span
class="methodname">XMLReader::XML ( <span
class="methodparam">string $source
[, <span
class="type">stringnull
$encoding
= null
[,
int $flags
<span
class="initializer"> = 0 ]] )
Set the data containing the XML to parse.
参数
source
String containing the XML to be parsed.
encoding
The document encoding or null
.
flags
A bitmask of the
LIBXML_* constants.
返回值
成功时返回 true
, 或者在失败时返回 false
。 If called
statically, returns an XMLReader
或者在失败时返回 false
.
错误/异常
This method may be called statically, but prior to PHP 8.0.0, will issue
an E_DEPRECATED
error in this case.
更新日志
版本 | 说明 |
---|---|
8.0.0 | XMLReader::XML is now declared as static method, but can still be called on an XMLReader instance. |
参见
- XMLReader::open
- XMLReader::close