Skip to content

Tag: xml

xml.etree.ElementTree get node depth

The XML: The code: The output: The Xml represents a tree like structure of a sitemap. I have been up and down the docs and Google all day and can’t figure it out hot to get the node depth of entries. I used counting of the children container but that only works for the first parent and then it breaks

Convert Python ElementTree to string

Whenever I call ElementTree.tostring(e), I get the following error message: Is there any other way to convert an ElementTree object into an XML string? TraceBack: Answer Element objects have no .getroot() method. Drop that call, and the .tostring() call works: You only need to use .getroot() if you have an El…

Extracting XML Attributes

I have an XML file with several thousand records in it in the form of: How can I convert this into a CSV or tab-delimited file? I know I can hard-code it in Python using re.compile() statements, but there has to be something easier, and more portable among diff XML file layouts. I’ve found a couple thre…

Python lxml – get index of tag’s text

I have an xml-file with a format similar to docx, i.e.: I need to get an index of BIG_TEXT in source xml, like: I can start a new search from position of current index + len(text), but is there another way? Element may have one character, w for example. It will find index of w, but not index of tag

Converting xml to dictionary using ElementTree

I’m looking for an XML to dictionary parser using ElementTree, I already found some but they are excluding the attributes, and in my case I have a lot of attributes. Answer Call as This works as long as you don’t actually have an attribute text; if you do, then change the third line in the functio…