Is there a way to read an entire XML file into a Python variable with tags and indentation?
data = """
//input[@type='text']
//input[@type='password']
"""
This was a short example so I could manually type it in but if my XML files are really big, how can I read them into a python variable? Using fopen is not an option because it is reading all font size and styles too which is redundant in my case.
Answer
import xml.dom.minidom
xmlObject = xml.dom.minidom.parse(xml_fname) # or xml.dom.minidom.parseString(xml_string)
pretty_xml_as_string = xmlObject.toprettyxml()
print(pretty_xml_as_string)
Answer taken from here: Pretty printing XML in Python
This adds extra newlines. If you want the output to exactly look like your data you can try this:
import xml.etree.ElementTree as ET
xmlObject = ET.parse(xml_fname) # or ET.fromstring(xml_string)
pretty_xml_as_string = ET.tostring(xmlObject).decode()
print(pretty_xml_as_string)
Please note that this is for Python3.
[OP EDIT:] This is the one that worked for me
import lxml.etree as etree
x = etree.parse("filename")
print etree.tostring(x, pretty_print = True)
No comments:
Post a Comment