[jdom-interest] Replace elements in a document

Sebastian Schirmer schirmer at sitewaerts.de
Mon Aug 20 11:05:56 PDT 2001


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
>>>>>>>>>>>>>>>>>>>>>>>>>>>





More information about the jdom-interest mailing list