System DataSet to todataset, IndexError: Row 0 doesn’t have the same number of columns as header list

I was trying out this ETree XML Parsing example
https://docs.inductiveautomation.com/display/DOC80/Parsing+XML+with+the+Etree+Library

import xml.etree.ElementTree as ET
##Grab data from MTConnect XML
client = system.net.httpClient()
response = client.get("URLoftheMTConnectIPaddressgoeshere")

######
# Here is where you would paste in the document string.
document = response.getText()
######
 
# We can then parse the string into useable elements.
root = ET.fromstring(document)
 
# This creates empty header and row lists that we will add to later.----------------------------------------
# These are used to create the dataset that will go into the Table.
# We could fill in the names of the headers beforehand, since we know what each will be.
# However, this allows us to add or remove children keys, and the script will automatically adjust.
headers = []
rows = []
 
# Now we can loop through each child of the root.
# Since the root is catalog, each child element is an individual book.
# We also create a single row empty list. We can add all of the data for a single book to this list.
for child in root:
    oneRow = []
 
    # Check if the book has any attributes.
    if child.attrib != {}:
 
        # If it does contain attributes, we want to loop through all of them.
        for key in child.attrib:
 
            # Since we only want to add the attributes to our header list once, first check if it is there.
            # If it isn't add it.
            if key not in headers:
                headers.append(key)
 
            # Add the attribute value to the oneRow list.
            oneRow.append(child.attrib[key])
 
    # Loop through the children of the book.
    for child2 in child:
 
        # Similar to above, we check if the tag is present in the header list before adding it.
        if child2.tag not in headers:
            headers.append(child2.tag)
 
        # We can then add the text of the Element to the oneRow list.
        oneRow.append(child2.text)
 
    # Finally, we want to add the oneRow list to our list of rows.
    rows.append(oneRow)
 
# Once the loop is complete, this will print out the headers and rows list so we can manually check them in the console.
print headers
print rows

but with some minor modifications to read an XML from an MTConnect IP address

the result looks like this

['creationTime', 'version', 'instanceId', 'sender', 'assetBufferSize', 'assetCount', 'bufferSize', '{urn:mtconnect.org:MTConnectDevices:1.5}Device']

[['2023-10-10T19:43:21Z', '1.2.0.23', '1683900000', 'MyComputersName', '1024', '0', '131072'], ['\n      ', '\n      ', '\n      ', '\n      ']]

the first line is the headers , the second is the rows

when i try to use data = system.dataset.toDataSet(headers,rows) at the end so I can put it in a tag

I get---- IndexError: Row 0 doesn’t have the same number of columns as header list

I am assuming this is because the final header seems to be returning its own list
so i end up with 8 headers and a row with 2 lists of 7 and 4 which doesnt add up

but how do i fix that?

You must collect all of the headers before you build any rows, so you can ensure all rows have the right number of columns. Loop through your source data twice if necessary.