[jdom-interest] Can I use JDOM here?
Jon Baer
jonbaer at digitalanywhere.com
Wed Feb 7 00:43:41 PST 2001
I have a special occasion where I need to replace certain tags
("get_variable") with database info and <script> gets evaluated as
well. There doesnt seem to be anything like a Element.getTextTokens()
to iterate through an Elements text, but Im not sure if I should be
using JDOM to being with or should write my own SAXHandler, below is
some basic test code. Is it possible to replace the actual <get_name />
text inline with building a JDOM document? Are there possibly any
toStrings() I could overwrite maybe to do the logic?
- Jon
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
public class ParserTest
{
public static void main(String[] args) {
try {
System.out.println("--- Parse Begin ---");
SAXBuilder builder = new SAXBuilder();
String test = "<template>This is <get_name
/><set_he>Jon</set_he><set_topic>Books</set_topic><think><set_location>New
York</set_location></think> and he is cool and his dog <dog_name
/><script>getTime()</script></template>";
Document doc = builder.build(new StringReader(test));
Element template = doc.getRootElement();
StringBuffer resultBuffer = new StringBuffer(template.getTextTrim());
List tags = template.getChildren();
Iterator i = tags.iterator();
while (i.hasNext()) {
Element e = (Element)i.next();
String n = e.getName();
StringTokenizer st = new StringTokenizer(n, "_");
System.out.println("tag: " + e.getName());
System.out.println("action: " + st.nextToken());
if (st.countTokens() > 0) System.out.println("property: " +
st.nextToken());
System.out.println("content/function: " + e.getTextTrim());
}
System.out.println("--- Parser Results ---");
System.out.println("Original text: " + template.getTextTrim());
System.out.println("Result text: " + resultBuffer.toString());
} catch (Exception e) {
System.out.println(e);
}
}
}
More information about the jdom-interest
mailing list