如何使用PHP和PHP的foreach XML(simplexml)

时间:2011-01-09 04:02:44

标签: php xml simplexml

那些知道PHP和XML的人吗?那么请看看!

这是我的PHP代码:

<? $xml = simplexml_load_file("movies.xml");
foreach ($xml->movie as $movie){ ?>

<h2><? echo $movie->title ?></h2>
<p>Year: <? echo $movie->year ?></p>
<p>Categori: <? echo $movie->regions->region->categories->categorie ?></p>
<p>Country: <? echo $movie->countries->country ?></p>

<? } ?>

这是mys XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<movies>
 <movie>
  <title>A movie name</title>
  <year>2010</year>
     <regions>
   <region>
    <categories>
      <categorie id="3">Animation</categorie>
      <categorie id="5">Comedy</categorie>
      <categorie id="9">Family</categorie>
     </categories>
    <countries>
     <country id="123">USA</country>
    </countries>
   </region>
  </regions>
 </movie>
 <movie>
  <title>Little Fockers</title>
  <year>2010</year>
     <regions>
   <region>
    <categories>
     <categorie id="5">Comedy</categorie>
             </categories>
        <countries>
         <country id="123">USA</country>
    </countries>
   </region>
  </regions>
 </movie>
</movies>

上述代码的结果是:

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

<h2>Little Fockers</h2>
<p>Year: 2010</p>
<p>Category: Comedy</p>
<p>Country: USA</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>

注意:我也想知道如何在单词之间加上逗号,但在最后一个单词上没有逗号......

7 个答案:

答案 0 :(得分:22)

试试这个。

<?php
$xml = simplexml_load_file("movies.xml");
foreach ($xml->movie as $movie) {

    echo '<h2>' . $movie->title . '</h2>';
    echo '<p>' . $movie->year . '</p>';

    $categories = $movie->regions->region->categories->categorie;

    while ($categorie = current($categories)) {
        echo $categorie;
        echo next($categories) ? ', ' : null;
    }

    echo '<p>' . $movie->countries->country . '</p>';
}
?>

答案 1 :(得分:4)

这就是你如何将foreach与simplexmlElement一起使用:

$xml = simplexml_load_file("movies.xml");
foreach ($xml->children() as $children) {
    echo $children->name;
}

答案 2 :(得分:1)

您也需要遍历categorie元素,就像迭代movies一样。

echo '<p>';
foreach($movie->regions->region->categories->categorie as $categorie){
    echo $categorie . ', ';
}
echo '</p>';

您可能也希望修剪尾随,


我的评论中提到的方法:

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

function categoryList(SimpleXmlElement $categories){
  $cats = array();
  foreach($categories as $category){
    $cats[] = (string) $category;
  }

  return implode(', ', $cats);

}

然后你可以从你的循环中调用它,如:

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

使用数组和implode消除了对计数和检测最后一个类别的逻辑的需要。

在函数中隔离它使得它更容易维护,并且比嵌套循环更容易混淆(尽管可以肯定的是嵌套循环 令人困惑)。

答案 6 :(得分:0)

试试这个

$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;
}