Précédent   Forum des professionnels en informatique > PHP > Bibliothèques et frameworks > XML
XML Forum d'entraide sur XML avec PHP. Exemples : SimpleXML, OpenXML... Avant de poster -> FAQ XML, Cours XML et Sources XML
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 05/08/2011, 10h59   #1
Membre éprouvé
 
Avatar de gtraxx
 
Homme Aurélien Gérits
Développeur Web
Inscription : mai 2006
Messages : 1 045
Détails du profil
Informations personnelles :
Nom : Homme Aurélien Gérits
Âge : 30
Localisation : Belgique

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : mai 2006
Messages : 1 045
Points : 498
Points : 498
Par défaut 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
__________________
Au petit déjeuner, je prend du PHP et au souper du jQuery.
AFUP 2009 : Bien optimiser son code pour le référencement
Mon projet open source de librairie PHP 5: magix cjquery
gtraxx est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/08/2011, 22h43   #2
Expert Confirmé
 
Avatar de Séb.
 
Inscription : mars 2005
Messages : 2 823
Détails du profil
Informations personnelles :
Âge : 34
Localisation : France

Informations professionnelles :
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : mars 2005
Messages : 2 823
Points : 3 454
Points : 3 454
Code :
foreach($xml->author as $row) {
Là tu tentes d'itérer sur les éléments <author> de la racine <config>.

Il faudrait que tu fasses :

Code :
foreach ( $xml->acl->admin->infos->authors->author as $row ) {
__________________
Un problème exposé clairement est déjà à moitié résolu
Keep It Smart and Simple
Séb. est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 10h49.


 
 
 
 
Partenaires

Hébergement Web