Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 8th, 2006, 12:14 PM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default firstchild.text is null...

Hi,

I'm reading through several XML files, but sometimes the tag has no text... How can I check to see if firstchild.text is null or not... I tried xmlObj.childNodes(i).childNodes(j).firstchild.text != null, but that didn't work... Any suggestion would be greatly appreciated...

  <Port>32459</Port>
  <DNS_Name />

Reply With Quote
  #2 (permalink)  
Old June 14th, 2006, 03:31 PM
Registered User
 
Join Date: Jun 2006
Location: boston, MA, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well if a element is empty and doesnt have any value ( i.e. it doesnt even ahve null ) , you could use the

xmlObj.getFirstChild().getValue() != null

Assuming that the xmlObj is pointing to the root of the XML file and the first child is the element u want to check for .

If the element you want to check is not the first child u can use the method "getElementByTagName ( ) " method to get the corresponding element by its name

Hope it helps .

Reply With Quote
  #3 (permalink)  
Old July 30th, 2007, 09:57 PM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have found another solution for this question.

In JavaScript with XML, if the node does not have any value, JavaScript consider it as an empty field. In this case JavaScript will throw an error that object not found. To overcome this problem use hasChildNodes() method. If the node is exist the result will be true otherwise it will be false.

For example,

<company>
<employee>
<name>Tom<name>
<city>Newyork</city>
<age></age>
</employee>
</company>

Here, <age> does not have any value so JavaScript will throw an error.
To solve this problem, follow the below example.

var age = "";
if(xmlObj.childNodes(0).hasChildNodes()){
  age = xmlObj.childNodes(0).firstChild.text;
}
else{
  age = "Age not provided...";
}

This is just a sample code, this method worked for me and hope it will be useful to others.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to show text in datalist while value in NULL nittin14 ASP.NET 1.0 and 1.1 Basics 4 September 1st, 2007 06:28 PM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
Don;t diplay text box if it is empty or null abdul_wasie Classic ASP Databases 5 May 20th, 2005 11:49 PM
Extract text from text file & put in dropdown box tsukey Beginning PHP 5 July 20th, 2004 09:49 PM



All times are GMT -4. The time now is 07:38 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.