# javascript/ custom element children



## ste2425 (Sep 24, 2013)

Im reading a xml file and parsing the result. This has gone without a hitch and i have returned a custom object with various child elements nested within each other depending on the nesting of the xml file.

What im trying to do is return a boolean if a property of the element contains children elements. 

For example from the above picture if i condition the property 'test-results' it should return true. However if i condition the property 'culture-info' it should return false as there are no more nested properties within. 

Trouble is no matter what i try i get zero or null returned no matter which properties i try and condition.

Ive tried jquery checking for children.

Here my code example


```
function readXml() {
    var url = "test.xml";
    var xml = new JKL.ParseXML(url);
    var data = xml.parse();
    var text = $(data["test-suite"]).children().length;
}
```

yet text is equal to zero.

Hope this makes sense and you all can help ta

EDIT:

think i misunderstood things. I think its called an object not an element. What i want in the end is a loop that will run though the first level properties of data, culture-info,date,environment,errors,failures,inconclusive,invalid,name,not-run,skipped,test-suite,time and total. For all except culture-info,environment and test-suite it will return false. For them three it will return true. But i may not know the names of the nested properties to check. All the examples ive found so far require you to know in advance the property names of the object.


----------



## BiggieShady (Sep 24, 2013)

using jquery it would be 


```
$(data).find("test-suite").children().length
```

or, with plain javascript, try ChildNodes.length


```
xmlDoc=loadXMLDoc("test.xml");
x=xmlDoc.getElementsByTagName("test-suite")[0].childNodes;
document.write(x.length);
```

this is only for the first test-suite node in the xml. You should iterate through all of them.


----------



## ste2425 (Sep 25, 2013)

BiggieShady said:


> using jquery it would be
> 
> 
> ```
> ...



That plain javascript option works a treat thanks. One question. When i use childnodes on test-results it returns 6. When i drill down in the debugger its picking up the three child nodes, culture-info, environment and test-suite but im also getting three nodes by name #text but there are no more child nodes. What is this #text?

edit:
Looking at node type all nodes with this #text name return a nodetype of 1 whilst the others return 3. According to w3c 1 means Text_node and three Element_node. I can just condition to check if the nodes type ==  3 before doing anything with it.


----------



## BiggieShady (Sep 25, 2013)

Ah, sorry about jQuery. It should be 
	
	



```
$(data).find("test-suite").length
```


----------

