in

XML parsing & reforming in python


I have xml data as below.

<?xml version="1.0" encoding="utf-8"?>
<INSTANCE>
    <NAME>CRV_A</NAME>
    <CATEGORY>CURVE</CATEGORY>
    <FEATURE>FT_A</FEATURE>
    <VALUE-CONT>
        <UNIT-DISPLAY-NAME xml:space="preserve">g</UNIT-DISPLAY-NAME>
        <VALUES-PHYS>
            <VG>
                <LABEL>10</LABEL>
                <V>0.95</V>
                <V>0.88</V>
                <V>0.87</V>
                <V>0.84</V>
                <V>0.82</V>
            </VG>
            <VG>
                <LABEL>20</LABEL>
                <V>0.95</V>
                <V>0.88</V>
                <V>0.87</V>
                <V>0.84</V>
                <V>0.82</V>
            </VG>
            <VG>
                <LABEL>30</LABEL>
                <V>0.95</V>
                <V>0.88</V>
                <V>0.87</V>
                <V>0.84</V>
                <V>0.82</V>
            </VG>
        </VALUES-PHYS>
    </VALUE-CONT>
    <AXIS-CONTS>
        <AXIS-CONT>
            <CATEGORY>STD_AXIS</CATEGORY>
            <UNIT-DISPLAY-NAME xml:space="preserve">mg</UNIT-DISPLAY-NAME>
            <VALUES-PHYS>
                <V>0</V>
                <V>50</V>
                <V>100</V>
                <V>500</V>
                <V>1000</V>
            </VALUES-PHYS>
        </AXIS-CONT>
        <AXIS-CONT>
            <CATEGORY>STD_AXIS</CATEGORY>
            <UNIT-DISPLAY-NAME xml:space="preserve">kg/h</UNIT-DISPLAY-NAME>
            <VALUES-PHYS>
                <V>10</V>
                <V>20</V>
                <V>30</V>
            </VALUES-PHYS>
        </AXIS-CONT>
    </AXIS-CONTS>
</INSTANCE>

I want to import this and write to excel file as below
But this structure is too complicated for me

NAME    MAP_A
CATEGORY    MAP
FEATURE FT_A
UNIT    g
+----+------+------+------+------+------+
|   | 0    | 50   | 100  | 500  | 1000 |
+----+------+------+------+------+------+
| 10 | 0.95 | 0.88 | 0.87 | 0.84 | 0.82 |
+----+------+------+------+------+------+
| 20 | 0.95 | 0.88 | 0.87 | 0.84 | 0.82 |
+----+------+------+------+------+------+
| 30 | 0.95 | 0.88 | 0.87 | 0.84 | 0.82 |
+----+------+------+------+------+------+
X-AXIS  STD_AXIS
UNIT    mg
+---+----+-----+-----+------+------+
| 0 | 50 | 100 | 500 | 1000 | 1000 |
+---+----+-----+-----+------+------+
Y-AXIS  STD_AXIS
UNIT    kg/h

But I can’t even parse xml data properly.
read_xml in Pandas doesn’t import all the data, and I failed to parse it using ElementTree, lxml, bs4.
Please help me.

My code is as follows:

import xml.etree.ElementTree as et
import pandas as pd

file="Sample.xml"

xml = open(file, encoding='utf-8')
tree = et.parse(xml)
root = tree.getroot()

df = pd.DataFrame()
iter_element = root.iter(tag="INSTANCE")
for element in iter_element:
    df['NAME'] = element.findall("NAME")
    df['CATEGORY'] = element.findall("CATEGORY")
    df['FEATURE'] = element.findall("FEATURE")
    df['VALUE-CONT'] = element.findall("VALUE-CONT")
...



Source: https://stackoverflow.com/questions/70536975/xml-parsing-reforming-in-python

A plugin enables you to import a Markdown file as various formats on your vite project

A small module for creating a card deck, used for making card games