Assuming you have a file called file.xml, containing:

<annotation>
    <folder>all_images</folder>
    <filename>0.jpg</filename>
    <path>/home/vishnu/Documents/all_images/0.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>4250</width>
        <height>5500</height>
        <depth>1</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>word</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>308</xmin>
            <ymin>45</ymin>
            <xmax>502</xmax>
            <ymax>162</ymax>
        </bndbox>
    </object>
</annotation>

Then the following Python script in the same folder gives you an idea how to use the Standard Library ElementTree API to parse the file:

import xml.etree.ElementTree as ET

tree = ET.parse("file.xml")
root = tree.getroot()

print(root.find("./folder").text)
print(root.find("./object/name").text)
print(root.find("./object/bndbox/xmin").text)

You will need to work out how to write the values to your own text files, but that should be straightforward. There are lots of resources such as this one.

Answer from rjmurt on Stack Overflow
🌐
Aspose
products.aspose.com › aspose.cells › python via .net › conversion › xml to txt
Convert XML to TXT in Python Excel Library - Conversion
November 13, 2025 - Add a library reference (import the library) to your Python project. Load XML file with an instance of Workbook. Convert XML to TXT by calling Workbook.save method.
🌐
GitHub
github.com › imrrahul › XML-TMX-to-Text-.txt-file-converter-
GitHub - imrrahul/XML-TMX-to-Text-.txt-file-converter-: This python script converts XML/TMX file into Text file (.txt).
This python script converts XML/TMX file into Text file (.txt) and uses beautifulSoup to extract only the text from the xml/tmx file and removes all html/xml/tmx tags.
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
SourceForge
xml2txt.sourceforge.net
xml2txt Documentation
The script xml2txt converts a valid XML document to formatted text.
🌐
Aspose
products.aspose.com › aspose.cells › python via java › conversion › xml to txt
Convert XML to TXT in Python Excel Library
November 13, 2025 - Add a library reference (import the library) to your Python project. Load XML file with an instance of Workbook. Convert XML to TXT by calling Workbook.save method.
🌐
GitHub
gist.github.com › aspose-com-gists › de1e265bc7a9ee9b806fb7be86853933
Convert TXT to XML in Python | Python Text to XML Converter · GitHub
Clone this repository at &lt;script ...;&gt;&lt;/script&gt; Save aspose-com-gists/de1e265bc7a9ee9b806fb7be86853933 to your computer and use it in GitHub Desktop. ... Learn how to convert a TXT file to XML in Python : https://blog.aspose.com/2022/06/23/convert-txt-to-xml-in-python/ ... This file contains hidden or bidirectional Unicode text that may be ...
🌐
Quora
quora.com › How-can-I-convert-XML-files-to-text-files
How to convert XML files to text files - Quora
Use streaming parsers (Python: iterparse; Java: StAX; streaming xmlstarlet) to extract and write line-by-line without loading whole file. ... Keep XML content exactly: rename or copy file. Human-readable formatted XML: xmllint or editor pretty-print. Extract specific values to plain text lines: xmlstarlet or XPath scripts.
Find elsewhere
🌐
GitHub
github.com › knipknap › Gelatin
GitHub - knipknap/Gelatin: Transform text files to XML, JSON, or YAML
<xml> <user lastname="Doe" firstname="John"> <office>1st Ave</office> <birth-date>1978-01-01</birth-date> </user> <user lastname="Foo" firstname="Jane"> <office>2nd Ave</office> <birth-date>1970-01-01</birth-date> </user> </xml> ... from Gelatin.util import compile, generate # Parse your .gel file. syntax = compile('syntax.gel') # Convert your input file to XML, YAML, and JSON.
Starred by 159 users
Forked by 42 users
Languages   Python 96.6% | Shell 1.7% | Makefile 1.7% | Python 96.6% | Shell 1.7% | Makefile 1.7%
🌐
Stack Overflow
stackoverflow.com › questions › 58600631 › how-to-convert-a-txt-to-xml-in-python
How to convert a .txt to .xml in python - Stack Overflow
<?xml version="1.0" encoding="utf-8"?> <root> <filedata> </serialnumber> <operatorid>test</operatorid> <time>00:00:42 Test Step 2</time> <tp1>17.25</tp1> <tp2>2.46</tp2> </filedata> ... </root> I was using a code like this to convert my previous text file to xml...but right now I'm facing problems in splitting the lines.
🌐
Wordpress
sikathabis.wordpress.com › 2017 › 12 › 16 › how-to-convert-xml-file-into-text-file
How to convert xml into text file [python] | Oracle, Web, Script, SQLserver, Tips & Trick
December 16, 2017 - import xml.etree.ElementTree as ET tree = ET.parse('test.xml') notags = ET.tostring(tree.getroot(), encoding='utf-8',method='text') f = open('test.txt','w') f.write(notags) f.close() reference : link1 | link2
Top answer
1 of 1
4

