counting number of xml node in java(using Dom4j)

Here should go questions about transforming XML with XSLT and FOP.
sujatha
Posts: 12
Joined: Sun Oct 16, 2005 5:09 pm

counting number of xml node in java(using Dom4j)

Post by sujatha » Tue Dec 20, 2005 11:49 pm

I have the following XML file and I have to count the number of nodes in it,

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XMLStylesheet.xsl"?>
<ELG-MONITOR_record>
<_ID>DW*286112233</_ID>
<MEMBER_NAME_MV>
<MEMBER_NAME_MS>
<MEMBER_NAME>CARDAY,DWAYNE</MEMBER_NAME>
</MEMBER_NAME_MS>
</MEMBER_NAME_MV>
<PREP_DATE>04/11/04</PREP_DATE>
<PREP_TIME>12:56:44</PREP_TIME>
<ASSOC_3-MV>
<ELG_STATUS>E</ELG_STATUS>
<ELG_MONTHS>3</ELG_MONTHS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200307</LAG_PERIODS>
</ASSOC_3-MS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200308</LAG_PERIODS>
</ASSOC_3-MS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200309</LAG_PERIODS>
</ASSOC_3-MS>
</ASSOC_3-MV>
<ASSOC_3-MV>
<ELG_STATUS>N</ELG_STATUS>
<ELG_MONTHS>3</ELG_MONTHS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200307</LAG_PERIODS>
</ASSOC_3-MS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200308</LAG_PERIODS>
</ASSOC_3-MS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200309</LAG_PERIODS>
</ASSOC_3-MS>
</ASSOC_3-MV>
<ASSOC_3-MV>
<ELG_STATUS>E</ELG_STATUS>
<ELG_MONTHS>3</ELG_MONTHS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200307</LAG_PERIODS>
</ASSOC_3-MS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200308</LAG_PERIODS>
</ASSOC_3-MS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200309</LAG_PERIODS>
</ASSOC_3-MS>
</ASSOC_3-MV>
<ASSOC_3-MV>
<ELG_STATUS>E</ELG_STATUS>
<ELG_MONTHS>3</ELG_MONTHS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200308</LAG_PERIODS>
</ASSOC_3-MS>
<ASSOC_3-MS>
<ELG_STATUS_AFTER>YM1</ELG_STATUS_AFTER>
<LAG_PERIODS>200309</LAG_PERIODS>
</ASSOC_3-MS>
</ASSOC_3-MV>
</ELG-MONITOR_record>

I need to coutn how many number of ASSOC_3-MV are there in java using any methodology to loop inside their elements. Let me know about it.

Thanks, in advance

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Wed Dec 21, 2005 10:50 am

Hi,

I do nor know the dom4j API.
If you use an XPath API as what Saxon or Xalan provides then you can just use an XPath expression like //ASSOC_3-MV for instance. If you use DOM I remember there is a method that sounds like getElementByTagName that you can use.

Best Regards,
George

sujatha
Posts: 12
Joined: Sun Oct 16, 2005 5:09 pm

Re: Count of nodes

Post by sujatha » Thu Dec 22, 2005 7:09 pm

Can you specifically mention how to code for that because when I coded to get the number of nodes, I am always getting error. And I would like to mention that I am doing this in a java program to get the count of the nodes.

Thanks,
Sujatha.

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Fri Dec 23, 2005 10:54 am

Hi Sujatha,

Just calling document.getElementsByTagName("test") should get you all the test elements in the document.

Best Regards,
George

Post Reply