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

simpleXML and PHP. How to find if a node is empty or exists?

P: n/a
SM
I think that simpleXML is the best thing i've seen in PHP5. It's so
simple (d'ho!) and easy to use when

it comes to reading a simple XML file. The only thing i'm not sure how
to achieve is finding if an XML

node is empty or if the node exist or doesn't exist.

I'm searching the web, but got nothing yet. I was thinking of posting
it here.

This is a condensed version of the code:

<?xml version="1.0" encoding="UTF-8"?>
<CD>
<title>Shine</title>
<year>1985</year>
<label></label>
<description>Good CD from the 1980s</description>

<item>
<track id="18">Moonlight</track>
<track>Shine</track>
<track>Absolute</track>
</item>

<item>
<track>Relative</track>
<track>Star</track>
<track>Crazy</track>
</item>
</CD>

$file = 'db_cd_number.xml';
$xml = @simplexml_load_file($file);

//??????
if(title node is empty || title node doesn't exist) {
echo 'title is empty or doesnt exist, do something';
} else {
echo 'title OK, continue';
}

//i.e. 2 What about attributes??????????
foreach ($xml->item-as $item) {
foreach($item->track as $track) {
if($track['id'] doesn't exist or empty) {
echo 'dont show the lyrics';
}
}
}
How do i find if a node is empty or if it exists?
Thanks
Marco
Jun 2 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
How do i find if a node is empty or if it exists?

http://www.emopanda.com/tmp/simplexml.php
http://www.emopanda.com/tmp/simplexml_php.txt

I made some changes- I inverted your if-tests so that they're testing
for positives not negatives (it's more intuitive to write, you can use
your !'s if you really need 'em that way for some reason.)
(drawing from string not file for simplicity)

<?php

$string = '<?xml version="1.0" encoding="UTF-8"?>
<CD>
<title>Shine</title>
<year>1985</year>
<label></label>
<description>Good CD from the 1980s</description>

<item>
<track id="18">Moonlight</track>
<track>Shine</track>
<track>Absolute</track>
</item>

<item>
<track id="">Relative</track>
<track id="4">Star</track>
<track id="0">Crazy</track>
</item>
</CD>';

$xml = @simplexml_load_string($string);

if( $xml->title && ($xml->title != '') ) {
echo 'title OK, continue<br/><br/>';
} else {
echo 'title is empty or doesnt exist, do something<br/><br/>';
}

foreach ($xml->item as $item) {
foreach($item->track as $track) {
$attribs = $track->attributes(); //You have to

if( $attribs['id'] && $attribs['id'] == true) {
echo '<b>Lyrics:</b<i>"Oh baby ' . $track . '- yeah, my honey
baby ' . $track . '~"</i><br/>';
} else{
echo 'LYRICS FOR "' . strtoupper($track) . '" WILL NOT NOT
SHOWN<br/>';
}
}

}
?>
This should work as long as your ID's are greater than 0. <title>
works even if the title is the number 0 or the string 'false'.

The .txt file I linked to above has comments with alternate
approaches.

-Derik
Jun 2 '08 #2

P: n/a
SM
On May 27, 9:15 pm, Derik <ReGenes...@aol.comwrote:
How do i find if a node is empty or if it exists?

http://www.emopanda.com/tmp/simplexm...plexml_php.txt

I made some changes- I inverted your if-tests so that they're testing
for positives not negatives (it's more intuitive to write, you can use
your !'s if you really need 'em that way for some reason.)
(drawing from string not file for simplicity)

<?php

$string = '<?xml version="1.0" encoding="UTF-8"?>
<CD>
<title>Shine</title>
<year>1985</year>
<label></label>
<description>Good CD from the 1980s</description>

<item>
<track id="18">Moonlight</track>
<track>Shine</track>
<track>Absolute</track>
</item>

<item>
<track id="">Relative</track>
<track id="4">Star</track>
<track id="0">Crazy</track>
</item>
</CD>';

$xml = @simplexml_load_string($string);

if( $xml->title && ($xml->title != '') ) {
echo 'title OK, continue<br/><br/>';} else {

echo 'title is empty or doesnt exist, do something<br/><br/>';

}

foreach ($xml->item as $item) {
foreach($item->track as $track) {
$attribs = $track->attributes(); //You have to

if( $attribs['id'] && $attribs['id'] == true) {
echo '<b>Lyrics:</b<i>"Oh baby ' . $track . '- yeah, my honey
baby ' . $track . '~"</i><br/>';
} else{
echo 'LYRICS FOR "' . strtoupper($track) . '" WILL NOT NOT
SHOWN<br/>';
}
}

}

?>
This should work as long as your ID's are greater than 0. <title>
works even if the title is the number 0 or the string 'false'.

The .txt file I linked to above has comments with alternate
approaches.

-Derik
Just one question: Why do you use this twice : if( $attribs['id'] &&
$attribs['id'] == true). Is this a typo or it should be called twice?

Thanks
Jun 2 '08 #3

P: n/a
SM
On May 27, 9:15 pm, Derik <ReGenes...@aol.comwrote:
How do i find if a node is empty or if it exists?

http://www.emopanda.com/tmp/simplexm...plexml_php.txt

I made some changes- I inverted your if-tests so that they're testing
for positives not negatives (it's more intuitive to write, you can use
your !'s if you really need 'em that way for some reason.)
(drawing from string not file for simplicity)

<?php

$string = '<?xml version="1.0" encoding="UTF-8"?>
<CD>
<title>Shine</title>
<year>1985</year>
<label></label>
<description>Good CD from the 1980s</description>

<item>
<track id="18">Moonlight</track>
<track>Shine</track>
<track>Absolute</track>
</item>

<item>
<track id="">Relative</track>
<track id="4">Star</track>
<track id="0">Crazy</track>
</item>
</CD>';

$xml = @simplexml_load_string($string);

if( $xml->title && ($xml->title != '') ) {
echo 'title OK, continue<br/><br/>';} else {

echo 'title is empty or doesnt exist, do something<br/><br/>';

}

foreach ($xml->item as $item) {
foreach($item->track as $track) {
$attribs = $track->attributes(); //You have to

if( $attribs['id'] && $attribs['id'] == true) {
echo '<b>Lyrics:</b<i>"Oh baby ' . $track . '- yeah, my honey
baby ' . $track . '~"</i><br/>';
} else{
echo 'LYRICS FOR "' . strtoupper($track) . '" WILL NOT NOT
SHOWN<br/>';
}
}

}

?>
This should work as long as your ID's are greater than 0. <title>
works even if the title is the number 0 or the string 'false'.

The .txt file I linked to above has comments with alternate
approaches.

-Derik
Hey thanks Derik.
This is what i've been looking for.

Thanks again
Marco
Jun 2 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.