Re: Tenplates and subtree plugins
Richard Keene <[email protected]>
| Newsgroups | gmane.comp.java.enhydra.xmlc |
|---|---|
| Message-ID | <[email protected]> |
That got me on the right track.
Here is the actual method I wrote....
/** This takes the whatToPutThere element and puts a deep copy
** of it under putItHere. Before the insertions all child nodes
** of putItUnderHere are removed.
** The idea here is to have a template doc with some
** DIV tag with an id. You find the div. Then create another
document
** with the contents for some part of the template, and copy it to
** be under the DIV.
** <code>
** layoutHTML L = new layoutHTML();
** indexHTML t = new indexHTML();
** // All HTMLElements here are org.w3c.dom.html.HTMLElement
** HTMLElement centerMiddle = L.getElementCenterMiddle();
** HTMLElement indexMainContents = t.getElementMainContents();
** HTMLElement N = replaceNodes(centerMiddle, indexMainContents);
** </code>
** @returns The new cloned node.
**/
public org.w3c.dom.html.HTMLElement replaceNodes(
org.w3c.dom.html.HTMLElement putItUnderHere,
org.w3c.dom.html.HTMLElement whatToPutThere) {
org.w3c.dom.NodeList N = putItUnderHere.getChildNodes();
while(N.getLength() > 0) {
putItUnderHere.removeChild(N.item(0));
}
org.w3c.dom.html.HTMLElement theNewNode =
(org.w3c.dom.html.HTMLElement)putItUnderHere.getOwnerDocument().importNode(whatToPutThere,
true);
putItUnderHere.appendChild(theNewNode);
return theNewNode;
}
And here is it's use...
layoutHTML L = new layoutHTML();
indexHTML t = new indexHTML();
HTMLElement centerMiddle = L.getElementCenterMiddle();
HTMLElement indexMainContents = t.getElementMainContents();
org.w3c.dom.html.HTMLElement newObj =
replaceNodes(centerMiddle, indexMainContents);
HTMLDivElement d =
(HTMLDivElement)L.getElementById("testPara1");
CharacterData cd = (CharacterData)d.getChildNodes().item(0);
cd.setData("This was changed from original clone text.");
The reason I change the CharacterData after the replaceNodes is that
if I was doing
something big like a multi row table from a database, then you avoid
copying huge amounts of nodes.
The template HTML (layoutHTML) is
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<!--#include file="javascript.js" -->
</head>
<body>
<table border="1">
<tbody>
<tr>
<td id="leftTop">Left top</td>
<td id="centerTop">Center top</td>
<td id="rightTop">Right top</td>
</tr>
<tr>
<td id="leftMiddle">Left center</td>
<td id="centerMiddle">Center center</td>
<td id="rightMiddle">Right center</td>
</tr>
<tr>
<td id="leftBottom">Left bottom</td>
<td id="centerBottom">Center bottom</td>
<td id="rightBottom">Right bottom</td>
</tr>
<tr>
</tr>
</tbody>
</table>
</body>
</html>
And the indexHTML is
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index</title>
</head>
<body>
<div id="mainContents">
<hr> <div id="testPara1"> This is a test paragraph
</div>
<hr>
</div>
</body>
</html>
David Corbin wrote:
On Thursday 21 August 2003 00:54, Richard Keene wrote:
I want to take one document called layout.html and another called body.html
and take the main contents node (a div with id='mainContents') and
remove it from the
instance of bodyHTML.class and replace the div in an instance of
layoutHTML.class
with the removed div.
I have code like....
layoutHTML L = new layoutHTML();
bodyHTML t = new bodyHTML();
// The div I want to replace.
HTMLElement centerMiddle = L.getElementCenterMiddle();
// The div I want to plug into the layoutHTML object.
HTMLElement indexMainContents = t.getElementMainContents();
HTMLElement nE =
(HTMLEelemet.getDocument().importNode(t.getElementMainContents(),true);
cenerMiddle.appendChild(newElement);
bodyMainContents.getParentNode().removeChild(indexMainContents);
centerMiddle.appendChild(t.getElementMainContents());
It compiles and when I run it it says 'wrong document'
It appears that the indexMainContents retains the class id or something
of the
owning document t, and so when L goes to render it'sself to the browser
it recognizes that the nodes are from the wrong doc.
Try someting like those two lines above.
--
Richard Keene
801-647-8932
[email protected]