What's new

Help XML

Status
Not open for further replies.

Aez4km1

Honorary Poster
Established
Joined
Oct 6, 2018
Posts
375
Solutions
1
Reaction
330
Points
203
Create an You do not have permission to view the full content of this post. Log in or register now. and retrieve the text node of every element and attribute in You do not have permission to view the full content of this post. Log in or register now..
 
Sure! Here's an example of how you can retrieve the text node of every element and attribute in an XML file using Python and present it in a table format:

Python:
import xml.etree.ElementTree as ET
import pandas as pd

# parse the XML file
tree = ET.parse('your_file.xml')
root = tree.getroot()

# create an empty list to store the data
data = []

# iterate through all elements in the XML file
for element in root.iter():
    # store the element and its text in a tuple and append to the data list
    data.append((element.tag, element.text))
    # iterate through all attributes of the element
    for attribute in element.attrib:
        # store the attribute and its value in a tuple and append to the data list
        data.append((f'{element.tag} - {attribute}', element.attrib[attribute]))

# create a pandas dataframe from the data list
df = pd.DataFrame(data, columns=['Element/Attribute', 'Text'])

# print the dataframe
print(df.to_string(index=False))

This code will output a pandas dataframe that presents the data in a table format, with columns for the element/attribute and its text value. You can modify the code to fit your specific XML file by changing the file name in the tree = ET.parse('your_file.xml') line.
 
Status
Not open for further replies.
Back
Top