如何重置对象“数据”?

时间:2012-08-14 10:28:17

标签: vbscript

例如,在字符串变量HG中我有html内容:

Set oHtmlFile = CreateObject("HtmlFile")
oHtmlFile.write H

<do some parsing on oHtmlFile object>

oHtmlFile.write G
...

如果我现在上面oHTMLFile包含HG内容。

如何在编写oHtmlFile数据之前重置G对象,而无需创建新对象(CreateObject("HtmlFile")

1 个答案:

答案 0 :(得分:1)

如果没有关于您的现实世界问题的进一步细节,很难找到好的/实用的建议。理论上,您可以使用.inner / outerHTLM和.innerTEXT等属性或调用.create *,。remove *或.append *等函数来操作oHtmlFile文档树。

在代码中:

  Dim oDOM  : Set oDOM = CreateObject("HTMLFILE")
  Dim sHTML : sHTML    = "<html><head></head><body></body></html>"
  WScript.Echo "Empty", TypeName(oDOM), "oDOM"
  dumpDoc oDOM

  oDOM.write sHTML
  WScript.Echo "After .write", qq(sHTML)
  dumpDoc oDOM

  sHTML = "<p>G</p><p>H</p>"
  oDOM.childNodes(0).childNodes(1).innerHTML = sHTML
  WScript.Echo "After setting body's .innerHTML to", qq(sHTML)
  dumpDoc oDOM

  sHTML = "pipapo"
  oDOM.childNodes(0).childNodes(1).childNodes(1).innerTEXT = sHTML
  WScript.Echo "After changing second P's .innerTEXT to", qq(sHTML)
  dumpDoc oDOM

  oDOM.childNodes(0).childNodes(1).removeChild oDOM.childNodes(0).childNodes(1).childNodes(0)
  WScript.Echo "After deleting first P"
  dumpDoc oDOM

  Dim oNode : Set oNode = oDOM.createElement("h1")
  oNode.appendChild oDOM.createTextNode("tritratrulala")
  oDOM.childNodes(0).childNodes(1).appendChild oNode
  WScript.Echo "After using DOM to append an H1"
  dumpDoc oDOM

  sHTML = "<span>abracadabra</span>"
  oDOM.childNodes(0).childNodes(1).childNodes(0).outerHTML = sHTML
  WScript.Echo "After overwriting first P's outerHTML", qq(sHTML)
  dumpDoc oDOM

Sub dumpDoc(oDOM)
  If oDOM.documentElement Is Nothing Then
     WScript.Echo "", "oDOM.documentElement Is Nothing"
     WScript.Echo "", "oDOM.childNodes.length", oDOM.childNodes.length
  Else
     WScript.Echo "", "oDOM.documentElement:",  oDOM.documentElement.tagName
     WScript.Echo "", "oDOM.childNodes.length", oDOM.childNodes.length
     dumpTree oDOM.documentElement, 2
  End If
  WScript.Echo "-----------------"
End Sub

Sub dumpTree(oNode, nL)
  Select Case oNode.nodeType
    Case 1
      WScript.Echo Space(nL), oNode.nodeType, oNode.tagName, qq(Replace(oNode.innerHTML, vbCrLf, ""))
      Dim oChild
      For Each oChild In oNode.childNodes
          dumpTree oChild, nL + 1
      Next
    Case 3
      WScript.Echo Space(nL), oNode.nodeType, oNode.nodeName, qq(Replace(oNode.nodeValue, vbCrLf, ""))
  End Select
End Sub

输出:

demoHtmlFile - demo HtmlFile
===============================================================================
Empty HTMLDocument oDOM
 oDOM.documentElement Is Nothing
 oDOM.childNodes.length 0
-----------------
After .write "<html><head></head><body></body></html>"
 oDOM.documentElement: HTML
 oDOM.childNodes.length 1
   1 HTML "<HEAD></HEAD><BODY></BODY>"
    1 HEAD ""
     1 TITLE ""
    1 BODY ""
-----------------
After setting body's .innerHTML to "<p>G</p><p>H</p>"
 oDOM.documentElement: HTML
 oDOM.childNodes.length 1
   1 HTML "<HEAD></HEAD><BODY><P>G</P><P>H</P></BODY>"
    1 HEAD ""
     1 TITLE ""
    1 BODY "<P>G</P><P>H</P>"
     1 P "G"
      3 #text "G"
     1 P "H"
      3 #text "H"
-----------------
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"
-----------------
After using DOM to append an H1
 oDOM.documentElement: HTML
 oDOM.childNodes.length 1
   1 HTML "<HEAD></HEAD><BODY><P>pipapo</P><H1>tritratrulala</H1></BODY>"
    1 HEAD ""
     1 TITLE ""
    1 BODY "<P>pipapo</P><H1>tritratrulala</H1>"
     1 P "pipapo"
      3 #text "pipapo"
     1 H1 "tritratrulala"
      3 #text "tritratrulala"
-----------------
After overwriting first P's outerHTML "<span>abracadabra</span>"
 oDOM.documentElement: HTML
 oDOM.childNodes.length 1
   1 HTML "<HEAD></HEAD><BODY><SPAN>abracadabra</SPAN><H1>tritratrulala</H1></BODY>"
    1 HEAD ""
     1 TITLE ""
    1 BODY "<SPAN>abracadabra</SPAN><H1>tritratrulala</H1>"
     1 SPAN "abracadabra"
      3 #text "abracadabra"
     1 H1 "tritratrulala"
      3 #text "tritratrulala"
-----------------

(qq(),nodeType常量以及避免这些

的策略
oDOM.childNodes(0).childNodes(1).removeChild oDOM.childNodes(0).childNodes(1).childNodes(0)

残骸留作运动)