AttributeError: 'str' object has no attribute 'firstChild'

S

Sir Muffington

Guest
So I was trying to upgrade the code to have the array ordernum contain every single order number (separated by a separator, but that isn't really necessary I was just too lazy and I copy-pasted my own code).

Then the error AttributeError: 'str' object has no attribute 'firstChild' appeared when I added the last block of code. How do I fix this?

import os
from xml.dom.minidom import parse

j=0
path = "C:/Users/User/Downloads"
quantity = []
# for every file in directory (path above)
for filename in os.listdir(path):
if not filename.endswith('.xml'): continue
fullname = os.path.join(path, filename) # add path and file together
data = parse(fullname) # parses XML
ordernum = data.getElementsByTagName('OrderNumber') # gets OrderNumber from XML
print(ordernum[0].firstChild.nodeValue) # prints OrderNumber from XML
quan = data.getElementsByTagName('OrderedQuantity') # same as above with OrderedQuantity
k=0
l=0
for k, q in enumerate(quan):
if q.firstChild.nodeValue != None:
quantity.append(quan[k].firstChild.nodeValue)
quantity.append("||separator||")
for l, p in enumerate(ordernum):
if p.firstChild.nodeValue != None:
ordernum.append(ordernum[l].firstChild.nodeValue)
ordernum.append("||separator||")


XML Example:

<OrderNumber>8762196_W0169</OrderNumber>
<OrderedQuantity>2.000</OrderedQuantity>
<OrderedQuantity>4.000</OrderedQuantity>
<OrderedQuantity>6.000</OrderedQuantity>
<OrderedQuantity>3.000</OrderedQuantity>
<OrderedQuantity>1.000</OrderedQuantity>
<OrderedQuantity>8.000</OrderedQuantity>
<OrderedQuantity>6.000</OrderedQuantity>
<OrderedQuantity>7.000</OrderedQuantity>

Continue reading...
 
Top