469,646 Members | 1,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

HTML DOM cloning nodes to copy document

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

Nov 21 '05 #1
6 8144
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
Nov 21 '05 #2
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?
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #3
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 Williams" <Zj********************@Zhotmail.comZ> escribió en el mensaje
news:36*************@individual.net...
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?
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 21 '05 #4
John,

You need (direct or by late binding) to use MSHTML for that. It is not an
easy job to use that and even more a problem to help somebody with this in a
newsgroup. Do not use an import to that however refence it everytime again.
It freezes your IDE because of the endless interfaces.

mshtml
http://msdn.microsoft.com/library/de...ng/hosting.asp
I hope this helps a little bit?

I hope this helps something.

Cor
Nov 21 '05 #5
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.

"alejandro lapeyre" <al**************@jotmail.com> wrote in message
news:O6**************@TK2MSFTNGP14.phx.gbl...
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 Williams" <Zj********************@Zhotmail.comZ> escribió en el mensaje
news:36*************@individual.net...
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?
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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



Nov 21 '05 #6
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.

"J Williams" <Zj********************@Zhotmail.comZ> escribió en el mensaje
news:36*************@individual.net...
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.

"alejandro lapeyre" <al**************@jotmail.com> wrote in message
news:O6**************@TK2MSFTNGP14.phx.gbl...
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 Williams" <Zj********************@Zhotmail.comZ> escribió en el
mensaje news:36*************@individual.net...
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?
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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



Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Simon | last post: by
1 post views Thread by Philipp Schumann | last post: by
reply views Thread by meh | last post: by
8 posts views Thread by kurotsuke | last post: by
8 posts views Thread by Tom | last post: by
gits
9 posts views Thread by gits | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.