473,491 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I have simple question using simpleXML and PHP

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
5 1718
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2566
by: Joshua Beall | last post by:
Hi All, I have a task that should be very simple but I'm running into trouble. All I want to do is query a document using XPath, and save the resulting XML in a string. Here's that I am trying...
1
4630
by: Harry Potter | last post by:
Hi, I have a problem with both DOM and SimpleXML. I'm reading an XML file which has a DOCTYPE declaration using a public identifier and an URL. the XML file contains some &nbsp; entities...
1
3561
by: hwcowan | last post by:
Hello, I have started using SimpleXML and can do most things, but there are a couple of things that I can't seem to figure out. Currently, I can: 1. Open & load an XML file 2. Manually...
2
12358
by: whisher | last post by:
Hi. I'm taking my first steps with xml and SimpleXML functions. xml.php (It could be made dynamically with a mysql result I post this snippet for example ) PHP Code xml.php: <?php $xmlstr...
0
2538
by: b.coolsaet | last post by:
Hi, like the title says simplexml is giving me a "Node no longer exists" error while using the addChild() function. Note that it's not giving an error on the first addChild(); Can somebody...
1
4192
by: Andrew Taylor | last post by:
Hi, I've got an RSS feed I'd like to consume from our Sharepoint server. When I try: $xml = simplexml_load_file($url); I get failed to open stream: HTTP request failed! HTTP/1.1 401...
3
5323
by: bleen | last post by:
I'm trying to use SimpleXML but I've run into a conundrum. Every day an XML file is generated that this script grabs and manipulates. How can I check that the XML has no problems before creating...
7
2906
by: dimo414 | last post by:
So I'm trying to use SimpleXML to get some attribute information about some nodes in my XML document, but it seems like SimpleXML ignores attributes for elements with no children, For instance:...
1
4375
by: duane.lortie | last post by:
I'm writing a routine that fetches XML and attempts to parse some values from it. A condensed entry node of the XML looks like this .. <entry> <title>Some title</title> <author>...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.