HTML DOM cloning nodes to copy document

J

J Williams

I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application
to create a new HTML document by cloning nodes. The function below is
called from the axWebBrowser1_DocumentComplete event using:

Dim mNewDoc As mshtml.IHTMLDocument3
mNewDoc = NewDoc(axWebBrowser1.Document)


Private Function NewDoc(ByVal mInputDoc As mshtml.IHTMLDocument3) As
mshtml.IHTMLDocument3
Dim mNodes As mshtml.IHTMLDOMChildrenCollection
Dim mNode, mNewNode As mshtml.IHTMLDOMNode
Dim i As Integer

NewDoc = New mshtml.HTMLDocument

mNodes = mInputDoc.childNodes
For i = 0 To mNodes.length - 1
mNode = mNodes.item(i)
mNewNode = mNode.cloneNode(True)

'Do something here to add mNewNode to NewDoc

Next

End Function

I just can't work out how to add the cloned node to the new HTML document.
I've tried .appendChild, .insertBefore, and .createElement on various
objects (probably the wrong ones or with incorrect arguments) and the VB
program just locks up.

I am aware that there is only 1 child node of mInputDoc - the <HTML> node -
and therefore the For Next loops just once, but I've specified Deep = True
to copy the whole DOM tree to the new document. There are easier ways to
copy a document, but I want to understand how to do it by copying/cloning
nodes from one document to a new document.

Thanks for any help!
--
 
C

Cor Ligthert

John,

When you want to make a copy of the document (be aware that that can be a
frame, a page can have more documents) than you can use in my opinion the
best the outertext from the <HTML> node which include that tag or the
innertext which exclude that tag..

I hope this heps.

Cor
 
J

J Williams

I would like to clone the HTML node directly and add it to a new
HTMLDocument. I can create a new HTMDocument using createElement and
appendChild as shown in the following code:

Dim mHTMLnode, mHEADnode, mTITLEnode, mBODYnode As mshtml.IHTMLDOMNode
Dim NewDoc As mshtml.HTMLDocument

NewDoc = New mshtml.HTMLDocument

mHTMLnode = NewDoc.appendChild(NewDoc.createElement("HTML"))
mHEADnode = mHTMLnode.appendChild(NewDoc.createElement("HEAD"))
mTITLEnode = mHEADnode.appendChild(NewDoc.createElement("TITLE"))
mTITLEnode.appendChild(NewDoc.createTextNode("The Title"))
mBODYnode = mHTMLnode.appendChild(NewDoc.createElement("BODY"))


As shown in my first post, I can also clone the first node of an existing
document (the HTML node which will contain all the other child nodes) but
can't add it to a new, empty HTMLDocument.

I would expect something like:

mHTMLnode = NewDoc.appendChild(mInputDoc.firstChild)

where mInputDoc is the existing HTMLDocument to work, but it doesn't.

Any ideas?
 
A

alejandro lapeyre

I think you should use importNode
Node importNode(in Node importedNode,
in boolean deep)

this is specified in Document Object Model (DOM) Level 2 Core
SpecificationVersion 1.0W3C Recommendation 13 November, 2000

I dont know if mshtml supports it.

Regards,
alejandro lapeyre
 
J

J Williams

Hi, Thanks for your reply.

Isn't importNode the equivalent of cloneNode for XML documents? They have
the same arguments. I'm already using cloneNode to clone the <HTML> </HTML>
node, but there seems to be no way of adding the node to an empty
HTMLDocument.
 
A

alejandro lapeyre

There should be some differences.
Every node belongs to a document (the one used to create the node).
I think you can only insert to a document nodes created with that document.
Thats why importNode was defined.
 

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

Top