[XML] parser un fichier xml avec simplexml
Bonjour, je souhaite retourner tous les éléments d'un fichier xml et les afficher d'une manière structuré.
J'ai tenter plusieurs chose pour arriver à mes fins mais sans succès.
Je coince sur l'itération d'un élément dans un noeud.
J'ai d'abord essayer avec xmlreader + simplexml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
$xml->open($pathxml, "UTF-8");
while($xml->read()){
if ($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == "authors") {
$v = $xml->expand();
$v = new SimpleXMLElement('<authors>'.$xml->readInnerXML().'</authors>');
$r = '<table class="table-plugin-author">
<thead>
<tr style="padding:3px;" class="ui-widget ui-widget-header">
<th>Author</th>
<th>Website</th>
</tr>
</thead>
<tbody>';
foreach($v->author as $row){
$r.= '<tr>';
$r.= '<td class="medium-cell">'.$row->name.'</td>';
$r .= '<td><ul>';
$t = '';
foreach($row->link as $link){
$t .= '<li>'.$link->url.'</li>';
}
$r .= $t;
$r.='</ul></td>';
$r.= '</tr>';
}
$r.='</tbody></table>';
return $r;
}
} |
Ceci ne passe pas :
Code:
1 2 3 4
|
foreach($row->link as $link){
$t .= '<li>'.$link->url.'</li>';
} |
J'ai alors tenter uniquement avec simplexml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
$xml = simplexml_load_file($pathxml);
$r = '<table class="table-plugin-author">
<thead>
<tr style="padding:3px;" class="ui-widget ui-widget-header">
<th>Author</th>
<th>Website</th>
</tr>
</thead>
<tbody>';
foreach($xml->author as $row) {
$r.= '<tr>';
$r.= '<td class="medium-cell">'.$row->name.'</td>';
$r .= '<td><ul>';
$t = '';
foreach($row->link as $link){
$t .= '<li>'.$link->url.'</li>';
}
$r .= $t;
$r.='</ul></td>';
$r.= '</tr>';
}
$r.='</tbody></table>';
return $r; |
Idem j'ai pas ce que je veux mon fichier xml ressemble à ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<acl>
<admin>
<authorized>
<allow_access>*</allow_access>
</authorized>
<infos>
<authors>
<author>
<name>mon super nom</name>
<link>
<url>http://www.test.com</url>
<url>http://www.test.be</url>
</link>
</author>
<author>
<name>Bobby</name>
<link>
<url>http://www.montruc.be</url>
</link>
</author>
</authors>
</infos>
</admin>
</acl>
</config> |
Je m'y prend probablement comme un manche :oops: