2010-05-16 8 views
0

J'ai un fichier XML comme ceci:PHP SimpleXML et XPath

<?xml version="1.0" encoding="UTF-8"?> 
<gallery> 
    <album tnPath="tn/" lgPath="imm/" fsPath="iml/" > 
     <img src="001.jpg" /> 
     <img src="002.jpg" /> 
    </album> 
</gallery> 

Je lis le fichier avec:

$xmlFile = "xml.xml"; 
$xmlStr = file_get_contents($xmlFile . "?" . time()); 
$xmlObj = simplexml_load_string($xmlStr); 

Maintenant, je suis la reconstruction du fichier XML et souhaitez enregistrer le album noeud avec ses attributs dans un var je pensais avec XPath:

// these all return arrays with the images... 
// echo $xmlObj->xpath('/gallery/[email protected]'); 
// echo $xmlObj->xpath('//album[@tnPath]'); 
// echo $xmlObj->xpath('//@tnPath'); 

Mais cela ne semble pas fonctionner? De l'aide?

+0

Le '"? " . time() 'stuff n'est pas nécessaire. PHP n'a pas de cache lors de l'extraction des ressources distantes (en supposant qu'il soit distant, sinon il serait encore plus inutile ou même casser l'ouverture du fichier) – ThiefMaster

+0

Oh vraiment? Merci pour la pointe .. – FFish

Répondre

0

$xmlObj->xpath('/gallery/album') donne le (s) noeud (s) de l'album.

+0

oui, mais je veux les attributs du nœud .. – FFish

+0

Ok, je l'ai eu en travaillant avec $ foo = $ xmlObj-> xpath ('// @ tnPath'); echo $ foo [0]; – FFish

Questions connexes