Search code examples
pythonxmlminidom

how to parse xml data by the text value name? By python minidom


The xml is like this, how can I parse out a dictionary data like:

{"Text":"Everyone listen up.", "Font": "Arial", "Size": 20}

The tag names are always "Name" and "Value", I know there are methods in python xml module like getElementsByTagName, is there any method can do this?

                <effect>
                    <name>Text</name>
                    <effectid>Text</effectid>
                    <effectcategory>Text</effectcategory>
                    <effecttype>generator</effecttype>
                    <mediatype>video</mediatype>
                    <parameter>
                        <parameterid>str</parameterid>
                        <name>Text</name>
                        <value>Everyone listen up.</value>
                    </parameter>
                    <parameter>
                        <parameterid>fontname</parameterid>
                        <name>Font</name>
                        <value>Arial</value>
                    </parameter>
                    <parameter>
                        <parameterid>fontsize</parameterid>
                        <name>Size</name>
                        <valuemin>0</valuemin>
                        <valuemax>1000</valuemax>
                        <value>20</value>
                    </parameter>
                    <parameter>
                        <parameterid>fontstyle</parameterid>
                        <name>Style</name>
                        <valuemin>1</valuemin>
                        <valuemax>4</valuemax>
                        <valuelist>
                            <valueentry>
                                <name>Plain</name>
                                <value>1</value>
                            </valueentry>
                            <valueentry>
                                <name>Bold</name>
                                <value>2</value>
                            </valueentry>
                            <valueentry>
                                <name>Italic</name>
                                <value>3</value>
                            </valueentry>
                            <valueentry>
                                <name>Bold/Italic</name>
                                <value>4</value>
                            </valueentry>
                        </valuelist>
                        <value>1</value>
                    </parameter>
                </effect>

Solution

  • Parsing xml using xml.dom.minidom never was a fun (really think about using BeautifulSoup, xml.etree.ElementTree or lxml).

    Here's where you should start:

    from pprint import pprint
    from xml.dom.minidom import parse, Text, parseString
    
    data = """your xml goes here"""
    
    dom = parseString(data)
    
    result = {}
    
    effect = dom.getElementsByTagName('effect')[0]
    result['name'] = effect.getElementsByTagName('name')[0].firstChild.nodeValue
    
    parameters = []
    for parameter in effect.getElementsByTagName('parameter'):
        id = parameter.getElementsByTagName('parameterid')[0].firstChild.nodeValue
        name = parameter.getElementsByTagName('name')[0].firstChild.nodeValue
        value = parameter.getElementsByTagName('value')[0].firstChild.nodeValue
        parameters.append({'id': id, 'name': name, 'value': value})
    
    result['parameters'] = parameters
    
    pprint(result)
    

    Prints:

    {'name': u'Text',
     'parameters': [{'id': u'str', 'name': u'Text', 'value': u'Everyone listen up.'},
                    {'id': u'fontname', 'name': u'Font', 'value': u'Arial'},
                    {'id': u'fontsize', 'name': u'Size', 'value': u'20'},
                    {'id': u'fontstyle', 'name': u'Style', 'value': u'1'}]}