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
Tag: xml
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
Merge xml files with nested elements without external libraries
I am trying to merge multiple XML files together using Python and no external libraries. The XML files have nested elements. Sample File 1: Sample File 2: What I Want: What I have tried: From this answer. What I Get: I hope you can see and understand my problem. I am looking for a proper solution, any guidance would be
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
How to get all sub-elements of an element tree with Python ElementTree?
I want to find a way to get all the sub-elements of an element tree like the way ElementTree.getchildren() does, since getchildren() is deprecated since Python version 2.7. I don’t want to use it anymore, though I can still use it currently. Answer All sub-elements (descendants) of elem: A more complete example: To exclude the root itself:
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
inserting newlines in xml file generated via xml.etree.ElementTree in python
I have created a xml file using xml.etree.ElementTree in python. I then use to write out the document to a file. But when I open filename using a text editor, there are no newlines between the tags. Everything is one big line How can I write out the document in a “pretty printed” format so that there are new lines
How to convert an XML string to a dictionary?
I have a program that reads an XML document from a socket. I have the XML document stored in a string which I would like to convert directly to a Python dictionary, the same way it is done in Django’s simplejson library. Take as an example: Then dic_xml would look like {‘person’ : { ‘name’ : ‘john’, ‘age’ : 20