By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,547 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

I have simple question using simpleXML and PHP

P: n/a
SM
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>Moonlight</title>
<year>1978</year>
<company>RCA</company>
<item label="CD 1:">
<title>Moonlight</title>
<title>Take It</title>
<title>My Forever</title>
<title>Anymore</title>
</item>
<item label="Cd 2:">
<title>All Right</title>
<title>No More</title>
<title>Between</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('db/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
Jun 2 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
SM escribió/wrote:
[...]
//get the <title>'s of all the <item>
<ul>
<?php
foreach ($xml->CD->item as $item) {
?>
<li><?php echo $item->title; ?></li>
<?php
}
?>
</ul>
[...]
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??
It's very easy if you know the actual contents of the variables you
handle. Add this line to the loop and you'll understand the issue:

var_dump($item);

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 2 '08 #2

P: n/a
SM schreef:
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??
<?php
foreach ($xml->CD->item as $item) {
foreach ($item->title as $value) {
?>
<li><?php echo $value; ?></li>
<?php
}
}
?>

JW
Jun 2 '08 #3

P: n/a
SM
On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.comwrote:
SM schreef:
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??

<?php
foreach ($xml->CD->item as $item) {
foreach ($item->title as $value) {
?>
<li><?php echo $value; ?></li>
<?php
}
}
?>

JW
Thanks JW & Alvaro:
I've decided on JW solution. I couldn't understand the var_dump
solution. I was also able to get the attributes.. Anyways, if it
helps, here's the code:

Thanks JW & Alvaro:
I've decided on JW solution. I couldn't understand the var_dump
solution. I was also able to get the attributes.. Anyways, if it
helps, here's the final code:
....
<h1>Tracks</h1>
<?php
foreach ($xml->CD->item as $item) {
?>
<h3><?php echo $item['label']; ?></h3>
<ul>
<?php foreach ($item->title() as $track) {
?>
<li><?php echo $track; ?></li>
<?php
}
?>
</ul>
<?php
}
?>
....

The output:

Moonlight
1978
RCA

CD 1:
Moonlight
Take It
My Forever
Anymore

CD 2:
All Right
No More
Between
Why
Jun 2 '08 #4

P: n/a
SM wrote:
On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.comwrote:
>SM schreef:
>>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??
<?php
foreach ($xml->CD->item as $item) {
foreach ($item->title as $value) {
?>
<li><?php echo $value; ?></li>
<?php
}
}
?>

JW

Thanks JW & Alvaro:
I've decided on JW solution. I couldn't understand the var_dump
solution. I was also able to get the attributes.. Anyways, if it
helps, here's the code:
<code snipped>

Alvaro was trying to help you in a more general way. var_dump()
displays the contents of the array, including the indexes and values.
From there you have a lot better shot at figuring out how to build the
code you need to use the array. Then the next time you run into a
problem with arrays or other complex objects (whether simpleXML or
something else), you can solve it more easily.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #5

P: n/a
SM
On May 8, 11:01 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
SM wrote:
On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.comwrote:
SM schreef:
>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??
<?php
foreach ($xml->CD->item as $item) {
foreach ($item->title as $value) {
?>
<li><?php echo $value; ?></li>
<?php
}
}
?>
JW
Thanks JW & Alvaro:
I've decided on JW solution. I couldn't understand the var_dump
solution. I was also able to get the attributes.. Anyways, if it
helps, here's the code:

<code snipped>

Alvaro was trying to help you in a more general way. var_dump()
displays the contents of the array, including the indexes and values.
From there you have a lot better shot at figuring out how to build the
code you need to use the array. Then the next time you run into a
problem with arrays or other complex objects (whether simpleXML or
something else), you can solve it more easily.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks for the explanation. It's a good tip for debugging...
Marco
Jun 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.