7

Anyone that knows PHP and XML out there? Then please have a look!

This is my PHP code:

<? $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>

<? } ?>

This is mys XML file:

<?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>

The outcome of the code above is:

<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>

I want it to be like this (see category on the first movie):

<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>

Note: Also I wonder how to get the comma between the words, but without a comma on the last word...

0

7 Answers 7

30

Try this.

<?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>';
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for stripping out the trailing comma. while($categorie = current($movie->...->categorie)) and echo next($movie->...->categorie) ? ', ' : ''; works too.
6

this is how you use foreach with an simplexmlElement:

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

Comments

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

Comments

1

You need to iterate through the categorie elements too, in the same way you've iterated through movies.

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

You'll probably want to trim the trailing , as well.


The method mentioned in my comment:

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

Comments

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

Comments

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

  return implode(', ', $cats);

}

Then you can jsut call it from your loop like:

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

Using an array and implode removes the need for logic of counting and detecting the last category.

Isolating it in a function makes it easier to maintain and is less confusing than nesting the loop (though admittedly nested loops arent that confusing).

Comments

-1

Try This one

$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 Comments

The json_encode+json_decode is absolutely unnecessary here
this function returning json here Xml_to_Json if you want Xml_to_Array then remove json_encode+json_decode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.