Bug 27864 - Wrong number of entries returned for getElementsByTagName() in special case
Summary: Wrong number of entries returned for getElementsByTagName() in special case
Status: RESOLVED FIXED
Alias: None
Product: classpath
Classification: Unclassified
Component: xml (show other bugs)
Version: 0.92
: P3 normal
Target Milestone: 0.92
Assignee: Robert Schuster
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-06-01 16:21 UTC by Robert Schuster
Modified: 2006-08-04 00:11 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
xml file which is not parsed correctly (82 bytes, text/plain)
2006-06-01 16:22 UTC, Robert Schuster
Details
a beanshell script which demonstrates the problem (675 bytes, text/plain)
2006-06-01 16:26 UTC, Robert Schuster
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Schuster 2006-06-01 16:21:25 UTC
Consider this piece of XML:

<main>
        <element>
                <alwaysThere/>
        </element>
        <element>
                <alwaysThere>
                <sometimesThere>foo</sometimesThere>
                </alwaysThere>
        </element>
        <element>
                <alwaysThere>
                <sometimesThere>bar</sometimesThere>
                </alwaysThere>
        </element>
</main>

Reach the <alwaysThere> tag inside the first <element> tag:

doc = DocumentBuildFactory.blafoobaz.parse(theStuffFromAbove);
elem0 = doc.getElementsByTagName("element").item(0);
at = elem0.getElementsByTagName("alwaysThere").item(0);

Now retrieve the children of this element which have the tagname 'sometimesThere':

st_nodes = at.getElementsByTagName("sometimesThere");

Although there are no <sometimesThere> children in the <alwaysThere> element the list says, there are:

st_nodes.getLength() will return 2.
st_nodes.item(0) and st_nodes().item(1) will turn out to be the <sometimesThere> elements inside the 2nd and 3rd <element> tag.

By investigating this problem I found out that the reason lies inside the gnu.xml.dom.DomIterator which does not properly stop iterating (forward) when the root element is exceeded.

I will provide a mauve test and probably have a fix for it, too.
Comment 1 Robert Schuster 2006-06-01 16:22:16 UTC
Created attachment 11572 [details]
xml file which is not parsed correctly
Comment 2 Robert Schuster 2006-06-01 16:26:02 UTC
Created attachment 11573 [details]
a beanshell script which demonstrates the problem

This beanshell scripts demonstrates the problem. Put the script and the XML file in one directory and run it like this:

$VM -cp $PATH_TO/bsh.jar bsh.Interpreter
< type source("testxml"); >
< type wrongo(); >

On GNU Classpath it will output:

<element>-tag[0] has 2 <sometimesThere> child elements.
<element>-tag[1] has 1 <sometimesThere> child elements.
<element>-tag[2] has 1 <sometimesThere> child elements.

On the RI it is:

<element>-tag[0] has 0 <sometimesThere> child elements.
<element>-tag[1] has 1 <sometimesThere> child elements.
<element>-tag[2] has 1 <sometimesThere> child elements.