473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

get the number of children of a given element

I want to get the number of children of a given element. Below is my
code attempt and
the xml sample file. The caller is getElementChildNumber("persons");,
I expect
to get 2. Since there are 2 children that are under persons element.
Any ideas??

/**
get the number of children of a given element
*/
public int getElementChildNumber(String elementName)
{
DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
Document doc = null;
try
{
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
doc = db.parse(fileName);
NodeList nodelist = doc.getElementsByTagName(elementName);
return nodelist.getLength();
}
catch (Exception e)
{ e.printStackTrace();
}
return -1;
}

<?xml version="1.0" encoding="UTF-8"?>
<response>
<persons>
<person>
<name>Joe</name>
<age>10</age>
</person>
<person>
<name>Mark</name>
<age>30</age>
</person>
</persons>
</response>
Jul 20 '05 #1
1 2491


Matt wrote:
I want to get the number of children of a given element. Below is my
code attempt and
the xml sample file. The caller is getElementChildNumber("persons");,
I expect
to get 2. Since there are 2 children that are under persons element.


node.getElementsByTagName("persons") gives you a list of <persons>
elements contained in the node. If you want to count the child nodes use
node.getChildNodes().getLength()

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
Is it possible to traverse the values of children of an element, and no need to specify the children's element names in XSLT element? The XML file can be <bio> <skills> <languages>...
1
by: Todd Cary | last post by:
In my previous post, I stated that some JavaScript I inherited does not work in Firefox but does work in IE 6 . I have now isolated a line where it fails in Firefox; the line with the property of...
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
17
by: eric.nave | last post by:
What is the correct way to find all the form elements in a particular div? I'd like to be able to loop through them and disable them. For example: <form> <div id="div1"> <input type=text...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
7
by: Thierry Lam | last post by:
I have the follownig codes: <html> <head> <title>Simple doc</title> </head> <body> <script type="text/javascript"> function greet() {
3
by: Andrej Pavlovic | last post by:
Hi, Please look at the following HTML snippet: <body> some text <p>more text</p> even more text </body>
1
by: matthewzammit | last post by:
I have an XML element <val> with child elements. I'd like to be able to include any of the child elements, or leave them out, without having to write default null values each time for unneeded...
3
by: Arndt Jonasson | last post by:
Let's say we have a schema (maybe expressed in XML Schema, but not necessarily so), that allows this instance document: <top> <txt>This is text</txt> <books> <book>Tarzan</book> <book>Harry...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.