Re: Parsing HTML

David Li <[email protected]>
Newsgroups gmane.comp.java.enhydra.xmlc
Message-ID <[email protected]>
On Thursday, Mar 27, 2003, at 02:53 Asia/Shanghai, Ben Sinclair wrote:

> Actually, now that I think about it further, this doesn't really work 
> for me. I have one servlet that can be used by multiple "sponsors." 
> Each time you pass a different referrer ID to the servlet, it loads a 
> different header and footer, so I couldn't recompile each time.

You don't need to compile every time. Let's assume that you have the 
following layout for HTML:

/Master.html
/sponsor1/Header.html
/sponsro1/Footer.html
/sponsor2/Header.html
/sponsor2/Footer.html

Assuming the Header section is tagged with ID 'Header' in Master.html 
and all Header.html and Footer is tagged with ID 'Footer'.

In your codes:

String sponsorid = request.getParameter("sponsor");
Document master = xmlcContext.loadFile("Master.html");
Document header = xmlcContext.loadFile("/" + sponsorid + 
"/Header.html");
Document footer = xmlcContext.loadFile("/" + sponsorid + 
"/Footer.html");

Element headerElm = header.getElementById("Header");
Element footerElm = footer.getElementById("Footer");

Element hElm = master.importNode(headerElm, true);
Element fElm master.importNode(footerElm, true);

mater.getElementById("Header").getParentNode().replaceChild(hElm, 
mater.getElementById("Header"));
mater.getElementById("Footer").getParentNode().replaceChild(fElm, 
mater.getElementById("Footer"));

In this codes, the parsing only took place when the document is first 
loaded and after each change to the html. Otherwise, the Document is 
retrieved from the Factory's cache.

By the way, the Header/Footer html has to be full HTML with 
<html>/<header>/<body> but it shouldn't be a problem.

David


> The way I'm doing it now works just fine for the browsers I've tested, 
> even though the HTML wouldn't pass a validator. The only problem with 
> my method is that it tends to chop off the last part of my footer file 
> when I load it like this:
>
>                 String footer = readFile(footerFile);
>                 Node footerNode = html.getElementById("footer");
>                 if (footerNode != null) {
>                         ((LazyText) 
> footerNode.getFirstChild()).setPreFormattedText(footer);
>                 }
>
> If I output the footer string right after I read it, I can see that 
> it's all there. When viewing the source after the page is generated, 
> it has chopped off part of it.
>
> At 12:30 PM 3/26/2003, you wrote:
>> On Wednesday 26 March 2003 18:33, Ben Sinclair wrote:
>> > Is there good documentation on how to do [ automatic reloading
>> > of *ML pages ]?
>>
>> Sorry, there's not much documentation at the site yet, I didn't get 
>> around to
>> document the new features. Maybe you want to write a howto based on 
>> your
>> experiences?
>
> --
> Ben Sinclair
> [email protected]
> _______________________________________________
> XMLC mailing list
> [email protected]
> http://www.enhydra.org/mailman/listinfo.cgi/xmlc
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.