Search code examples
pythonxmlminidom

parse in a xml file an element using minidom


I am triying to get the String "W:_fdsw\Projects\HIL\releases\release_9_1_0\Config\CDNG\UX_EF_TSHIL\UX_EF_TSHIL.CDP" and save it in a variable using minidom

        <TOOL id="CONTROLDESKNG" xsi:type="tool">
        <TOOL-HOST xsi:type="unicode">tsp:QMUC633107:5018</TOOL-HOST>
        <TOOL-NAME xsi:type="unicode">CONTROLDESKNG</TOOL-NAME>
        <START-OPTION xsi:type="integer">0</START-OPTION>
        <START-PRIORITY xsi:type="integer">0</START-PRIORITY>
        <SETTINGS xsi:type="dynamicPropertySet">
            <PROPERTY format-rev="1" name="ExpName" propertyType="string" readonly="false" xsi:type="_property">
                <VALUE xsi:type="unicode">K53MU</VALUE>
            </PROPERTY>
            <PROPERTY format-rev="1" name="ModelDir" propertyType="uri" readonly="false" xsi:type="_property">
                <VALUE xsi:type="unicode">W:\_fdsw\Projects\HIL\releases\release_9_1_0\Config\CDNG\UX_EF_TSHIL\Variable Descriptions\UX_EF_TSHIL.sdf(#14)</VALUE>
            </PROPERTY>
            <PROPERTY format-rev="1" name="PrjFile" propertyType="uri" readonly="false" xsi:type="_property">
                <VALUE xsi:type="unicode">W:\_fdsw\Projects\HIL\releases\release_9_1_0\Config\CDNG\UX_EF_TSHIL\UX_EF_TSHIL.CDP</VALUE>
            </PROPERTY>
            <PROPERTY format-rev="1" name="RecordingFormat" propertyType="string" readonly="false" xsi:type="_property">
                <VALUE xsi:type="unicode">MDF</VALUE>
            </PROPERTY>
            <PROPERTY format-rev="1" name="ToolState" propertyType="string" readonly="false" xsi:type="_property">
                <VALUE xsi:type="unicode">Online</VALUE>
            </PROPERTY>
            <PROPERTY format-rev="1" name="VersionCDNG" propertyType="string" readonly="false" xsi:type="_property">
                <VALUE xsi:type="unicode">5.5</VALUE>
            </PROPERTY>
            <PROPERTY format-rev="1" name="VersionHILAPI" propertyType="string" readonly="false" xsi:type="_property">
                <VALUE xsi:type="unicode">2015-B</VALUE>
            </PROPERTY>
        </SETTINGS>
    </TOOL>

My code is:

xmldoc = minidom.parse('C:\Users\qxn5622\Desktop\EF10018\DEFAULT.tbc')
propertyList = xmldoc.getElementsByTagName('PROPERTY')
for prop in propertyList:
    if prop.attributes["name"].value == "ModelDir":
        myString = prop.getElementsByTagName("VALUE").value

I think the problem is that the element I am trying to get doesnt have any Id. Can anybody help me?


Solution

  • This might help

    from xml.dom.minidom import parse
    dom = parse('C:\Users\qxn5622\Desktop\EF10018\DEFAULT.tbc')
    
    propertyList = dom.getElementsByTagName('PROPERTY')
    for prop in propertyList:
        if prop.getAttribute('name') == "PrjFile":
            myString = prop.getElementsByTagName("VALUE")
            print myString[0].firstChild.nodeValue
    

    Output:

    W:\_fdsw\Projects\HIL\releases\release_9_1_0\Config\CDNG\UX_EF_TSHIL\UX_EF_TSHIL.CDP