PHP: XMLReader functions - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

XMLReader->close() xmlrpc_set_type

Last updated: Fri, 26 Jan 2007

view this page in

# CLXXVII. XMLReader functions

# Введение

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.

# Установка

The XMLReader extension is available in PECL for PHP 5.0 and is included and enabled in PHP 5.1 by default. It can be enabled by adding the argument `--enable-xmlreader` (or `--with-xmlreader` before 5.1) to your configure line. The libxml extension is required.

# Предопределенные классы

## XMLReader

### Методы

- XMLReader->close() - Close the XMLReader input

- XMLReader->expand() - Export current node to a DOM node

- XMLReader->getAttribute() - Get value of attribute by name

- XMLReader->getAttributeNo() - Get value of attribute by position

- XMLReader->getAttributeNS() - Get value of attribute by name and URI

- XMLReader->getParserProperty() - Indicates if parser property is set or not

- XMLReader->isValid() - Indicates if document is valid

- XMLReader->lookupNamespace() - Get URI for prefix in scope of node

- XMLReader->moveToAttribute() - Positions reader on named attribute

- XMLReader->moveToAttributeNo() - Positions reader on attribute by index

- XMLReader->moveToAttributeNs() - Position reader on attribute by name and URI

- XMLReader->moveToElement() - Move to parent element of current attribute node

- XMLReader->moveToFirstAttribute() - Move to first attribute of node

- XMLReader->moveToNextAttribute() - Move to next attribute of node

- XMLReader->next() - Move to next element skipping children

- XMLReader->open() - Set URI to be parsed

- XMLReader->read() - Move to next node in stream

- XMLReader->setParserProperty() - Set parser property

- XMLReader->setRelaxNGSchema() - Set URI of RelaxNG schema to validate against

- XMLReader->setRelaxNGSchemaSource() - Set string containing RelaxNG schema to validate against

- XMLReader->XML() - Set string of data to be parsed

### Свойства

Таблица 1.

Name | Type | Read-only | Description
--- | --- | --- | ---
attributeCount | int | yes | The number of attributes on the node
baseURI | string | yes | The base URI of the node
depth | int | yes | Depth of the node in the tree starting at 0
hasAttributes | bool | yes | Indicates if node has attributes
hasValue | bool | yes | Indicates if node has a text value
isDefault | bool | yes | Indicates if attribute is defaulted from DTD
isEmptyElement | bool | yes | Indicates if node is an empty element tag
localName | string | yes | The local name of the node
name | string | yes | The qualified name of the node
namespaceURI | string | yes | The URI of the namespace associated with the node
nodeType | int | yes | The node type for the node
prefix | string | yes | The prefix of the namespace associated with the node
value | string | yes | The text value of the node
xmlLang | string | yes | The xml:lang scope which the node resides

# Предопределенные константы

Перечисленные ниже константы определены данным расширением и могут быть доступны только в том случае, если PHP был собран с поддержкой этого расширения или же в том случае, если данное расширение подгружается во время выполнения.

Внимание
XMLReader uses class constants since PHP 5.1. Prior releases use global constants in the form `XMLREADER_ELEMENT`.

Таблица 2. XMLReader Node Types

Constant | Value | Description
--- | --- | ---
`XMLReader::NONE` (integer) | 0 | No node type
`XMLReader::ELEMENT` (integer) | 1 | Start element
`XMLReader::ATTRIBUTE` (integer) | 2 | Attribute node
`XMLReader::TEXT` (integer) | 3 | Text node
`XMLReader::CDATA` (integer) | 4 | CDATA node
`XMLReader::ENTITY_REF` (integer) | 5 | Entity Reference node
`XMLReader::ENTITY` (integer) | 6 | Entity Declaration node
`XMLReader::PI` (integer) | 7 | Processing Instruction node
`XMLReader::COMMENT` (integer) | 8 | Comment node
`XMLReader::DOC` (integer) | 9 | Document node
`XMLReader::DOC_TYPE` (integer) | 10 | Document Type node
`XMLReader::DOC_FRAGMENT` (integer) | 11 | Document Fragment node
`XMLReader::NOTATION` (integer) | 12 | Notation node
`XMLReader::WHITESPACE` (integer) | 13 | Whitespace node
`XMLReader::SIGNIFICANT_WHITESPACE` (integer) | 14 | Significant Whitespace node
`XMLReader::END_ELEMENT` (integer) | 15 | End Element
`XMLReader::END_ENTITY` (integer) | 16 | End Entity
`XMLReader::XML_DECLARATION` (integer) | 17 | XML Declaration node

Таблица 3. XMLReader Parser Options

Constant | Value | Description
--- | --- | ---
`XMLReader::LOADDTD` (integer) | 1 | Load DTD but do not validate
`XMLReader::DEFAULTATTRS` (integer) | 2 | Load DTD and default attributes but do not validate
`XMLReader::VALIDATE` (integer) | 3 | Load DTD and validate while parsing
`XMLReader::SUBST_ENTITIES` (integer) | 4 | Substitute entities and expand references

