CLVIII. 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 in PHP 5.1 by default. It and can be enabled by adding the argument --with--xmlreader to your configure line. The libxml extension is required.

预定义类

XMLReader

方法

属性

表格 1.

NameTypeRead-onlyDescription
attributeCountintyesThe number of attributes on the node
baseURIstringyesThe base URI of the node
depthintyesDepth of the node in the tree starting at 0
hasAttributesboolyesIndicates if node has attributes
hasValueboolyesIndicates if node has a text value
isDefaultboolyesIndicates if attribute is defaulted from DTD
isEmptyElementboolyesIndicates if node is an empty element tag
localNamestringyesThe local name of the node
namestringyesThe qualified name of the node
namespaceURIstringyesThe URI of the namespace associated with the node
nodeTypeintyesThe node type for the node
prefixstringyesThe prefix of the namespace associated with the node
valuestringyesThe text value of the node
xmlLangstringyesThe xml:lang scope which the node resides

预定义常量

以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。

表格 2. XMLReader Node Types

ConstantValueDescription
XMLREADER_NONE (integer) 0No node type
XMLREADER_ELEMENT (integer) 1Start element
XMLREADER_ATTRIBUTE (integer) 2Attribute node
XMLREADER_TEXT (integer) 3Text node
XMLREADER_CDATA (integer) 4CDATA node
XMLREADER_ENTITY_REF (integer) 5Entity Reference node
XMLREADER_ENTITY (integer) 6Entity Declaration node
XMLREADER_PI (integer) 7Processing Instruction node
XMLREADER_COMMENT (integer) 8Comment node
XMLREADER_DOC (integer) 9Document node
XMLREADER_DOC_TYPE (integer) 10Document Type node
XMLREADER_DOC_FRAGMENT (integer) 11Document Fragment node
XMLREADER_NOTATION (integer) 12Notation node
XMLREADER_WHITESPACE (integer) 13Whitespace node
XMLREADER_SIGNIFICANT_WHITESPACE (integer) 14Significant Whitespace node
XMLREADER_END_ELEMENT (integer) 15End Element
XMLREADER_END_ENTITY (integer) 16End Entity
XMLREADER_XML_DECLARATION (integer) 17XML Declaration node

表格 3. XMLReader Parser Options

ConstantValueDescription
XMLREADER_LOADDTD (integer) 1Load DTD but do not validate
XMLREADER_DEFAULTATTRS (integer) 2Load DTD and default attributes but do not validate
XMLREADER_VALIDATE (integer) 3Load DTD and validate while parsing
XMLREADER_SUBST_ENTITIES (integer) 4Subsitute entities and expand references
目录
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->setParserProperty() -- Set or Unset parser options
XMLReader->setRelaxNGSchema() -- Set the filename or URI for a RelaxNG Schema
XMLReader->setRelaxNGSchemaSource() -- Set the data containing a RelaxNG Schema
XMLReader->XML() -- Set the data containing the XML to parse

add a note add a note User Contributed Notes
jcatalaa at catium dot com
21-Mar-2006 03: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::LOADDTDTRUE);
$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 08: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
12-Feb-2006 06: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";
}
?>