classic asp htmlfile object remove child

2019-07-27 13:40发布

have to extract some info from an HTML File and strip out a div and I need to use classic asp.

I'm using HTMLFile object and it works everything except when I try to remove the div

here the relevant part of the routine:

.......

Set HTML = CreateObject("HTMLFile")
Set Frame = CreateObject("HTMLFile")  

.........  

tmp=t.ReadAll
HTML.Write tmp
t.close

Set SPN=HTML.getElementsByTagName("div")
for each sp in SPN
    if sp.getAttribute("id")="frame" then
        Frame.write sp.InnerHTML
        exit for
    end if
next        

Set Divs=Frame.getElementsByTagName("div")
for each div in Divs
    if div.getAttribute("id")="link_1" then
        Frame.removeChild(div)
    end if
next

it returns

htmlfile error '80070057'
Invalid argument
Line 57

Where command is Frame.Removechild(div)

can suggest solution or a link where can find documentation about HTMLFile Object.. since I googled a lot but apparently with wrong keywords

Thanks

Joe

1条回答
成全新的幸福
2楼-- · 2019-07-27 13:50

I think that the Docs for HTMLFILE start here.

According to the docs for removeChild, the incantation needed to remove node X is:

X.parentNode.removeChild X

If you change the delete demo code from here

oDOM.childNodes(0).childNodes(1).removeChild DOM.childNodes(0).childNodes(1).childNodes(0)

to

  Set p = oDOM.getElementsByTagName("P")(0)
  p.parentNode.removeChild p
  WScript.Echo "After deleting first P"
  dumpDoc oDOM

you should get

...

-----------------
After changing second P's .innerTEXT to "pipapo"
 oDOM.documentElement: HTML
 oDOM.childNodes.length 1
   1 HTML "<HEAD></HEAD><BODY><P>G</P><P>pipapo</P></BODY>"
    1 HEAD ""
     1 TITLE ""
    1 BODY "<P>G</P><P>pipapo</P>"
     1 P "G"
      3 #text "G"
     1 P "pipapo"
      3 #text "pipapo"
-----------------
After deleting first P
 oDOM.documentElement: HTML
 oDOM.childNodes.length 1
   1 HTML "<HEAD></HEAD><BODY><P>pipapo</P></BODY>"
    1 HEAD ""
     1 TITLE ""
    1 BODY "<P>pipapo</P>"
     1 P "pipapo"
      3 #text "pipapo"
-----------------
...
查看更多
登录 后发表回答