473,387 Members | 1,798 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,387 software developers and data experts.

How to find non-existing nodes or nodes with no text

SM
Ok, this must be simple but the more i search the more i don't find.
It's about SimpleXML and PHP.
How to find non-existing nodes or nodes with no text

My XML file looks a little bit like this:

i.e. 1
<discography>
<CD>
<title></title>
<year>1978</year>
</CD>
</discography>
i.e. 2:
<discography>
<CD>
<year>1978</year>
</CD>
</discography>
In i.e. 1, the tag <titleis empty. How do i test for empty tags in
PHP?
In i.e. 2, the tag <titledoesn't even exist. How do i test for non-
existing tags?
This is how i get the values from XML in PHP without validation
....
//set the XML file name as a PHP string
$cd_xml = "db/discography/cd.xml";

//load the XML file
$xml = @simplexml_load_file($cd_xml) or die ("Error: The XML file
can't open.") ;
....

//no validation if empty, creates empty <h1tags. Not good!
<h1><?php echo $xml->CD->title; ?></h1>
<h2><?php echo $xml->CD->year; ?></h2>

How to validate?
Any suggestions?

Thanks in advance
Marco
Jun 2 '08 #1
1 1455
<?php if (isset($xml->CD->title) && trim($xml->CD->title) != '') { ?>

<h1><?php echo $xml->CD->title ?></h1>

<?php } ?>

On May 9, 7:01 pm, SM <servandomont...@gmail.comwrote:
Ok, this must be simple but the more i search the more i don't find.
It's about SimpleXML and PHP.
How to find non-existing nodes or nodes with no text

My XML file looks a little bit like this:

i.e. 1
<discography>
<CD>
<title></title>
<year>1978</year>
</CD>
</discography>

i.e. 2:
<discography>
<CD>
<year>1978</year>
</CD>
</discography>

In i.e. 1, the tag <titleis empty. How do i test for empty tags in
PHP?
In i.e. 2, the tag <titledoesn't even exist. How do i test for non-
existing tags?

This is how i get the values from XML in PHP without validation
...
//set the XML file name as a PHP string
$cd_xml = "db/discography/cd.xml";

//load the XML file
$xml = @simplexml_load_file($cd_xml) or die ("Error: The XML file
can't open.") ;
...

//no validation if empty, creates empty <h1tags. Not good!
<h1><?php echo $xml->CD->title; ?></h1>
<h2><?php echo $xml->CD->year; ?></h2>

How to validate?
Any suggestions?

Thanks in advance
Marco
Jun 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: simduss | last post by:
Hi, First of all, I'm a beginner with Unix. I have a "make" (Unix command) problem with a Pro*C sub-program since I installed Oracle8i (before I was at 7.3.4). I have a script builder that...
24
by: shafique | last post by:
Hello, Can anybody have idea as how to find it there exist consective one's in a word/dword using bitwise operatiors in C language. Obviously this can be done by shifting and looping one by...
6
by: Ravi | last post by:
Hi, I need to find the non-recursive algorithm to find the height of a Binary Tree. Regards, SunLight.
29
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
2
by: tbh | last post by:
hi, hope this cross-post is OK. it's unclear to me whether this question belongs more under vstudio or dotnet... i'm using VS2005 pro and am one co-developer of a web solution that is getting to...
3
usafshah
by: usafshah | last post by:
C:\Documents and Settings\Link>nslookup *** Can't find server name for address 192.168.0.100: Non-existent domain Default Server: UnKnown Address: 192.168.0.100 > mypc *** UnKnown can't find...
0
by: Bryce | last post by:
I have an ObjectDataSource binding a GridView, with Delete enabled. The following is the declaration (with non-delete stuff left out to save space): <asp:ObjectDataSource...
18
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a...
0
by: Cirene | last post by:
Using Visual Studio I created a DataSet using the GUI (XSD file). Trying to use a tiered methodology I called the functions from my BLL. Namespace Zzz.BusinessLogicLayer #Region "DAL Access"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.