note 125427 added to class.simplexmliterator

From: Date: Sat, 10 Oct 2020 12:47:25 +0000
Subject: note 125427 added to class.simplexmliterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-215563@lists.php.net to get a copy of this message
<?php ======================================================= $index = <?xml version="1.0" encoding="UTF-8"?> <root> <article id="8" visibility="true" filename="2020-10-08" fileExtension="xml"> <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> <article id="7" visibility="true" filename="2020-10-07" fileExtension="xml"> <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> <article id="6" visibility="true" filename="2020-10-02" fileExtension="xml"> <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> <article id="5" visibility="true" filename="2020-09-30" fileExtension="xml"> <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> <article id="4" visibility="true" filename="2020-09-26" fileExtension="xml"> <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> <article id="3" visibility="true" filename="2020-09-22" fileExtension="xml"> <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> <article id="2" visibility="true" filename="2020-09-20" fileExtension="xml"> <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> <article id="1" visibility="true" filename="hello world" fileExtension="xml"> Hello World <tag>xml</tag> <tag>php</tag> <tag>experiment</tag> </article> </root> ==================================================================== ?> If you have to use an iterator to parse your XML and need tu get an attribute of tags of this iterator then use the fonction ->current() of simpleXMLIterator on your object before ->attributes()->{attibute name} <?php $file=""; try{ $index = new SimpleXMLIterator ( file_get_contents ( FILEDIRECTORY. 'index.xml' ) );} catch(Exception $e) {whatever you want to do on error}} for( $index->rewind(); $index->valid(); $index->next() ) { try { $file = file_get_contents(FILESDIRECTORY.$index->current()->attributes()->{'fileName'}. '.xml' ); } catch (Exception $e) {whatever you want to do on error} $article = new Article (); $article->setXMLArticle ($file); array_push( $this->articles, $article ); $file =""; } } ?> This exemple use a custom made Article object that itself parse the file given to him to initialise its properties . Here we open a file (Yes I use constant for my directories) make it a simpleXMLIterator and parse article elements to get the filename attribute to use it to open another XML file for data collection. don't forget !!! In PHP Object created by new ClassName() are ALWAYS given by reference that is why new Article() in inside the loop and not outside (yhea, I did that mistake) XMLIterator are powerfull yet harder to understand in themselves but once passed that one point. They are very more easier to use than plain arrays. ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.172 (proxied: 2a01:e34:ef49:8b50:adec:e470:8995:33c2) ---- Manual Page -- http://php.net/manual/en/class.simplexmliterator.php Edit -- https://master.php.net/note/edit/125427 Del: integrated -- https://master.php.net/note/delete/125427/integrated Del: useless -- https://master.php.net/note/delete/125427/useless Del: bad code -- https://master.php.net/note/delete/125427/bad+code Del: spam -- https://master.php.net/note/delete/125427/spam Del: non-english -- https://master.php.net/note/delete/125427/non-english Del: in docs -- https://master.php.net/note/delete/125427/in+docs Del: other reasons-- https://master.php.net/note/delete/125427 Reject -- https://master.php.net/note/reject/125427 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215563) next »