Marcos R. Guevara Marcos R. Guevara - 2 months ago 20
Python Question

AttributeError: 'NoneType' object has no attribute 'text'

This is what i have, this code will generate an Xml file when you call the function with his parameters.

from lxml import etree

root = etree.Element("UpdateInventoryRequest")
doc = etree.ElementTree(root)

start_date_sard = root.append(etree.Element("StartDate"))
room_id_sard = root.append(etree.Element("RoomId"))
root.append(etree.Element("Data"))
data_root = root[2]
availability_in_data = data_root.append(etree.Element("Availability"))
price_in_data = data_root.append(etree.Element("Price"))


def buildXmlUpdate(dfrom, roomId, ldays):

start_date_sard.text = dfrom
roomId = str(roomId)
room_id_sard.text = roomId


for n in ldays:
print (dfrom, roomId, n)
#availability_in_data.text = get.ldays['avail']
#price_in_data.txt = get.ldays['price']
ldays[-1]['avail'] = str(ldays[-1]['avail'])
ldays[-1]['price'] =str(ldays[-1]['price'])
availability_in_data.text = ldays[-1]['avail']
price_in_data.text = ldays[-1]['price']


#here execute the function

buildXmlUpdate('21/12/2015', 1, [{'avail': 1, 'price': 100}, {'avail': 3, 'price': 120}])
doc.write('testoutput.xml', pretty_print=True)


And this is what i receive:

AttributeError: 'NoneType' object has no attribute 'text'


What happen?

Answer

Solved:

from lxml import etree
# Create Xml
root = etree.Element("UpdateInventoryRequest")
doc = etree.ElementTree(root)

root.append(etree.Element("StartDate"))
root.append(etree.Element("RoomId"))
root.append(etree.Element("Data"))
data_root = root[2]
data_root.append(etree.Element("Availability"))
data_root.append(etree.Element("Price"))
# Xml in code

def buildXmlUpdate(dfrom, roomId, ldays):   
    start_date_sard = dfrom
    roomId = str(roomId)  
    room_id_sard = roomId

    for n in ldays:
        print (dfrom, roomId, n)
        ldays[-1]['avail'] = str(ldays[-1]['avail'])
        ldays[-1]['price'] =str(ldays[-1]['price'])
        availability_in_data = ldays[-1]['avail']
        price_in_data = ldays[-1]['price']

    root[0].text = start_date_sard
    root[1].text = room_id_sard
    data_root[0].text = availability_in_data
    data_root[1].text = price_in_data

#here execute the function 

buildXmlUpdate('21/12/2015', 1, [{'avail': 1, 'price': 100}, {'avail': 3, 'price': 120}])
doc.write('testoutput.xml', pretty_print=True)
Comments