How to return the values of dictionary in XML file

Nguyễn Cường :

I have XML as following format

<case>
    <number>162</number>
    <age>40</age>
    <sex>F</sex>
    <composition>solid</composition>
    <echogenicity>hypoechogenicity</echogenicity>
    <margins>ill defined</margins>
    <calcifications>non</calcifications>
    <tirads>4c</tirads>
    <reportbacaf/>
    <reporteco/>
    <mark>
        <image>1</image>
        <svg>[{"points": [{"x": 403, "y": 79}, {"x": 399, "y": 79}, {"x": 391, "y": 78}, {"x": 379, "y": 82}, {"x": 373, "y": 88}, {"x": 368, "y": 99}, "annotation": {}, "regionType": "freehand"}]
        </svg>
    </mark>
</case>

Now, I would like to get the value as a pair of x and y from tag, such as (403,79), (399,79) ...

I have tried but only get the string type values

root = tree.getroot()
for item in root.findall('mark'):
svg = item.findall('svg')
svg_value = t[0].text

Could I able to get it as dictionary value type ?

balderman :

Below

(The JSON string inside svg was invalid and had to be fixed)

import xml.etree.ElementTree as ET
import json


xml = '''<case>
    <number>162</number>
    <age>40</age>
    <sex>F</sex>
    <composition>solid</composition>
    <echogenicity>hypoechogenicity</echogenicity>
    <margins>ill defined</margins>
    <calcifications>non</calcifications>
    <tirads>4c</tirads>
    <reportbacaf/>
    <reporteco/>
    <mark>
        <image>1</image>
        <svg>[{"points": [{"x": 493, "y": 79}, {"x": 399, "y": 79}, {"x": 391, "y": 78}, {"x": 379, "y": 82}, {"x": 373, "y": 88}, {"x": 368, "y": 99}], "annotation": {}, "regionType": "freehand"}]
        </svg>
    </mark>
     <mark>
        <image>5</image>
        <svg>[{"points": [{"x": 343, "y": 79}, {"x": 399, "y": 79}, {"x": 391, "y": 78}, {"x": 379, "y": 82}, {"x": 373, "y": 88}, {"x": 368, "y": 99}], "annotation": {}, "regionType": "freehand"}]
        </svg>
    </mark>
</case>'''

root = ET.fromstring(xml)
svg_lst = [s.text for s in root.findall('.//svg')]
data = [json.loads(svg) for svg in svg_lst]
print(data)

output

[[{'points': [{'x': 493, 'y': 79}, {'x': 399, 'y': 79}, {'x': 391, 'y': 78}, {'x': 379, 'y': 82}, {'x': 373, 'y': 88}, {'x': 368, 'y': 99}], 'annotation': {}, 'regionType': 'freehand'}], [{'points': [{'x': 343, 'y': 79}, {'x': 399, 'y': 79}, {'x': 391, 'y': 78}, {'x': 379, 'y': 82}, {'x': 373, 'y': 88}, {'x': 368, 'y': 99}], 'annotation': {}, 'regionType': 'freehand'}]]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=382880&siteId=1