<?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.