[jdom-interest] Exception when creating a document from arraylist

Bradley S. Huffman hip at a.cs.okstate.edu
Sun Jun 12 10:25:18 PDT 2005


"X.P.Pratibha :-)" writes:

> This is a multi-part message in MIME format.
> --===============1125767959==
> Content-Type: multipart/alternative;
> 	boundary="------------000501060705090303010009"
> 
> This is a multi-part message in MIME format.
> --------------000501060705090303010009
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> Content-Transfer-Encoding: 7bit
> 
> Hi all,
>        My problem is to edit an xml document to remove the duplicate 
> entries it has before giving it to the XSL for display....
> Here goes my logic:
>  1)Get the document.
> 2) traverse it for duplicate entries and put all the unique entries into 
> an array list
> 3) Then create the new document with elements in arraylist.
> 4)I am creating the root element and subroot element and adding the each 
> element in my array list...Here I am getting the exception
> "*element* *already* *has* an existing *parent FWRules" 
> --IllegalADDException.* 
> <http://www.jdom.org/pipermail/jdom-interest/2001-October/007851.html>
> 
> P.S :The elements in the list has elements which in turn has so many 
> sub-elements...
> 
> Can u please help me what I am doing wrong ???How to convert  a list to 
> Document again...I am using JDK1.3.1
>         
>                     Element Oldelem = 
> doc.getRootElement().getChild("Success").getChild("Rules");       
>                
>                          ListIterator OldIter = 
> Oldelem.getChild("FWRules").getChildren("ConRule").listIterator();
>                          Vector ID_ref = new Vector();
>                          ArrayList new_list = new ArrayList();
>                          int count=0;
>                          while(OldIter.hasNext())
>                          {
>                              Element OldChildElem=((Element) 
> OldIter.next());
>                              Element Attri = 
> OldChildElem.getChild("FWRule");
>                              String ID=Attri.getAttributeValue("id");   
>                                                     
>                              count++;
>                              if(count<=1)
>                              {                               
>                                  ID_ref.addElement(ID);                 
>               
>                                  new_list.add(OldChildElem);  

OldChildElem is still attached to it's orignal parent.

>                       
> 
>                              }
>                              else if(!ID.equals(ID_ref.elementAt(0)))
>                              {                               
>                                  ID_ref.remove(0);
>                                  ID_ref.addElement(ID);
>                                  new_list.add(OldChildElem);           
>                 
> 
>                              }
>                          }
> 
>                         Document Doc_New = new Document(new 
> Element("Rules"));               
>                         Element sub-elem =new Element("FWRules");      
>                                                    
>                         for(int l=0;l<new_list.size();l++)
>                         {                      
>                            Element element_In_list = 
> ((Element)new_list.get(l));
>                            XMLOutputter output =new XMLOutputter();
>                            
> System.out.println("XMLOutputter:"+output.outputString(output));
>                            *sub-elem.addContent(elment_In_list); //HERE 
> IS where I get the exception*
>                        }
>                          Doc_New.getRootElement().addContent(subroot);
> 
>                      }
> 
> --------------000501060705090303010009
> Content-Type: text/html; charset=ISO-8859-1
> Content-Transfer-Encoding: 7bit
> 
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
>   <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
> </head>
> <body bgcolor="#ffffff" text="#000099">
> Hi all,<br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; My problem is to edit an xml document to
>  remove the duplicate
> entries it has before giving it to the XSL for display....<br>
> Here goes my logic:<br>
> &nbsp;1)Get the document.<br>
> 2) traverse it for duplicate entries and put all the unique entries
> into an array list<br>
> 3) Then create the new document with elements in arraylist.<br>
> 4)I am creating the root element and subroot element and adding the
> each element in my array list...Here I am getting the exception <br>
> "<a onmousedown="return clk(this,'res',1,'')"
>  href="http://www.jdom.org/pipermail/jdom-interest/2001-October/007851.html">
> <b>element</b>
> <b>already</b> <b>has</b> an existing <b>parent FWRules"
> --IllegalADDException.</b></a><br>
> <br>
> P.S :The elements in the list has elements which in turn has so many
> sub-elements...<br>
> <br>
> Can u please help me what I am doing wrong ???How to convert&nbsp; a list to
> Document again...I am using JDK1.3.1<br>
> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <br>
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Element
>  Oldelem =
> doc.getRootElement().getChild("Success").getChild("Rules");&nbsp;&nbsp; &nbsp
> ;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
> ;&nbsp;&nbsp; <br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
> sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ListIterator OldIte
> r =
> Oldelem.getChild("FWRules").getChildren("ConRule").listIterator();<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
> sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vector ID_ref = new
>  Vector();<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
> sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList new_list 
> = new ArrayList();<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
> sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int count=0;<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
> sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(OldIter.hasNe
> xt())<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
> sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Element OldChildElem
> =((Element)
> OldIter.next());<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Element Attri =
> OldChildElem.getChild("FWRule");<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String ID=Attri.getA
> ttributeValue("id");&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&
> nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &
> nbsp;&nbsp; <br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; count++;<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(count&lt;=1)<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&n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<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if(!ID.equals(I
> D_ref.elementAt(0)))<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp; &nbsp;
> &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; I
> D_ref.remove(0);<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; I
> D_ref.addElement(ID);<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; n
> ew_list.add(OldChildElem); &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&n
> bsp; <br>
> <br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
> <br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; Document Doc_New = new Document(new
> Element("Rules"));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
> &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Element sub-elem =new Element("FWRules");&nbs
> p; &nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;
> &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
> &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&
> nbsp; <br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; for(int l=0;l&lt;new_list.size();l++)<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
> bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
> nbsp;&nbsp; <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
> p; Element element_In_list =
> ((Element)new_list.get(l));<br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
> p; XMLOutputter output =new XMLOutputter();<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&
> nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> System.out.println("XMLOutputter:"+output.outputString(output));<br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
> ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
> p;<font color="#cc0000"> <big><b>sub-elem.addContent(elment_In_list);
> //HERE IS where I get the exception</b></big></font><br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&
> nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Doc_New.getRootElement().addContent(sub
> root);<br>
> <br>
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
> &nbsp;&nbsp;&nbsp; }
> </body>
> </html>
> 
> --------------000501060705090303010009--
> 
> --===============1125767959==
> Content-Type: text/plain; charset="us-ascii"
> MIME-Version: 1.0
> Content-Transfer-Encoding: 7bit
> Content-Disposition: inline
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
> --===============1125767959==--


More information about the jdom-interest mailing list