0

I am trying to push my output from VBScript code in table row and table cell.

My code is:

Set table = document.CreateElement("table") 
i = 0
For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    Set td = document.createElement("td")
    For Each element In node
        td.innerText = element.parentNode.nodeName & "->" & element.text
        tr.appendChild td
    Next
    table.appendChild tr
    ObjOutFile.WriteLine node.parentNode.nodeName & "->" & node.text
    i = i + 1
Next
document.body.appendChild table

What is wrong with it? it is not working while I am able to push output in list.

Edit I am using this code it prints output as expected but table is not populated.

ObjOutFile.WriteLine thing.path
document.body.appendChild p
Set tbody = document.createElement("tbody") 
For Each node In objMSXML.selectNodes(sXPath)
    Set trow = document.createElement("tr")
    Set tcol = document.createElement("td")
    tcol.innerText = tcol.innerText & node.parentNode.nodeName & "->" & node.text
    ObjOutFile.WriteLine node.parentNode.nodeName & "->" & node.text
    trow.appendChild(tcol)
    tbody.appendChild(trow)
    'ObjOutFile.WriteLine node.parentNode.nodeName & "->" & node.text
Next
document.appendChild(tbody)

The ObjOutFile.writeLine prints like:

C:\Users\abc\Desktop\samp.txt
hamster->AbcPos
hamster->Database Layer
hairyStyle->qskxyz
hairyGirl->qixyz
hairyGirl->abc
hairyGirl->def
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user2816085
  • 655
  • 4
  • 19

2 Answers2

1

You have two issues in your code:

  • node is not a collection, so For Each element In node fails. You probably have an On Error Resume Next elswhere in your HTA that is hiding this error, because otherwise you'd be seeing an error message like this:

    Object doesn't support this property or method.

  • You need to append the table rows to a <tbody> element first (see this related question).

Change your code to something like this (and remove the On Error Resume Next):

Set table = document.createElement("table")
Set tbody = document.createElement("tbody")
i = 0
For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    Set td = document.createElement("td")
    td.innerText = node.parentNode.nodeName & "->" & node.text
    tr.appendChild td
    tbody.appendChild tr
    i = i + 1
Next
table.appendChild tbody
document.body.appendChild table
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

You want a row for each node and a column for each element of the current node. So

For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    Set td = document.createElement("td")
    For Each element In node
        td.innerText = element.parentNode.nodeName & "->" & element.text
        ...

should be

For Each node In objMSXML.selectNodes(sXPath)
    Set tr = document.createElement("tr")
    For Each element In node
        Set td = document.createElement("td")
        td.innerText = element.parentNode.nodeName & "->" & element.text
        ...
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96