|
XMLReader functionsThe 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. It is important to note that internally, libxml uses the UTF-8 encoding and as such, the encoding of the retrieved contents will always be in UTF-8 encoding.
The XMLReader extension is available in PECL as of PHP 5.0.0 and is
included and enabled as of PHP 5.1.0 by default. It can be enabled
by adding the argument
Table 339.
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
Warning:
XMLReader uses class constants since PHP 5.1. Prior releases use global
constants in the form Table 340. XMLReader Node Types
Table 341. XMLReader Parser Options
Table of Contents
Code Examples / Notes » ref.xmlreaderorion
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
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"; } ?> usedsk
I found it a little hard to parse nested elements, so wrote a function simplifies it (based off http://www.thescripts.com/forum/thread627281.html): <?php function read_mixed_xml($filename, $arrayBeginElem, $arrayEndElem) { $output = ""; $arrayBeginKeys = array_keys($arrayBeginElem); $lengthBegin = count($arrayBeginElem); // Length of the begin array $arrayEndKeys = array_keys($arrayEndElem); $lengthEnd = count($arrayEndElem); // Length of end element array $xmlReader = new XMLReader(); $xmlReader->open($filename); $xmlReader->read(); // Skip root node /* Go through the nodes */ while($xmlReader->read()) { /* We're only parsing begin and #text nodes right now */ if($xmlReader->nodeType != XMLReader::END_ELEMENT) { switch($xmlReader->nodeType) { /* If the current node is a begin element, go through the array of begin elements, in search of the current node's name. If it is, append $arrayBeginElem's value for the current node's name to the $output. (Simulates case "paragraph": $output .= " " break; ) */ case XMLReader::ELEMENT: for($i = 0; $i < $lengthBegin; $i++) { $key = $arrayBeginKeys[i]; if($key==$xmlReader->name) { $output .= $arrayBeginElem[$key]; } } break; /* If the current node is a #text node, append the node's value to $output */ case XMLReader::TEXT: $output .= $xmlReader->value; break; } } /* If the current node is an end element, go through the array of end elements, and search for the current node's name. If found, append $arrayEndElem's value for the current node's name to the output */ else if($xmlReader->nodeType == XMLReader::END_ELEMENT) { for($i = 0; $i < $lengthEnd; $i++) { $key = $arrayEndKeys[i]; if($key==$xmlReader->name) { $output .= $arrayEndElem[$key]; } } } } $xmlReader->close(); return $output; } Example input: $begin = array("title" => " <h1>", "paragraph" => " ", "italicized" => "<i>"); $end = array("title" => "</h1>", "paragraph" => "", "italicized" => "</i>"); $content = read_mixed_xml("index.xml", $begin, $end); echo $content; ?> index.xml: <?xml version="1.0"?> <body> <title>Introduction</title> <paragraph> Lorem <italicized>ipsum dolor sit amet</italicized>, consectetuer adipiscing elit. Donec neque augue, nonummy sit amet, interdum vitae, egestas a, nulla. Aenean sed turpis eget lacus venenatis tincidunt. Integer in leo vitae est euismod congue. Curabitur quis tellus ut nulla pharetra fringilla. Phasellus id risus sagittis turpis lobortis pretium. </paragraph> <paragraph> Curabitur ultrices pulvinar massa. Nullam ac massa. Morbi adipiscing pharetra est. In non neque vitae massa adipiscing vestibulum. Integer congue, lacus non sagittis consectetuer, magna nisl eleifend nisl, id fringilla justo justo et arcu. </paragraph> </body> Example output: <h1>Introduction</h1> Lorem <i>ipsum dolor sit amet</i>, consectetuer adipiscing elit. Donec neque augue, nonummy sit amet, interdum vitae, egestas a, nulla. Aenean sed turpis eget lacus venenatis tincidunt. Integer in leo vitae est euismod congue. Curabitur quis tellus ut nulla pharetra fringilla. Phasellus id risus sagittis turpis lobortis pretium. Curabitur ultrices pulvinar massa. Nullam ac massa. Morbi adipiscing pharetra est. In non neque vitae massa adipiscing vestibulum. Integer congue, lacus non sagittis consectetuer, magna nisl eleifend nisl, id fringilla justo justo et arcu. jamespic
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
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()); ?> |
Change Language.NET Functions Apache-specific Functions Alternative PHP Cache Advanced PHP debugger Array Functions Aspell functions [deprecated] BBCode Functions BCMath Arbitrary Precision Mathematics Functions PHP bytecode Compiler Bzip2 Compression Functions Calendar Functions CCVS API Functions [deprecated] Class/Object Functions Classkit Functions ClibPDF Functions [deprecated] COM and .Net (Windows) Crack Functions Character Type Functions CURL Cybercash Payment Functions Credit Mutuel CyberMUT functions Cyrus IMAP administration Functions Date and Time Functions DB++ Functions Database (dbm-style) Abstraction Layer Functions dBase Functions DBM Functions [deprecated] dbx Functions Direct IO Functions Directory Functions DOM Functions DOM XML Functions enchant Functions Error Handling and Logging Functions Exif Functions Expect Functions File Alteration Monitor Functions Forms Data Format Functions Fileinfo Functions filePro Functions Filesystem Functions Filter Functions Firebird/InterBase Functions Firebird/Interbase Functions (PDO_FIREBIRD) FriBiDi Functions FrontBase Functions FTP Functions Function Handling Functions GeoIP Functions Gettext Functions GMP Functions gnupg Functions Net_Gopher Haru PDF Functions hash Functions HTTP Hyperwave Functions Hyperwave API Functions i18n Functions IBM Functions (PDO_IBM) IBM DB2 iconv Functions ID3 Functions IIS Administration Functions Image Functions Imagick Image Library IMAP Informix Functions Informix Functions (PDO_INFORMIX) Ingres II Functions IRC Gateway Functions PHP / Java Integration JSON Functions KADM5 LDAP Functions libxml Functions Lotus Notes Functions LZF Functions Mail Functions Mailparse Functions Mathematical Functions MaxDB PHP Extension MCAL Functions Mcrypt Encryption Functions MCVE (Monetra) Payment Functions Memcache Functions Mhash Functions Mimetype Functions Ming functions for Flash Miscellaneous Functions mnoGoSearch Functions Microsoft SQL Server Functions Microsoft SQL Server and Sybase Functions (PDO_DBLIB) Mohawk Software Session Handler Functions mSQL Functions Multibyte String Functions muscat Functions MySQL Functions MySQL Functions (PDO_MYSQL) MySQL Improved Extension Ncurses Terminal Screen Control Functions Network Functions Newt Functions NSAPI-specific Functions Object Aggregation/Composition Functions Object property and method call overloading Oracle Functions ODBC Functions (Unified) ODBC and DB2 Functions (PDO_ODBC) oggvorbis OpenAL Audio Bindings OpenSSL Functions Oracle Functions [deprecated] Oracle Functions (PDO_OCI) Output Control Functions Ovrimos SQL Functions Paradox File Access Parsekit Functions Process Control Functions Regular Expression Functions (Perl-Compatible) PDF Functions PDO Functions Phar archive stream and classes PHP Options&Information POSIX Functions Regular Expression Functions (POSIX Extended) PostgreSQL Functions PostgreSQL Functions (PDO_PGSQL) Printer Functions Program Execution Functions PostScript document creation Pspell Functions qtdom Functions Radius Rar Functions GNU Readline GNU Recode Functions RPM Header Reading Functions runkit Functions SAM - Simple Asynchronous Messaging Satellite CORBA client extension [deprecated] SCA Functions SDO Functions SDO XML Data Access Service Functions SDO Relational Data Access Service Functions Semaphore SESAM Database Functions PostgreSQL Session Save Handler Session Handling Functions Shared Memory Functions SimpleXML functions SNMP Functions SOAP Functions Socket Functions Standard PHP Library (SPL) Functions SQLite Functions SQLite Functions (PDO_SQLITE) Secure Shell2 Functions Statistics Functions Stream Functions String Functions Subversion Functions Shockwave Flash Functions Swish Functions Sybase Functions TCP Wrappers Functions Tidy Functions Tokenizer Functions Unicode Functions URL Functions Variable Handling Functions Verisign Payflow Pro Functions vpopmail Functions W32api Functions WDDX Functions win32ps Functions win32service Functions xattr Functions xdiff Functions XML Parser Functions XML-RPC Functions XMLReader functions XMLWriter Functions XSL functions XSLT Functions YAZ Functions YP/NIS Functions Zip File Functions Zlib Compression Functions |