Question

J'ai la structure XML suivante:

<?xml version="1.0" ?>
<course xml:lang="nl">
  <body>
    <item id="787900813228567" view="12000" title="0x|Beschrijving" engtitle="0x|Description"><![CDATA[Dit college leert studenten hoe ze een onderzoek kunn$
    <item id="5453116633894965" view="12000" title="0x|Onderwijsvorm" engtitle="0x|Method of instruction"><![CDATA[instructiecollege]]></item>
    <item id="7433550075448316" view="12000" title="0x|Toetsing" engtitle="0x|Examination"><![CDATA[Opdrachten/werkstuk]]></item>
    <item id="015071401858970545" view="12000" title="0x|Literatuur" engtitle="0x|Required reading"><![CDATA[Wayne C. Booth, Gregory G. Colomb, Joseph M. Wi$
    <item id="5960589172957031" view="12000" title="0x|Uitbreiding" engtitle="0x|Expansion"><![CDATA[]]></item>
    <item id="3610066867901779" view="12000" title="0x|Aansluiting" engtitle="0x|Place in study program"><![CDATA[]]></item>
    <item id="19232369892482925" view="12000" title="0x|Toegangseisen" engtitle="0x|Course requirements"><![CDATA[]]></item>
    <item id="3332396346891524" view="12000" title="0x|Doelgroep" engtitle="0x|Target audience"><![CDATA[]]></item>
    <item id="6606851872934866" view="12000" title="0x|Aanmelden bij" engtitle="0x|Enrollment at"><![CDATA[]]></item>
    <item id="1478643580820973" view="12000" title="0x|Informatie bij" engtitle="0x|Information at"><![CDATA[Docent]]></item>
    <item id="9710608434763993" view="12000" title="0x|Rooster" engtitle="0x|Schedule"><![CDATA[1e semester, maandag 15.00-17.00, zaal 1175/030]]></item>
  </body>
</course>

Je souhaite obtenir les données d'une des balises d'élément. Pour accéder à cette balise, j'utilise le xpath suivant:

$description = $xml->xpath("//item[@title='0x|Beschrijving']");

Ceci renvoie en effet un tableau sous la forme de:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 787900813228567
                    [view] => 12000
                    [title] => 0x|Beschrijving
                    [engtitle] => 0x|Description
                )
        )
)

Mais où se trouvent les informations réelles (stockées entre les balises d’article)? Je dois faire quelque chose de mal, mais je ne peux pas comprendre ce que cela pourrait être ... Probablement quelque chose de très simple ... Une aide serait appréciée.

Était-ce utile?

La solution

Lorsque vous chargez le fichier XML, vous devez gérer le CDATA. Cet exemple fonctionne:

<?php
$xml = simplexml_load_file('file.xml', NULL, LIBXML_NOCDATA);
$description = $xml->xpath("//item[@title='0x|Beschrijving']");
var_dump($description);
?>

Voici le résultat:

array(1) {
  [0]=>
  object(SimpleXMLElement)#2 (2) {
    ["@attributes"]=>
    array(4) {
      ["id"]=>
      string(15) "787900813228567"
      ["view"]=>
      string(5) "12000"
      ["title"]=>
      string(15) "0x|Beschrijving"
      ["engtitle"]=>
      string(14) "0x|Description"
    }
    [0]=>
    string(41) "Dit college leert studenten hoe ze een on"
  }
}

Autres conseils

Je pense que c'est l'équivalent de la méthode __toString () sur l'objet, donc

echo $description[0];

devrait l'afficher, ou vous pouvez le lancer;

$str = (string) $description[0];

Consultez la documentation PHP.net pour "SimpleXMLElement". ( http://fr.php.net/manual/fr /function.simplexml-element-children.php ) cela ressemble à la conversion du nœud en chaîne "(chaîne) $ valeur;"; fait le tour.

À défaut, de nombreux exemples sur cette page devraient vous guider dans la bonne direction!

$description = $xml->xpath("//item[@title='0x|Beschrijving']");

while(list( , $node) = each($description)) {

echo($node);

}

la solution de dreamwerx est meilleure

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top