Try the following code as a starter:

#!python3

import re
import xml.etree.ElementTree as ET

rex = re.compile(r'''(?P<title>Longitude
                       |Latitude
                       |date&time
                       |gsm\s+cell\s+id
                     )
                     \s*:?\s*
                     (?P<value>.*)
                     ''', re.VERBOSE)

root = ET.Element('root')
root.text = '\n'    # newline before the celldata element

with open('cell.txt') as f:
    celldata = ET.SubElement(root, 'celldata')
    celldata.text = '\n'    # newline before the collected element
    celldata.tail = '\n\n'  # empty line after the celldata element
    for line in f:
        # Empty line starts new celldata element (hack style, uggly)
        if line.isspace():
            celldata = ET.SubElement(root, 'celldata')
            celldata.text = '\n'
            celldata.tail = '\n\n'

        # If the line contains the wanted data, process it.
        m = rex.search(line)
        if m:
            # Fix some problems with the title as it will be used
            # as the tag name.
            title = m.group('title')
            title = title.replace('&', '')
            title = title.replace(' ', '')

            e = ET.SubElement(celldata, title.lower())
            e.text = m.group('value')
            e.tail = '\n'

# Display for debugging            
ET.dump(root)

# Include the root element to the tree and write the tree
# to the file.
tree = ET.ElementTree(root)
tree.write('cell.xml', encoding='utf-8', xml_declaration=True)

It displays for your example data:

<root>
<celldata>
<latitude>23.1100348</latitude>
<longitude>72.5364922</longitude>
<datetime>30:August:2014 05:04:31 PM</datetime>
<gsmcellid>4993</gsmcellid>
</celldata>

<celldata>
<latitude>23.1120549</latitude>
<longitude>72.5397988</longitude>
<datetime>30:August:2014 05:04:34 PM</datetime>
<gsmcellid>4993</gsmcellid>
</celldata>

</root>

Update for the wanted neigbour list:

#!python3

import re
import xml.etree.ElementTree as ET

rex = re.compile(r'''(?P<title>Longitude
                       |Latitude
                       |date&time
                       |gsm\s+cell\s+id
                       |Neighboring\s+List-\s+Lac\s+:\s+Cid\s+:\s+RSSI
                     )
                     \s*:?\s*
                     (?P<value>.*)
                     ''', re.VERBOSE)

root = ET.Element('root')
root.text = '\n'    # newline before the celldata element

with open('cell.txt') as f:
    celldata = ET.SubElement(root, 'celldata')
    celldata.text = '\n'    # newline before the collected element
    celldata.tail = '\n\n'  # empty line after the celldata element
    for line in f:
        # Empty line starts new celldata element (hack style, uggly)
        if line.isspace():
            celldata = ET.SubElement(root, 'celldata')
            celldata.text = '\n'
            celldata.tail = '\n\n'
        else:
            # If the line contains the wanted data, process it.
            m = rex.search(line)
            if m:
                # Fix some problems with the title as it will be used
                # as the tag name.
                title = m.group('title')
                title = title.replace('&', '')
                title = title.replace(' ', '')

                if line.startswith('Neighboring'):
                    neighbours = ET.SubElement(celldata, 'neighbours')
                    neighbours.text = '\n'
                    neighbours.tail = '\n'
                else:
                    e = ET.SubElement(celldata, title.lower())
                    e.text = m.group('value')
                    e.tail = '\n'
            else:
                # This is the neighbour item. Split it by colon,
                # and set the attributes of the item element.
                item = ET.SubElement(neighbours, 'item')
                item.tail = '\n'

                lac, cid, rssi = (a.strip() for a in line.split(':'))
                item.attrib['lac'] = lac
                item.attrib['cid'] = cid
                item.attrib['rssi'] = rssi.split()[0] # dBm removed

# Include the root element to the tree and write the tree
# to the file.
tree = ET.ElementTree(root)
tree.write('cell.xml', encoding='utf-8', xml_declaration=True)

Update for accepting empty line before neighbours -- also better implementation for general purposes:

#!python3

import re
import xml.etree.ElementTree as ET

