Hello
I have simple question using simpleXML and PHP.
i have an xml file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<discography version="0.01">
<CD>
<title>Moonligh t</title>
<year>1978</year>
<company>RCA</company>
<item label="CD 1:">
<title>Moonligh t</title>
<title>Take It</title>
<title>My Forever</title>
<title>Anymor e</title>
</item>
<item label="Cd 2:">
<title>All Right</title>
<title>No More</title>
<title>Betwee n</title>
<title>Why</title>
</item>
</CD>
</discography>
Note that there could be more then one <itemtag. In this case, there
is 2, but it could be 1 or 5...
Using php, the output should look like this:
Moonlight
1978
RCA
CD 1:
*Moonlight
*Take It
*My Forever
*Anymore
CD 2:
*All Right
*No More
*Between
*Why
I'm almost there, but i don't know how to loop to get the <title>'s of
al the <itemtags and the attribute of all the <item>'s tag.
Here's my code:
<?php
if (file_exists('d b/discography/cd_1.xml')) {
$xml = simplexml_load_ file('db/discography/cd_1.xml');
}
else {
exit('Error opening the XML file.');
}
$title = $xml->CD->title;
$year = $xml->CD->year;
$company = $xml->CD->company;
?>
<h1><?php echo $title; ?></h1>
<h3><?php echo $year; ?></h3>
<h3><?php echo $company; ?></h3>
//get the <title>'s of all the <item>
<ul>
<?php
foreach ($xml->CD->item as $item) {
?>
<li><?php echo $item->title; ?></li>
<?php
}
?>
</ul>
My code will output:
Moonlight
1978
RCA
*Moonlight
How do i loop through all the <titlein al lthe <item>'s that exist
in the xml + how do i get the attribute of the <itemfile??
Help!
Thanks
Marco