[jdom-interest] How to replace an Element with Text?

Brad Cox bcox at virtualschool.edu
Sun Mar 28 04:16:39 PST 2004

Thanks! That was exactly the problem. But the solution still eludes.

After rewriting like this: (no such class as Parent, and Element 
doesn't do indexOf()):

	    Element parent = e.getParent();
	    List siblings = parent.getChildren();
	    int index = siblings.indexOf(e);
             Text t = new Text("#radioElement('"+nameAttr+"' 
'"+valueAttr+"' '"+buf+"')");
	    siblings.set(index, t);

I get this error:
org.jdom.IllegalAddException: Filter won't allow index 0 to be set to 
org.jdom.Text at 
Looks like JDOM is onto this and is determined to stop it. Time for 
bigger hammers? ;)

At 10:25 PM -0600 3/27/04, Bradley S. Huffman wrote:
>Brad Cox writes:
>>  I've an XHTML processing application that supports persistent forms.
>>  It needs to rewrite some elements (<input type="radio"> and <option>
>>  elements, for example) as text elements (velocity macro calls that
>>  add checked="checked" attributes).
>>  For example
>>	<input type="radio" ... > must be rewritten as #radioElement(...)
>>	<option>Value</option>	must be rewritten as #optionElement(...)
>>  with suitable attributes and arguments of course.
>>  First try (iteration over the List returned by XPath) didn't work
>>  because you can only delete elements, not replace them.
>The list return from XPath isn't "live" so changes to it are not reflected
>back in the tree.
>>  Second try was index through list and use set(index, new Text(...))
>>  to replace elements. That approach didn't work either. Debugger
>>  showed that the list was modified propertly, but the changes didn't
>>  affect the underlying DOM, just the list.
>>  Can someone explain why #2 didn't work and what I can do to fix it? Thanks!
>Same reason. Try this, index throught the list and
>     Element element = (Element) xpath_result.get(i); // Assumes xpath returned
>                                                      // only Elements
>     Parent parent = element.getParent();             // get the 
>element's parent
>     int index = parent.indexOf(element);             // and element's index
>     parent.setContent(index, "#radioElement ...");   // replace it
>Go Pokes!!!
>To control your jdom-interest membership:

Brad J. Cox, PhD, 703 361 4751, http://virtualschool.edu
        http://virtualschool/ale Action Learning Environment
http://virtualschool.edu/mybank Digital Rights Management System
    http://virtualschool.edu/jco Java Cryptographic Objects (JCO)
   http://virtualschool.edu/jwaa Java Web Application Architecture (JWAA)
  http://virtualschool.edu/java+ Java Preprocessor (Java+)

More information about the jdom-interest mailing list