[jdom-interest] Replace elements in a document

Todd O'Bryan toddobryan at mac.com
Mon Aug 20 12:07:33 PDT 2001


Try a recursive approach:

// replaces all <a> elements with <link> elements below the Element 
passed in
void replaceAsWithLinks(Element e) {
	//replace this element if needed
	if (e.getName().equals("a")) {
		e.setName("link");
	}
	//now check its children
	List l = e.getChildren();
	for (int i=0; i<l.getSize(); i++) {
		if (l.get(i) instanceof Element) {
			replaceAsWithLinks( (Element) l.get(i));
		}
	}
}

This should work even if you have nested <a> tags. I didn't have a 
chance to compile and
test it, however, so let me know if it doesn't work.


Todd

	
On Monday, August 20, 2001, at 11:05 AM, Sebastian Schirmer wrote:

> Hello!
>
> I have the following problem:
>
> I have a XML Document that looks like this:
>
> --- snip ---
>
> <element schematype="text">
> 	Der folgende Textblock hat eine größere Menge Fließtext.
> 	<p>
> 	Aber nicht nur in Bezug auf Design eröffnet <a
> href="http//www.google.com">&lt;sitewaerts&gt;</a> neue
>       Möglichkeiten. War es bisher noch ausreichend, überhaupt im Web
> präsent
>       zu sein, so nimmt die Bedeutung einer lokalisierten Ansprache 
> Ihrer
>       Kunden eine immer wichtigere Bedeutung ein. Nur über eine
> Onlinepräsenz
>       in der Sprache Ihrer Kunden zeigen Sie ein echtes Commitment zu 
> neuen
>       Märkten.</p>
> </element>
>
> --- snip ---
>
>
>
> Now I want to replace the <a> elements with <link> elements using JDom. 
> Till
> now I tried it this way:
>
> I used a stack as a buffer for all elements and initialized it with the
> <element> element of the above document.
> Then I tested each element from the stack if it was a <a> element and
> replaced it if yes. Otherwise I added the children of that element to 
> the
> stack using getContent() and so on:
>
>
>
>
> --- snip ---
>
> stack.add(elElement);
>
> while (!stack.isEmpty())
> {
>   Element elFromStack = (Element)stack.pop();
>   if (elFromStack.getName().equals("a"))
>   {
>     elFromStack.setName("link");
>   }
>   else
>   {
>     stack.addAll(elFromStack.getContent());
>   }
> }
>
> --- snip ---
>
>
>
>
> Only if an <a> element comes directly within the <element> element,
> everything is executed correctly:
>   <element>
>     <a>link text goes here</a>
>   </element>
>
>
> The problem is if getContent supplies text first. Then nothing is 
> inserted
> in the <element> element:
>   <element>
>     some text goes here <a>link text goes here</a>
>   </element>
>
>
> I also tried to use getChildren() instead of getContent() but the 
> problem
> stays.
>
>
> Has Anybody an idea how to solve this without using XSLT?
>
>
>
>
> Sebastian Schirmer
>
> <<<<<<<<<<<<<<<<<<<<<<<<<<<
> sitewaerts GmbH
> Hebelstraße 15
> D-76133 Karlsruhe
>
> Tel: +49 (721) 920 918 0
> Fax: +49 (721) 920 918 29
> http://www.sitewaerts.de
>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-
> interest/youraddr at yourhost.com




More information about the jdom-interest mailing list