Home > Net >  How to parse XML data as string instead of dictionary using Python XML ElementTree library
How to parse XML data as string instead of dictionary using Python XML ElementTree library

Time:12-10

'I want to parse an XML file using the Python XML ElementTree library.

XML data example:

<stat pass="2" fail="1" skip="0" id="s1-s2" name="Webview">Test passed</stat>

I would like to parse the XML data in the exact format as in the XML file and store as a Python string, so:

string = '<stat pass="2" fail="1" skip="0" id="s1-s2" name="Bankwebview">Test passed</stat>'

At the moment, I have only came across the .attrib method and .text method which gives me the following format:

def extract_testcase(xml_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()

    #Get root <robot> data
    root_att = root.attrib    # {'pass':'2', 'fail':'1', 'skip':'0', 'id'='s1-s2', 'name'='Webview'}
    root_text = root.text     # 'Test passed'

In which I will need to manipulate the data again to get my desired string. Is there a function in ElementTree for my intended output?

CodePudding user response:

To get the value of an attribute there is a function Element.get('name') in xml.etree.ElementTree. You can use Element.set('name','value') to manipulate the attribute value of a XML byte Element (Doc).

import xml.etree.ElementTree as ET

xml = """<stat pass="2" fail="1" skip="0" id="s1-s2" name="Webview">Test passed</stat>"""

def extract_testcase(xml_string):
    x_st = ET.fromstring(xml)
    x_st.set('name', 'Bankwebview')
    return ET.tostring(x_st).decode('utf-8')
    
print("Input string: ", xml)
xml_new = extract_testcase(xml)
print("Output string",xml_new)

Print:

Input string:  <stat pass="2" fail="1" skip="0" id="s1-s2" name="Webview">Test passed</stat>
Output string: <stat pass="2" fail="1" skip="0" id="s1-s2" name="Bankwebview">Test passed</stat>

CodePudding user response:

My understanding is that you want to read the XML into a string, without really parsing it. You could do it with a regular file access in Python:

with open("my_file.xml", "r") as my_input_xml:
    my_string = my_input_xml.read()
  • Related