473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1711
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.