sax find and replace

J

joe

Here is what I want to do go through an xhtml look for my custom tags
and call methods based on the tag names and take the output from the
tag and put it into the xhtml.

Take the following xhtml:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmls:nx="my.test.namespace">
<head>
<title>simple document</title>
</head>
<body>
<p>some paragraph
<nx:test name="joe"/>
</p>
</body>
</html>

and turn it into this:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmls:nx="my.test.namespace">
<head>
<title>simple document</title>
</head>
<body>
<p>some paragraph
Hello my name is joe
</p>
</body>
</html>


Now I know that I can use SAX and a ContentHandler to find my custom
tags and call the methods that is the easy part. I guess my question is
with a ContentHandler how would re-write the rest of the document ?
 
B

Bryce

Here is what I want to do go through an xhtml look for my custom tags
and call methods based on the tag names and take the output from the
tag and put it into the xhtml.

You can also take a look at the XPath API. To find:
<nx:test name="joe"/>

just use //nx:test[@name='joe'] or such.

Or, you can look at the Velocity Templating Engine:
http://jakarta.apache.org/velocity/index.html
 
B

Bryce

Here is what I want to do go through an xhtml look for my custom tags
and call methods based on the tag names and take the output from the
tag and put it into the xhtml.

I forgot, XSLT would do it nicely as well.
 
J

joe

I don't thnk XPath is quite what I want. I took a look a Velocity its
closer to what I want.

Maybe I didn't explain my problem well enough.

In my ContentHandler when I come across a nx namespace tag I will look
up the corresponding java Class and call an execute method. That method
may goto a db and look stuff based on the parameters in the tag,
perform biz function, etc... I know how to do that.

What I don't know is how to collect the rest of the HTML around my nx
tags and also add that to the output.
 
B

Bryce

I don't thnk XPath is quite what I want. I took a look a Velocity its
closer to what I want.

Maybe I didn't explain my problem well enough.

In my ContentHandler when I come across a nx namespace tag I will look
up the corresponding java Class and call an execute method. That method
may goto a db and look stuff based on the parameters in the tag,
perform biz function, etc... I know how to do that.

Well, Velocity is definitely a good option.

You can also use JSP. Your HTML would look like this:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<html xmlns="http://www.w3.org/1999/xhtml"
xmls:nx="my.test.namespace">
<head>
<title>simple document</title>
</head>
<body>
<p>some paragraph
Hello my name is <c:eek:ut value="joe"/>
</p>
</body>
</html>

Assuming you are using JSTL.

If you wanted to have the value dynamic, you could write a servlet
that gets the value from the database, write it to the request or
session and then the line above would read:

Hello my name is <c:eek:ut value="${name}"/>

or less verbose:

Hello my name is ${name}
 
J

John C. Bollinger

joe said:
Here is what I want to do go through an xhtml look for my custom tags
and call methods based on the tag names and take the output from the
tag and put it into the xhtml.
[...]

Now I know that I can use SAX and a ContentHandler to find my custom
tags and call the methods that is the easy part. I guess my question is
with a ContentHandler how would re-write the rest of the document ?

The basic idea here would be that every callback to your ContentHandler
should cause appropriate output to be generated to an output stream,
which would contain the modified document. Wherever your ContentHandler
is processing your custom tag it would output your custom result;
elsewhere it would just reconstruct the input.

You might be able to do this somewhat more easily by wiring the SAX
events into a custom XMLReader, which you could then process via a
default Transformer object (TransformerFactory.newInstance()). That
brings us around to XSL, which you might find would be an altogether
easier way to accomplish the general goal you are pursuing. YMMV.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top