473,769 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ 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>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
Jun 2 '08 #1
5 1733
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*******@attgl obal.net
=============== ===

Jun 2 '08 #5
SM
On May 8, 11:01 am, Jerry Stuckle <jstuck...@attg lobal.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...@attgl obal.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
2589
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 (PHP5): // $xml has the entire XML document in it; I included it for reference at the end of the document $dom = new DOMDocument(); $dom->loadXML($xml);
1
4660
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 (defined in the DTD but apparently libxml is not trying to fetch the URL). Both DOM and SimpleXML fill my window with warnings like: Warning: DOMDocument::load() : Entity 'nbsp' not defined
1
3590
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 parse the XML file 3. Search the XML file using XPath
2
12371
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 = "<?xml version=\"1.0\" standalone=\"yes\"?>";
0
2573
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 help ? code causing error:
1
4223
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 Unauthorized
3
5338
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 my SimpleXMLelement object. Here's what I mean: // this is the code in question: $file_topstory = /some/xml/file.xml $top_story_xml = new SimpleXMLElement($file_topstory, NULL, TRUE); If the XML doc contains (for example) a URL with an '&' in...
7
2922
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: <xmlfile> <object foo="bar"> <color mode="rgb">ffddee</color> </object>
1
4395
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> <name>Author</name> </author> <source:resource url="http://somesite.com?tid=123456"/>
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5294
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.