[jdom-interest] getElementsByTagName + insertBefore

Bart Read bart at wdi.co.uk
Thu Feb 28 09:04:21 PST 2002

Right, stop and think about this.  You're not adding <a href="whatever"> before your <img> element because the <a> element will ENCLOSE <img> like so:

<a href="whatever"><img src="somethingelse"/></a>

See?  So <a> becomes the parent of <img> so you need to remove <img> from the list and replace it with <a> and add <img> as a child element of <a>.  Seriously, check out the documentation and read Java and XML by Brett McLaughlin and Jason Hunter -- it won't take long and it'll save you a shedload of time in the long run. ;-)

Bart Read
Senior Developer
Abbotsbury Software Ltd
Abbotsbury, DORSET DT3 4JT
Tel: +44 (0) 1305 871543
E-mail: bart at wdi.co.uk
  ----- Original Message ----- 
  From: Cédric Dumetz 
  To: jdom-interest at jdom.org 
  Sent: Thursday, February 28, 2002 4:06 PM
  Subject: RE: [jdom-interest] getElementsByTagName + insertBefore

  Sorry for my ignorance

  I finally do a function like this

  private static List getElementsByTagName(String name, Element elem) {
   Vector v = new Vector(elem.getChildren(name));
   for (Iterator it=elem.getChildren().iterator(); it.hasNext(); ) {
    v.addAll(getElementsByTagName(name, (Element)it.next()));
   return v;

  and in my code I do this call :
      Element root = doc.getRootElement();
      List myIMG = getElementsByTagName("IMG", root)

  Now I have another problem : how to insert an Element before another because I would like to insert a <a href> tag before each <img> tag (I found all the <img> tags with my method 'getElementsByTagName')


---------- Automatically inserted by WDI Mailserver ----------------
This message has been scanned for virus, trojans, illegal relays and
spam. If you have any comments, problems or questions please do not
hesitate to contact:
Abbotsbury Software Ltd
Tel: +44 1305 871543 Fax: +44 1305 871688
Email: postmaster at wdi.co.uk
Web: http://www.wdi.co.uk/virus.phtml

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20020228/36f63e70/attachment.htm

More information about the jdom-interest mailing list