Solved

Javascript: how do i check if node has children

Posted on 2014-03-03
5
252 Views
Last Modified: 2014-03-12
I have the following XML and I want a function that will throw an error whenever there's an instance of an Empty <Name> (or where <Name> has no children). How do I capture that?

<root>
	<Person>
		<Name/>
	</Person>
	<Person>
		<Name>
			<fname>babe</fname>
			<lname>ruth</lname>
		</Name>
	</Person>
</root>

Open in new window

i have the following code:
var nsPerson = dom.SelectNode(dom.Root, "Person/Name");
if (nsPerson)
{
	for (var i=0; i < nsPerson.Count; i++)	
	{
		var nPerson =  nsPerson.GetItem(i);
		if (!nPerson)
		{
		  //throw "Empty Person.";
				
		}
	}
}

Open in new window

0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Sar1973
ID: 39902678
With document.getElementById("yourItem").parentNode; see http://www.w3schools.com/jsref/prop_node_parentnode.asp.
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39902685
Sar1973 how would you update his code using your solution?
0
 

Accepted Solution

by:
badtz7229 earned 0 total points
ID: 39913596
i answered my own question.

var nsPerson = dom.SelectNode(dom.Root, "Person/Name");
if (nsPerson)
{
      for (var i=0; i < nsPerson.Count; i++)      
      {
            var nPerson =  nsPerson.GetItem(i);
            if (nPerson)
            {
                           var hasChildren = nPerson.SelectSingle('*');
                           if (!hasChildren)
                                    throw "Empty, try again.";                  
                        
            }
      }
}
0
 

Author Closing Comment

by:badtz7229
ID: 39922903
this worked
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question