I have simple question using simpleXML and PHP

This is a discussion on I have simple question using simpleXML and PHP within the PHP Language forums, part of the PHP Programming Forums category; Hello I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-08-2008
SM
 
Posts: n/a
Default I have simple question using simpleXML and PHP

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 <item> tag. 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 <item> tags 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 <title> in al lthe <item>'s that exist
in the xml + how do i get the attribute of the <item> file??

Help!
Thanks
Marco
Reply With Quote
  #2 (permalink)  
Old 05-08-2008
Álvaro G. Vicario
 
Posts: n/a
Default Re: I have simple question using simpleXML and PHP

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 <title> in al lthe <item>'s that exist
> in the xml + how do i get the attribute of the <item> file??


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
--
Reply With Quote
  #3 (permalink)  
Old 05-08-2008
Janwillem Borleffs
 
Posts: n/a
Default Re: I have simple question using simpleXML and PHP

SM schreef:
> How do i loop through all the <title> in al lthe <item>'s that exist
> in the xml + how do i get the attribute of the <item> file??
>


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

JW
Reply With Quote
  #4 (permalink)  
Old 05-08-2008
SM
 
Posts: n/a
Default Re: I have simple question using simpleXML and PHP

On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.com> wrote:
> SM schreef:
>
> > How do i loop through all the <title> in al lthe <item>'s that exist
> > in the xml + how do i get the attribute of the <item> file??

>
> <?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
Reply With Quote
  #5 (permalink)  
Old 05-08-2008
Jerry Stuckle
 
Posts: n/a
Default Re: I have simple question using simpleXML and PHP

SM wrote:
> On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.com> wrote:
>> SM schreef:
>>
>>> How do i loop through all the <title> in al lthe <item>'s that exist
>>> in the xml + how do i get the attribute of the <item> file??

>> <?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.
jstucklex@attglobal.net
==================

Reply With Quote
  #6 (permalink)  
Old 05-08-2008
SM
 
Posts: n/a
Default Re: I have simple question using simpleXML and PHP

On May 8, 11:01 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> SM wrote:
> > On May 8, 5:20 am, Janwillem Borleffs <j...@jwscripts.com> wrote:
> >> SM schreef:

>
> >>> How do i loop through all the <title> in al lthe <item>'s that exist
> >>> in the xml + how do i get the attribute of the <item> file??
> >> <?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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:32 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0