Skip to content
Advertisement

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 ElementTree instance. Other notes: This produces a bytestring, which in

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 threads here about attribs,

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

Why is lxml.etree.iterparse() eating up all my memory?

This eventually consumes all my available memory and then the process is killed. I’ve tried changing the tag from schedule to ‘smaller’ tags but that didn’t make a difference. What am I doing wrong / how can I process this large file with iterparse()? I can easily cut it up and process it in smaller chunks but that’s uglier than

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 function body to use a

Advertisement