Does anyone have any tips on how to use lxml.objectify
with recover=True
?
I have xml where the attributes are not quoted --> name=value instead of name='value'.
Below is some sample code... I do not have control over the XML formatting so I can not go back and have it changed. The etree
parsing does work
The error is
File "<string>", line unknown
XMLSyntaxError: AttValue: " or ' expected, line 4, column 21
lxml.objectify
CODE -- FAILS
xmlSample="""<dict>
<maptable>
<hdterm displevel=1 autlookup entrytype=1>Source term</hdterm>
</maptable>
</dict>"""
If I don't get an answer do I have to re
import io
#p = objectify.XMLParser(recover=True)
root = objectify.fromstring(xmlSample)
# returns attributes in element node as dict
attrib = root.getattrib()
# how to extract element data
tbl = root.mytable
print("root.mytable type=%s" % type(tbl))
lxml.etree
- WORKS!
from lxml import etree, objectify
import io
xmlIO = io.StringIO(xmlSample)
p = etree.XMLParser(recover=True)
tree = etree.parse(xmlIO, parser=p)
root = tree.getroot()
print(root.tag)
OUTPUT:
myxml