How to Solve XML Parser

Written By
Adam Bhula

XML Parser Introduction

The XML Parser problem (and XML formatter problem) as the name would suggest asks us to write an XML parser and formatter. An XML parser is used for XML documents. It reads the document and analyzes the document structure and the data properties. To turn our XML data into a readable format we will solve this problem recursively. There are many libraries in each programming language we can leverage to achieve this.

XML Parser Problem

Write an XML parser and formatter.

XML Parser Solutions

To work with XML data, we need to parse it into a structured format and also be able to format it back into a readable XML string. The solution follows a recursive approach to achieve this.

For parsing XML, we use different libraries in Python, JavaScript, and Java. In Python, the xml.etree.ElementTree module provides a handy API for parsing XML. It converts the XML string into an object representation, allowing us to access and extract information from the XML structure.

After parsing, we can format the parsed object or document back into a well-formatted XML string. In Python, we utilize the ElementTree object's built-in tostring method, which takes care of proper indentation and line breaks, resulting in a nicely formatted XML string.

Similarly, in JavaScript, we rely on the xml2js library, which offers functions to parse XML into JavaScript objects and format them back into readable XML strings. In Java, we use the javax.xml.parsers package and the Transformer class. The javax.xml.parsers package enables XML parsing into a structured document, and the Transformer class provides methods to format the XML document into a human-friendly string representation.

By leveraging these XML parsing and formatting capabilities, the solution simplifies the process of working with XML data in Python, JavaScript, and Java, allowing us to parse XML, extract desired information, and format XML documents for improved readability and manipulation.

import xml.etree.ElementTree as ET

def parse_and_format_xml(xml_string):
    # Parse the XML string into an ElementTree object
    tree = ET.ElementTree(ET.fromstring(xml_string))
    # Get the root element of the XML tree
    root = tree.getroot()
    # Format the XML string
    formatted_xml = format_xml(root)
    # Process the XML tree recursively
    result = process_element(root)
    return {'formatted_xml': formatted_xml, 'parsed_xml': result}

def process_element(element):
    # Create a dictionary to store the element's attributes and children
    result = {'tag': element.tag, 'attributes': element.attrib, 'children': []}
    # Process the element's children recursively
    for child in element:
        child_result = process_element(child)
    return result

def format_xml(element, indent=''):
    # Format the opening tag of the element
    xml_string = f'{indent}<{element.tag}'
    # Format the element's attributes
    for key, value in element.attrib.items():
        xml_string += f' {key}="{value}"'
    # Check if the element has children
    if element.text or len(element) > 0:
        xml_string += '>\n'
        # Format the element's text
        if element.text:
            xml_string += f'{indent}  {element.text.strip()}\n'
        # Format the element's children recursively
        for child in element:
            xml_string += format_xml(child, indent + '  ')
        # Format the closing tag of the element
        xml_string += f'{indent}</{element.tag}>\n'
        # Format the self-closing tag if the element has no children
        xml_string += '/>\n'
    return xml_string

xml_string = '''

result = parse_and_format_xml(xml_string)
formatted_xml = result['formatted_xml']
parsed_xml = result['parsed_xml']

print("Formatted XML:")

print("\nParsed XML:")

Time/Space Complexity Analysis

  • Time Complexity: O(n + m), where n is the length of the XML string and m is the number of elements in the XML document.
  • Space Complexity: O(m), since the XML tree representation requires space for each element and its attributes, and the formatted XML string also grows with the number of elements in the XML document.

About is a mock interview practice platform. We've hosted over 100K mock interviews, conducted by senior engineers from FAANG & other top companies. We've drawn on data from these interviews to bring you the best interview prep resource on the web.

We know exactly what to do and say to get the company, title, and salary you want.

Interview prep and job hunting are chaos and pain. We can help. Really.