473,799 Members | 3,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1477
<?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.comwrot e:
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
8460
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 generate a makefile (that include env_precomp.mk) and launch this makefile after. So, I receive this error: make: 1254-002 Cannot find a rule to create target /u02/apps_r/dev/ben/lib/fcfz02.a from dependencies.
24
2227
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 one; but there must be some faster way of doing it. The thing I want is to define some masks and macros and find if there are consective ones on a word/dword. e.g. 0000b has no consective
6
11211
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
51756
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
2
1487
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 be at least medium sized. (about 60k lines of C# in the solution itself and perhaps another 20k lines in libraries we've developed.) finding classes we've built can be challenging at times and in theory the object browser should be my friend.
3
26125
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 mypc: Non-existent domain ------------------------------------------------------------------
0
1516
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 ID="CatalogItemsListDataSource" runat="server" DeleteMethod="DeleteCatalogItem" InsertMethod="InsertCatalogItem" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCatalogItemsList" TypeName="CatalogItemManager" UpdateMethod="UpdateCatalogItem">...
18
3874
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 given file. I tried my level best even after the quiz to come up with a solution but cudnt find an efficient one. :( This is what I did.
0
1581
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" Public Class States Public Sub New() End Sub
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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,...
1
10214
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
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
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
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...
1
4135
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
3
2935
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.