add a note User Contributed Notes
XMLReader functions
jamespic at gmail dot nsopam com
14-Nov-2006 08:09
`Example, as requested, with nested nodes.
<?php
ob_start();
?>
<root>
<folder>
<name>folder A</name>
<files>
<file>
<name>Afile 1</name>
</file>
<file>
<name>Afile 2</name>
</file>
</files>
</folder>
<folder>
<name>folder B</name>
<files>
<file>
<name>Bfile 1</name>
</file>
<file>
<name>Bfile 2</name>
</file>
</files>
</folder>
</root>
<?php
$xmldata = ob_get_contents();
ob_end_clean();
$xml = new XMLReader();
$xml->XML($xmldata);
$data = array();
while ($xml->read())
{
while($xml->depth<=2 && $xml->nodeType==1)
$xml->read();
if ($xml->nodeType==3 && $xml->depth==3) // NodeType 3 : Text Element
{
$strFolderName = $xml->value;
$data[$strFolderName]=array();
while($xml->depth<=3)
$xml->read();
while($xml->depth>=3)
{
//xdump();
if ($xml->nodeType==3)
$data[$strFolderName][] = $xml->value;
$xml->read();
}
}
}
print_r($data);
echo "\n";
?>
Output :
Array
(
[folder A] => Array
(
[0] => Afile 1
[1] => Afile 2
)
[folder B] => Array
(
[0] => Bfile 1
[1] => Bfile 2
)
)`
jcatalaa at catium dot com
20-Mar-2006 11:52
`DTD Validation
Parser properties can be set using:
$xml_reader->setParserProperty(XMLReader::CONSTANT_NAME, BoolenValue);
The constant setting in the xmlreader_validatedtd.php example that comes
with the xmlread package results in an error.
Here is how I got it to work...
<?php
$indent = 5; /* Number of spaces to indent per level */
$xml = new XMLReader();
$xml->open("dtdexample.xml");
// CHANGED NEXT TWO LINES TO REMOVE ERROR
// FROM: $xml->setParserProperty(XMLREADER_LOADDTD, TRUE);
$xml->setParserProperty(XMLReader::LOADDTD, TRUE);
$xml->setParserProperty(XMLReader::VALIDATE, TRUE);
while($xml->read()) {
/* Print node name indenting it based on depth and $indent var */
print str_repeat(" ", $xml->depth * $indent).$xml->name."\n";
if ($xml->hasAttributes) {
$attCount = $xml->attributeCount;
print str_repeat(" ", $xml->depth * $indent)." Number of Attributes: ".$xml->attributeCount."\n";
}
}
print "\n\nValid:\n";
var_dump($xml->isValid());
?>`
orion at ftf-hq dot dk
15-Feb-2006 04:50
`Some more documentation (i.e. examples) would be nice :-)
This is how I read some mysql parameters in an xml file:
<?php
$xml = new XMLReader();
$xml->open("config.xml");
$xml->setParserProperty(2,true); // This seems a little unclear to me - but it worked :)
while ($xml->read()) {
switch ($xml->name) {
case "mysql_host":
$xml->read();
$conf["mysql_host"] = $xml->value;
$xml->read();
break;
case "mysql_username":
$xml->read();
$conf["mysql_user"] = $xml->value;
$xml->read();
break;
case "mysql_password":
$xml->read();
$conf["mysql_pass"] = $xml->value;
$xml->read();
break;
case "mysql_database":
$xml->read();
$conf["mysql_db"] = $xml->value;
$xml->read();
break;
}
}
$xml->close();
?>
The XML file used:
<?xml version='1.0'?>
<MySQL_INIT>
<mysql_host>localhost</mysql_host>
<mysql_database>db_database</mysql_database>
<mysql_username>root</mysql_username>
<mysql_password>password</mysql_password>
</MySQL_INIT>`
Ariel Gonzalez
11-Feb-2006 02:09
`Simple function I used while playing around with XMLReader.
<?php
function dump_xmlreader($o) {
$node_types = array (
0=>"No node type",
1=>"Start element",
2=>"Attribute node",
3=>"Text node",
4=>"CDATA node",
5=>"Entity Reference node",
6=>"Entity Declaration node",
7=>"Processing Instruction node",
8=>"Comment node",
9=>"Document node",
10=>"Document Type node",
11=>"Document Fragment node",
12=>"Notation node",
13=>"Whitespace node",
14=>"Significant Whitespace node",
15=>"End Element",
16=>"End Entity",
17=>"XML Declaration node"
);
echo "attributeCount = " . $o->attributeCount . "\n";
echo "baseURI = " . $o->baseURI . "\n";
echo "depth = " . $o->depth . "\n";
echo "hasAttributes = " . ( $o->hasAttributes ? 'TRUE' : 'FALSE' ) . "\n";
echo "hasValue = " . ( $o->hasValue ? 'TRUE' : 'FALSE' ) . "\n";
echo "isDefault = " . ( $o->isDefault ? 'TRUE' : 'FALSE' ) . "\n";
echo "isEmptyElement = " . ( @$o->isEmptyElement ? 'TRUE' : 'FALSE' ) . "\n";
echo "localName = " . $o->localName . "\n";
echo "name = " . $o->name . "\n";
echo "namespaceURI = " . $o->namespaceURI . "\n";
echo "nodeType = " . $o->nodeType . ' - ' . $node_types[$o->nodeType] . "\n";
echo "prefix = " . $o->prefix . "\n";
echo "value = " . $o->value . "\n";
echo "xmlLang = " . $o->xmlLang . "\n";
}
?>`
add a note
XMLReader->close() xmlrpc_set_type

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 11:33:19 2007 PST