rex = re.compile(r'''(?P<title>Longitude
                       |Latitude
                       |date&time
                       |gsm\s+cell\s+id
                       |Neighboring\s+List-\s+Lac\s+:\s+Cid\s+:\s+RSSI
                     )
                     \s*:?\s*
                     (?P<value>.*)
                     ''', re.VERBOSE)

root = ET.Element('root')
root.text = '\n'    # newline before the celldata element

with open('cell.txt') as f:
    celldata = ET.SubElement(root, 'celldata')
    celldata.text = '\n'    # newline before the collected element
    celldata.tail = '\n\n'  # empty line after the celldata element
    status = 0              # init status of the finite automaton
    for line in f:
        if status == 0:     # lines of the heading expected
            # If the line contains the wanted data, process it.
            m = rex.search(line)
            if m:
                # Fix some problems with the title as it will be used
                # as the tag name.
                title = m.group('title')
                title = title.replace('&', '')
                title = title.replace(' ', '')

                if line.startswith('Neighboring'):
                    neighbours = ET.SubElement(celldata, 'neighbours')
                    neighbours.text = '\n'
                    neighbours.tail = '\n'
                    status = 1  # empty line and then list of neighbours expected
                else:
                    e = ET.SubElement(celldata, title.lower())
                    e.text = m.group('value')
                    e.tail = '\n'
                    # keep the same status

        elif status == 1:   # empty line expected
            if line.isspace():
                status = 2  # list of neighbours must follow
            else:
                raise RuntimeError('Empty line expected. (status == {})'.format(status))
                status = 999 # error status

        elif status == 2:   # neighbour or the empty line as final separator

            if line.isspace():
                celldata = ET.SubElement(root, 'celldata')
                celldata.text = '\n'
                celldata.tail = '\n\n'
                status = 0  # go to the initial status
            else:
                # This is the neighbour item. Split it by colon,
                # and set the attributes of the item element.
                item = ET.SubElement(neighbours, 'item')
                item.tail = '\n'

                lac, cid, rssi = (a.strip() for a in line.split(':'))
                item.attrib['lac'] = lac
                item.attrib['cid'] = cid
                item.attrib['rssi'] = rssi.split()[0] # dBm removed
                # keep the same status

        elif status == 999: # error status -- break the loop
            break

        else:
            raise LogicError('Unexpected status {}.'.format(status))
            break

# Display for debugging
ET.dump(root)

# Include the root element to the tree and write the tree
# to the file.
tree = ET.ElementTree(root)
tree.write('cell.xml', encoding='utf-8', xml_declaration=True)

The code implements so called finite automaton where the status variable represents its current status. You can visualize it using pencil and paper -- draw small circles with the status numbers inside (called nodes in the graph theory). Being at the status, you allow only some kind of input (line). When the input is recognized, you draw the arrow (oriented edge in the graph theory) to another status (possibly to the same status, as a loop returning back to the same node). The arrow is annotated `condition | action'.

The result may look complex at the beginning; however, it is easy in the sense that you can always focus ony on the part of the code that belongs to certain status. And also, the code can be easily modified. However, finite automatons have limited power. But they are just perfect for this kind of problems.

🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch12s08.html
Converting Ad-Hoc Text into XML Markup - Python Cookbook [Book]
def markup(text, paragraph_tag='paragraph', inline_tags={'_':'highlight'}): # First we must escape special characters, which have special meaning in XML text = text.replace('&', "&amp;")\ .replace('<', "&lt;")\ .replace('"', "&quot;")\ .replace('>', "&gt;") # paragraph markup; pass any false value as the paragraph_tag argument to disable if paragraph_tag: # Get list of lines, removing leading and trailing empty lines: lines = text.splitlines(1) while lines and lines[-1].isspace(): lines.pop( ) while lines and lines[0].isspace( ): lines.pop(0) # Insert paragraph tags on empty lines: marker = '<
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - This function takes an XML data string (xml_data) or a file path or file-like object (from_file) as input, converts it to the canonical form, and writes it out using the out file(-like) object, if provided, or returns it as a text string if not. The output file receives text, not bytes.
🌐
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-xml-files-in-python
Reading and Writing XML Files in Python - GeeksforGeeks
January 12, 2026 - .text: Assigns text content inside the XML tags. ET.tostring(root): Converts the XML tree into bytes. Writing to "GFG.xml": Saves the generated XML to a file. XML parsing in Python · XML Tutorial · Comment · Article Tags: Article Tags: Python · python-utility ·
🌐
JSON Formatter
jsonformatter.org › xml-to-python
Best XML to Python Converter
Load Data XML to Python Download · × · Load URL · Upload File · Close · × · DownloadUpdateSave OnlineCloseErrorDialog ·