You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Wolfhart Bauer <ba...@netlife.com.sg> on 2000/02/23 06:42:27 UTC

[BUG] Behavior of NodeList implementation in 1.0.2


Hello,

I have discovered a bug in the class org.apache.xerces.dom.DeepNodeListImpl. The
attached code demonstrates it, just try switching the order of the two println
lines. It seems that you have to call the getLength() method at least once
before accessing any of the items directly. I don't feel that this behavior is
intentional.

public class XMLTest
{
public static void main (String[] args)
throws Exception
  {
    org.apache.xerces.parsers.DOMParser parser = new
org.apache.xerces.parsers.DOMParser();
    parser.parse("file:///d:/testing/xercestest/test.xml");
    org.w3c.dom.Node d = parser.getDocument();
    org.w3c.dom.NodeList nl = d.getChildNodes();
    System.out.println("Node 0 is an element?
"+(nl.item(0).getNodeType()==org.w3c.dom.Node.ELEMENT_NODE));
    System.out.println("Length of nodelist: "+nl.getLength());
  }
}

Executed on the file test.xml:
<?xml version="1.0"?>
<!DOCTYPE TEST SYSTEM "test.dtd">
<TEST/>

with the DTD test.dtd:
<!ELEMENT TEST EMPTY>

By the way, thanks to all developers for the good work with this parser.

Wolfhart Bauer



Re: [BUG] Behavior of NodeList implementation in 1.0.2

Posted by Arnaud Le Hors <le...@us.ibm.com>.
Wolfhart Bauer wrote:
> 
> I have discovered a bug in the class org.apache.xerces.dom.DeepNodeListImpl. The
> attached code demonstrates it, just try switching the order of the two println
> lines. It seems that you have to call the getLength() method at least once
> before accessing any of the items directly. I don't feel that this behavior is
> intentional.
> 
> public class XMLTest
> {
> public static void main (String[] args)
> throws Exception
>   {
>     org.apache.xerces.parsers.DOMParser parser = new
> org.apache.xerces.parsers.DOMParser();
>     parser.parse("file:///d:/testing/xercestest/test.xml");
>     org.w3c.dom.Node d = parser.getDocument();
>     org.w3c.dom.NodeList nl = d.getChildNodes();
>     System.out.println("Node 0 is an element?
> "+(nl.item(0).getNodeType()==org.w3c.dom.Node.ELEMENT_NODE));
>     System.out.println("Length of nodelist: "+nl.getLength());
>   }
> }
> 
> Executed on the file test.xml:
> <?xml version="1.0"?>
> <!DOCTYPE TEST SYSTEM "test.dtd">
> <TEST/>

First, this code does not actually use the DeepNodeListImpl class at
all. getChildNodes() is implemented by the NodeContainer class.
Second, the first node of your document is not an element. It's a
doctype.
This can easily be seen in changing your first test with the following:

System.out.println("Node 0 is a doctype? "
   +(nl.item(0).getNodeType()==org.w3c.dom.Node.DOCUMENT_TYPE_NODE));
System.out.println("Node 1 is an element? "
   +(nl.item(1).getNodeType()==org.w3c.dom.Node.ELEMENT_NODE));

This said, you're right about getLength(). The cache is wrong. The patch
is attached.
-- 
Arnaud  Le Hors - IBM Cupertino, XML Technology Group