Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check XML node exists or not using javascript

Tags:

javascript

How do check 'roster' has no child values using javascript

<club code="3RD1" tvcode="">
   <name>3RD PLACE TEAM 1</name>
   <roster/>
</club>
<club code="3RD1" tvcode="">
   <name>3RD PLACE TEAM 1</name>
   <roster>
     <player code="AUQ"/>
   </roster>
</club>
like image 200
vishnu Avatar asked Oct 05 '22 04:10

vishnu


1 Answers

if you can use jquery,

you can parse this by using $.parseXML

xmlDoc = $.parseXML( "<xml></xml>" );

and then you can use find to get the prefered node

$(xmlDoc).find("roster")

and get number of children via

.children().length

putting it together

var rostersChildren = $(xmlDoc).find("roster").children().length;
console.log(rostersChildren > 0);

here is a sample fiddle.

like image 189
Mithun Satheesh Avatar answered Oct 10 '22 03:10

Mithun Satheesh