<html>
<body>
Sounds like it would be easier to use JSP with custom tags, if your
environment permits.<br><br>
At 2006-05-06 01:14, Leonardo Barbosa wrote:<br>
<blockquote type=cite class=cite cite="">First of all, thanks everybody
for your help!<br>
And sorry about the reply for your email, I didn't see it.<br><br>
OK, here is an example of my template:<br><br>
... <br>
<a href="$LINK.htm">$LINK </a>.. <br>
So, as you can see, I have <brtpchamada>, <brtpolho> and
<brtptitulo><br>
proprietary tags. These are responsable to map where and what kind
of<br>
data should be there.<br>
I use to have a <brtplink> to indicate where where I should create
the<br>
<a href="">. But now I need to worry about javascript's,
inside every<br>
tag (<img>, <a>, etc), and in the text of all elements.
Actually, all<br>
elements that are inside the <brtpchamada> tag. Thats where come
the<br>
context problem: the $LINK in the <brtpchamada> with
id="1" is<br>
diferente from the $LINK in the <brtpchamada> with
id="2". So, to<br>
create the replacement list, I do this:<br><br>
HashMap replacement = new HashMap();<br><br>
Iterator chamadaIterator = root.getDescendants(new<br>
ElementFilter("brtpchamada"));<br>
while (chamadaIterator.hasNext()) {<br>
// read stuf from the database for this chamada, with
id = 1 or id<br>
= 2 for this example<br><br>
// ElementStartsFilter is a new filter that I created
to filter,<br>
as the name says, only<br>
// elements that starts with "brtp".<br>
Iterator filhosIterator =
chamadaTagAtual.getDescendants(new<br>
ElementStartsFilter("brtp"));<br>
while (filhosIterator.hasNext()) {<br>
Element filhoTagChamadaAtual =
(Element) filhosIterator.next();<br>
Element conteudoTag = new
Element("abc");<br><br>
// do some processing here,
acording to this <chamada> tag,<br>
and modify conteudoTag<br><br>
replacement.put(filhoTagChamadaAtual, conteudoTag);<br>
}<br>
}<br><br>
// This funcion replaces the key of the hashmap for the value in the
document<br>
replaceAllTags(replacement);<br><br>
Now is the trick part: for every content inside the <chamada> tag,
I<br>
need to replace $LINK for a SPECIFIC url to this <brtpchamada> (id
= 1<br>
or id = 2 in this template that I used for example).<br><br>
Now, how can I do that? Or am I freaking out the porpouse of
getDescendents? :-)<br><br>
After Thomas reply, I thought about to do somenthing like this (of<br>
course I will need to learn the java.util.regex.* first ;-)<br><br>
String document = LoadFile("document.tpl");<br><br>
// supose for a minute that its like perl :-)<br>
while(document.matches("<brtpchamada>(.+)</brtpchamada>")){<br>
String content = document.lastMatch();<br>
content.replaceAll("<brtptitulo\>",
getDataForThis());<br>
content.replaceAll("LINK",
getLinkForThis());<br>
// etc<br>
document.replaceAll("<brtpchamada>(.+)</brtpchamada>",
contet);<br>
}<br><br>
but the problem here is that I would need to grep all atributes, to<br>
get <brtpchamada id="1"> id for example, replace empty
tags or not<br>
empty tags (<brtptitulo/> could be
<brtptitulo>...</brtptitulo> for<br>
example), and do all that stuff that I think jdom does much better<br>
then I do...<br><br>
I hope I was more clear at this time.<br><br>
Best reagards and thanks again,<br>
Leonardo Barbosa<br><br>
On 5/5/06, Edelson, Justin <Justin.Edelson@mtvn.com> wrote:<br>
><br>
> I was actually suggesting (as did Thomas) to not use JDOM at all and
just<br>
> use regex across the whole document. But now you're saying that
the<br>
> replacement text is somehow related to the context within the XML
document.<br>
><br>
> Can you provide some more detail?<br>
><br>
> And please respond to the list, not to me individually.<br>
><br>
> Justin<br>
><br>
> ________________________________<br>
> From: Leonardo Barbosa
[<a href="mailto:lsbarbosa@gmail.com" eudora="autourl">
mailto:lsbarbosa@gmail.com</a>]<br>
> Sent: Friday, May 05, 2006 4:38 PM<br>
> To: Edelson, Justin<br>
> Subject: Re: [jdom-interest] Text replacement<br>
><br>
><br>
> Thanks Justin!<br>
><br>
> Infact, the problem with creating the string and using replaceAll is
that I<br>
> don't have the output ready at this time. I'm iterating over the
<chamada><br>
> tags and, for each one, I need to replace all ocurrences of
$REPLACE_THIS<br>
> with a spacific value, for that <chamada> tag. To be more
specific, each<br>
> <chamada> tag has a url associated with it, according to it's
position in<br>
> the XML.<br>
><br>
> So, to replace using replaceAll, wouldnt I need to create an String
out<br>
> using XMLOutputter for each <chamada> tag, and them use the
SAXParser to<br>
> create the JDOM model again, and atach this new JDOM model to the
old one?<br>
><br>
> I mean, I can't reaplace in the and, when I finish processing the
document,<br>
> beacause its the same string $REPLACE_ALL with diferent values.. I
could ask<br>
> the client to use diferente names, but i could generate errors, I
upset him<br>
> :-)<br>
><br>
> Thanks again for your help,<br>
> Leonardo Barbosa.<br>
><br>
><br>
> On 5/5/06, Edelson, Justin <Justin.Edelson@mtvn.com >
wrote:<br>
> ><br>
> ><br>
> > I'd first look at whether or not you need ANY kind of XML
processing for<br>
> this. If all you are doing is replacing $REPLACE_THIS with some
string, then<br>
> you could just get the template into a String object and do<br>
> > string.replaceAll("\$REPLACE_THIS",
replacementString);<br>
> > (because replaceAll expects a RegEx, you need to escape the
dollar sign)<br>
> ><br>
> > Doing this wouldn't even require "tidying" up the
doc.<br>
> ><br>
> ><br>
> > ________________________________<br>
> From: jdom-interest-bounces@jdom.org<br>
>
[<a href="mailto:jdom-interest-bounces@jdom.org" eudora="autourl">
mailto:jdom-interest-bounces@jdom.org</a>] On Behalf Of Leonardo
Barbosa<br>
> > Sent: Friday, May 05, 2006 2:10 PM<br>
> > To: jdom-interest@jdom.org<br>
> > Subject: [jdom-interest] Text replacement<br>
> ><br>
> ><br>
> ><br>
> > Hi!<br>
> ><br>
> > I'm new to jdom and xml, so sorry if this is non sense.<br>
> > I read a xml template (a html to be true, transformed to XML
using tidy)<br>
> file from my client, and want to change all ocurrences of one text
to<br>
> something else. It will probably be inside some javascript,
something like<br>
> > <a href="#"<br>
> onclick="javascript:openLink($REPLACE_THIS);"><br>
> ><br>
> > bu could be in the text of some <script> tag for
example:<br>
> > <script><br>
> > funcion onclick(){<br>
> >
opeanUrl("REPLACE_THIS");<br>
> > }<br>
> > </script><br>
> ><br>
> > There only one more thing: It needs to be inside some tag, the
one that<br>
> defines the link url:<br>
> ><br>
> > <chamada><br>
> > 1 <a
href="#"<br>
> onclick="openLink($REPLACE_THIS);">click
here</a><br>
> > </chamada><br>
> ><br>
> > <chamada><br>
> > 2 <a
href="#"<br>
>
onclick="openLink($REPLACE_THIS);">$REPLACE_THIS</a><br>
> > </chamada><br>
> ><br>
> ><br>
> > Is there a way to do this with jdom API, without getting all
Descendents<br>
> from the tag <chamada> and looking inside its Text and
Attributes ?<br>
> > something like:<br>
> ><br>
> > Iterator
chamadaIterator = root.getDescendants (new<br>
> ElementFilter("chamada"));<br>
> > while
(chamadaIterator.hasNext()) {<br>
>
>
Element chamadaTag = (Element) chamadaIterator.next();<br>
>
>
chamadaTag.setTextReplacement("$REPLACE_THIS", "<br>
>
<a href="http://www.thinkgeek.org/" eudora="autourl">
http://www.thinkgeek.org</a>");<br>
> > }<br>
> ><br>
> > I've searched the forum and found some ideas aboute html
entities, but it<br>
> seams to be not the case, since I have one value for the whole
document.<br>
> ><br>
> > I no that it seeas to be lazyness :-)<br>
> > But in fact I'm trying to avoid some desnecessary loop inside
all<br>
> elements, all texts from theese elements, and all attributes from
theese<br>
> elements.<br>
> ><br>
> > Thanks in advance,<br>
> > Leonardo Barbosa<br>
> > lsbarbosa@gmail.com</blockquote></body>
</html>