[jdom-interest] getElementsByTagName + insertBefore

Dennis Sosnoski dms at sosnoski.com
Thu Feb 28 09:59:03 PST 2002


You can also take a look at the JDOM code sample in my IBM devWorks 
article at: 
http://www-106.ibm.com/developerworks/xml/library/x-injava2/?dwzone=xml. 
The sample code in that article is doing a similar operation with text 
(replacing a content text string with an element that has the text as 
content), so it gives a pretty good basis for what you want to do.

  - Dennis

Bart Read wrote:

> 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 <mailto:bart at wdi.co.uk>
> =================================
>
>     ----- Original Message -----
>
>     * From:* Cédric Dumetz <mailto:cedric at eolas.fr>
>
>     * To:* jdom-interest at jdom.org <mailto: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
>





More information about the jdom-interest mailing list