elementtree

Faithfully Preserve Comments in Parsed XML

Faithfully Preserve Comments in Parsed XML Question: I’d like to preserve comments as faithfully as possible while manipulating XML. I managed to preserve comments, but the contents are getting XML-escaped. #!/usr/bin/env python # add_host_to_tomcat.py import xml.etree.ElementTree as ET from CommentedTreeBuilder import CommentedTreeBuilder parser = CommentedTreeBuilder() if __name__ == ‘__main__’: filename = “/opt/lucee/tomcat/conf/server.xml” # this is …

Total answers: 6

How do I get Python's ElementTree to pretty print to an XML file?

How do I get Python's ElementTree to pretty print to an XML file? Question: Background I am using SQLite to access a database and retrieve the desired information. I’m using ElementTree in Python version 2.6 to create an XML file with that information. Code import sqlite3 import xml.etree.ElementTree as ET # NOTE: Omitted code where …

Total answers: 8

How to solve TypeError: cannot serialize float Python Elementtree

How to solve TypeError: cannot serialize float Python Elementtree Question: I got a debugging question. Since I am quite new here, please forgive possible janky walls-of-text. After many hours I finally got elementtree to do what I want, but I cannot output my results, because tree.write(“output3.xml”) as well as print(ET.tostring(root)) gives me TypeError: cannot serialize …

Total answers: 3

Empty list returned from ElementTree findall

Empty list returned from ElementTree findall Question: I’m new to xml parsing and Python so bear with me. I’m using lxml to parse a wiki dump, but I just want for each page, its title and text. For now I’ve got this: from xml.etree import ElementTree as etree def parser(file_name): document = etree.parse(file_name) titles = …

Total answers: 2

Use xml.etree.ElementTree to print nicely formatted xml files

Use xml.etree.ElementTree to print nicely formatted xml files Question: I am trying to use xml.etree.ElementTree to write out xml files with Python. The issue is that they keep getting generated in a single line. I want to be able to easily reference them so if it’s possible I would really like to be able to …

Total answers: 2

How can I check the existence of attributes and tags in XML before parsing?

How can I check the existence of attributes and tags in XML before parsing? Question: I’m parsing an XML file via Element Tree in python and and writing the content to a cpp file. The content of children tags will be variant for different tags. For example first event tag has party tag as child …

Total answers: 1

How to write XML declaration using xml.etree.ElementTree

How to write XML declaration using xml.etree.ElementTree Question: I am generating an XML document in Python using an ElementTree, but the tostring function doesn’t include an XML declaration when converting to plaintext. from xml.etree.ElementTree import Element, tostring document = Element(‘outer’) node = SubElement(document, ‘inner’) node.NewValue = 1 print tostring(document) # Outputs “<outer><inner /></outer>” I need …

Total answers: 11

Convert Python ElementTree to string

Convert Python ElementTree to string Question: Whenever I call ElementTree.tostring(e), I get the following error message: AttributeError: ‘Element’ object has no attribute ‘getroot’ Is there any other way to convert an ElementTree object into an XML string? TraceBack: Traceback (most recent call last): File “Development/Python/REObjectSort/REObjectResolver.py”, line 145, in <module> cm = integrateDataWithCsv(cm, csvm) File “Development/Python/REObjectSort/REObjectResolver.py”, …

Total answers: 6

Parsing XML with namespace in Python via 'ElementTree'

Parsing XML with namespace in Python via 'ElementTree' Question: I have the following XML which I want to parse using Python’s ElementTree: <rdf:RDF xml_base=”http://dbpedia.org/ontology/” > <owl:Class rdf_about=”http://dbpedia.org/ontology/BasketballLeague”> <rdfs:label xml_lang=”en”>basketball league</rdfs:label> <rdfs:comment xml_lang=”en”> a group of sports teams that compete against each other in Basketball </rdfs:comment> </owl:Class> </rdf:RDF> I want to find all owl:Class tags and …

Total answers: 7

ParseError: not well-formed (invalid token) using cElementTree

ParseError: not well-formed (invalid token) using cElementTree Question: I receive xml strings from an external source that can contains unsanitized user contributed content. The following xml string gave a ParseError in cElementTree: >>> print repr(s) ‘<Comment>ddddddddx08x08x08x08x08x08_____</Comment>’ >>> import xml.etree.cElementTree as ET >>> ET.XML(s) Traceback (most recent call last): File “<pyshell#4>”, line 1, in <module> ET.XML(s) …

Total answers: 15