jhon.smith
jhon.smith

Reputation: 2043

Argument must be bytes or unicode, got '_Element'

I am using Python 3.3 and running into this error

from lxml import etree
xmlns = "http://www.fpml.org/FpML-5/confirmation"
xsi  = "http://www.w3.org/2001/XMLSchema-instance"
fpmlVersion="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-6.xsd http://www.w3.org/2000/09/xmldsig# ../../xmldsig-core-schema.xsd"
page = etree.Element("{"+xmlns+"}dataDocument",nsmap={None:xmlns,'xsi':xsi })
page.set("fpmlVersion", fpmlVersion)
doc = etree.SubElement(page,trade)
s = etree.tostring(doc, xml_declaration=True,encoding="UTF-8",pretty_print=True)
print (s)
TypeError: Argument must be bytes or unicode, got '_Element'

I want the output to be

<dataDocument xmlns="http://www.fpml.org/FpML-5/confirmation" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
fpmlVersion="http://www.tradfpml.org/FpML-5/confirmation ../../fpml-main-5-6.xsd http://www.w3.org/2000/09/xmldsig# ../../xmldsig-core-schema.xsd">
<trade>
</trade>
</dataDocument>

Upvotes: 2

Views: 13655

Answers (1)

Birei
Birei

Reputation: 36282

I think that second argument to SubElement() function has to be a string, and python complaints about it. It should be:

doc = etree.SubElement(page,"trade")

Upvotes: 3

Related Questions