sax find and replace

Discussion in 'Java' started by joe, Oct 7, 2004.

  1. joe

    joe Guest

    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 ?
    joe, Oct 7, 2004
    #1
    1. Advertising

  2. joe

    Bryce Guest

    On 7 Oct 2004 09:52:52 -0700, "joe" <> wrote:

    >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

    --
    now with more cowbell
    Bryce, Oct 7, 2004
    #2
    1. Advertising

  3. joe

    Bryce Guest

    On 7 Oct 2004 09:52:52 -0700, "joe" <> wrote:

    >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.

    --
    now with more cowbell
    Bryce, Oct 7, 2004
    #3
  4. joe

    joe Guest

    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.
    joe, Oct 7, 2004
    #4
  5. joe

    Bryce Guest

    On 7 Oct 2004 12:06:08 -0700, "joe" <> wrote:

    >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}

    >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.



    --
    now with more cowbell
    Bryce, Oct 8, 2004
    #5
  6. joe wrote:

    > 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
    John C. Bollinger, Oct 8, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Behnel
    Replies:
    2
    Views:
    591
    =?ISO-8859-15?Q?Walter_D=F6rwald?=
    Nov 25, 2004
  2. RamaKrishna Narla
    Replies:
    1
    Views:
    649
    Joe Kesselman
    Aug 22, 2006
  3. RC
    Replies:
    1
    Views:
    309
  4. RC
    Replies:
    1
    Views:
    1,308
  5. Wybo Dekker
    Replies:
    1
    Views:
    350
    Yukihiro Matsumoto
    Nov 15, 2005
Loading...

Share This Page