7
votes

Comment utiliser PHP & XML (SimplexML)

Quiconque connaît PHP et XML là-bas? Ensuite, veuillez jeter un coup d'œil!

Ceci est mon code PHP: strong> p> xxx pré>

Ceci est MyS XML Fichier: P>

<h2>A movie name</h2>
<p>Year: 2010</p>
<p>Category: Animation, Comedy, Family</p>
<p>Country: USA</p>

<h2>Little Fockers</h2>
<p>Year: 2010</p>
<p>Category: Comedy</p>
<p>Country: USA</p>


0 commentaires

7 Réponses :


1
votes

Vous devez également parcourir les éléments code> Catégorie CODE>, de la même manière que vous avez itéré via films code>.

$categories = $movie->regions->region->categories->categorie;
while($category = current($categories)){
    echo $category . next($categories) ? ', ' : '';
}


0 commentaires

0
votes
<? foreach($movie->regions->region->categories->categorie as $category) { ?>
<p>Categori: <?= $category ?></p>
<? } ?>

0 commentaires

28
votes

Essayez ceci. XXX


1 commentaires

+1 pour éliminer la virgule de fuite. tandis que ($ Catégorie = actuel ($ film -> ...-> Catégorie)) et Echo Suivant ($ Movie -> ...-> Catégorie)? ',': ''; fonctionne aussi.



0
votes
<? echo categoryList($movie->regions->region->categories); ?>

0 commentaires

1
votes
<?php
$cat_out='';
foreach($movie->regions->region->categories->categorie as $cat){
 $cat_out.= $cat.',';
}
echo rtrim($cat_out,',');
?>

0 commentaires

6
votes

C'est ainsi que vous utilisez pour utiliser un simplexmlelement: xxx


0 commentaires

-1
votes

Essayez celui-ci

$xml = ... // Xml file data
$Json = Xml_to_Json($xml);
$array = json_decode($Json,true);
echo '<pre>'; print_r($array);
foreach ($array as $key => $value) {
    foreach ($value as $key1 => $value1) {
        echo '<h2>'.$value1['title'].'</h2>
        <p>Year: '.$value1['year'].'</p>
        <p>Category: ';
        foreach ($value1['regions']['region']['categories']['categorie'] as $categoriekey => $categorie) {
            echo $categorie.' ';                
        }
        echo 'Animation</p>
        <p>Country: '.$value1['regions']['region']['countries']['country'].'</p>';
    }
}

function Xml_to_Json($array){
    $xml = simplexml_load_string($array, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    return $json;
}


2 commentaires

Le JSON_ENCODE + JSON_DECODE est absolument inutile ici


Cette fonction renvoie JSON ici ici xml_to_json si vous souhaitez xml_to_array supprimer json_encode + json_decode.