I've fixed this temporarily in CVS by throwing an UnsupportedOperationException when the remove() operation is called on the Iterator returned by the Element.elementIterator(...) methods. This Iterator was an instance of FilterIterator (which uses another Iterator and filters away unwanted elements). I did this because I didn't find a way to easily solve the problem. I also believe it's better to not support the (optional) remove() operation than to provide a buggy implementation. Remark that this doesn't violate the Iterator specification because the remove() operation is optional and doesn't have to be supported by implementations.

For what it's wort: the Jakarta Collections API also provides a FilterIterator which has the same purpose and there the remove() operation isn't supported neither. See http://jakarta.apache.org/commons/collections/api/index.html for more details...

regards,
Maarten

At 14:00 17/10/2002 +0100, Carlos Barroso wrote:
There may be a bug in the "elementIterator()" method of the classe
"Element".
The contents of my test and results are below which were sent to the list
hours ago.



Hy guys.
I'm having a weird problem in removing a certain element.
My XML doc is ("teste.xml"):

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE CLIENTES SYSTEM 'teste.dtd'>

<CLIENTES>
  <CLIENTE id='1'>
    <NOME valor='Mike'/>
    <MORADA valor='xxx'/>
  </CLIENTE>
  <CLIENTE id='2'>
    <NOME valor='Neca'/>
    <MORADA valor='Aveiro'/>
  </CLIENTE>
</CLIENTES>

---------------------------------
And I have a class like:
---------------------------------

import java.util.Iterator;

import java.net.MalformedURLException;

import java.io.*;

import org.dom4j.*;
import org.dom4j.io.*;

public class Teste {
  public static void main(String[] args) {
    Document doc = null;

    try {
      SAXReader reader = new SAXReader();
      doc = reader.read(new File("teste.xml"));
    } catch(MalformedURLException e) {
      System.out.println(e.getMessage());

    } catch(DocumentException e) {
      System.out.println(e.getMessage());
    }

    Element root = doc.getRootElement();

    for(Iterator i = root.elementIterator(); i.hasNext();) {
      Element elem = (Element)i.next();
      for(Iterator j = elem.attributeIterator(); j.hasNext();) {
        Attribute att = (Attribute)j.next();
        if(att.getValue().equals("1")) {
          i.remove();
        }
      }
    }

    try {
      // Pretty print the document
      OutputFormat format = OutputFormat.createPrettyPrint();
      XMLWriter writer = new XMLWriter(System.out, format);
      writer.write(doc);
      writer.close();

    } catch(IOException e) {
      System.out.println(e.getMessage());
    }
  }
}

---------------------------------
And the weird result is:
---------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CLIENTES SYSTEM "teste.dtd">

<CLIENTES>
  <CLIENTE id="1">
    <NOME valor="Mike"/>
    <MORADA valor="xxx"/>
  </CLIENTE>
</CLIENTES>

If I'm trying to remove a "CLIENTE" with attribute "id" equals to "1", why
is it the output!?!?
Please help.


-------------------------------------------------------
This sf.net email is sponsored by: viaVerio will pay you up to
$1,000 for every account that you consolidate with us.
http://ad.doubleclick.net/clk;4749864;7604308;v?
http://www.viaverio.com/consolidator/osdn.cfm
_______________________________________________
dom4j-dev mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-dev


-------------------------------------------------------
This sf.net email is sponsored by: viaVerio will pay you up to
$1,000 for every account that you consolidate with us.
http://ad.doubleclick.net/clk;4749864;7604308;v?
http://www.viaverio.com/consolidator/osdn.cfm
_______________________________________________
dom4j-dev mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-dev




-------------------------------------------------------
This sf.net email is sponsored by: viaVerio will pay you up to
$1,000 for every account that you consolidate with us.
http://ad.doubleclick.net/clk;4749864;7604308;v?
http://www.viaverio.com/consolidator/osdn.cfm
_______________________________________________
dom4j-dev mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-dev

Reply via email to