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