Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  175] [ 1]  / answers: 1 / hits: 27067  / 15 Years ago, tue, july 21, 2009, 12:00:00

Consider this problem:



Using Javascript/E4X, in a non-browser usage scenario (a Javascript HL7 integration engine), there is a variable holding an XML snippet that could have multiple repeating nodes.



<pets>       
<pet type=dog>Barney</pet>
<pet type=cat>Socks</pet>
</pets>


Question: How to get the count of the number of pet nodes in Javascript/E4X ?



EDIT: To clarify, this question should be around E4X (ECMAScript for XML). Apologies to those who answered without this information. I should have researched & posted this info beforehand.


More From » xml

 Answers
13

Use an E4X XML object to build an XMLList of your 'pet' nodes. You can then call the length method on the XMLList.



//<pets>
// <pet type=dog>Barney</pet>
// <pet type=cat>Socks</pet>
//</pets>

// initialized to some XML resembling your example
var petsXML = new XML();

// build XMLList
var petList = petsXML['pet'];

// alternative syntax
// var petList = petsXML.pet;

// how many pet nodes are under a given pets node?
var length = petList.length();

[#99083] Friday, July 17, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;