Home » Developer & Programmer » JDeveloper, Java & XML » Number of node elements in XML (Oracle 10g R2, VMS)
Number of node elements in XML [message #320176] Wed, 14 May 2008 04:35 Go to next message
Seshagiri
Messages: 13
Registered: October 2007
Location: United Kingdom
Junior Member

Hi,

I would like to loop through an XML doc. For this I need to know the number of nodes present.

Is there a way of doing this using XPath ?

Regards,
Sesha
Re: Number of node elements in XML [message #320261 is a reply to message #320176] Wed, 14 May 2008 07:53 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Normally, using XPath and DOM etc, the whole idea is that you can traverse through a series of nodes _without_ having to know the number of nodes up front.
Re: Number of node elements in XML [message #320265 is a reply to message #320261] Wed, 14 May 2008 08:15 Go to previous messageGo to next message
Seshagiri
Messages: 13
Registered: October 2007
Location: United Kingdom
Junior Member

Hi,

Thank You. There is no DOM functionality in the DB i.e. XML DB is not installed.

Now I can only use XPath. Can you pls give me an example of traversing nodes.

Many Thanks
Sesha
Re: Number of node elements in XML [message #321153 is a reply to message #320176] Mon, 19 May 2008 04:33 Go to previous messageGo to next message
hobbes
Messages: 173
Registered: January 2006
Senior Member
An example using XPath, though I cannot see much business utility for this...

SQL> DECLARE
  2    x          XMLTYPE :=
  3               xmlType('<DATA>
  4                          <LINE>A</LINE>
  5                          <LINE>B</LINE>
  6                          <LINE>C</LINE>
  7                          <LINE>D</LINE>
  8                        </DATA>');
  9    CountNode  NUMBER(4);
 10  BEGIN
 11    SELECT COUNT(ve.EXTRACT('/*').GetStringval()) INTO CountNode
 12    FROM TABLE(xmlSequence(EXTRACT(x,'/DATA/LINE'))) ve;
 13    dbms_Output.Put_Line('No. of nodes: '
 14                         ||CountNode);
 15    FOR i IN 1..CountNode LOOP
 16      dbms_Output.Put_Line(i
 17                           ||': '
 18                           ||x.EXTRACT('/DATA/LINE['
 19                                       ||i
 20                                       ||']').GetStringval());
 21    END LOOP;
 22  END;
 23  /
No. of nodes: 4
1: <LINE>A</LINE>

2: <LINE>B</LINE>

3: <LINE>C</LINE>

4: <LINE>D</LINE>

PL/SQL procedure successfully completed.
Re: Number of node elements in XML [message #321169 is a reply to message #321153] Mon, 19 May 2008 05:45 Go to previous message
Michel Cadot
Messages: 68728
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Or in SQL:
SQL> select decode(rownum, 1, 'No. of nodes: '||count(*) over ()||'
  2  ') || rownum || ': ' || column_value val
  3  from table(xmlsequence(extract(xmltype(
  4  '<DATA>
  5  <LINE>A</LINE>
  6  <LINE>B</LINE>
  7  <LINE>C</LINE>
  8  <LINE>D</LINE>
  9  </DATA>'),
 10  '/DATA/LINE'))) t
 11  /
VAL
-------------------------------------------------------
No. of nodes: 4
1: <LINE>A</LINE>
2: <LINE>B</LINE>
3: <LINE>C</LINE>
4: <LINE>D</LINE>

4 rows selected.

Regards
Michel
Previous Topic: Toplink resets the time part of the date
Next Topic: About Creating Java Objects in Oracle
Goto Forum:
  


Current Time: Tue Dec 17 23:41:44 CST 2024