Home > OS >  find a value in an XML file in Python without knowing the structure
find a value in an XML file in Python without knowing the structure

Time:01-26

I have to get a specific value from different xml files with different structures (some simple, some more complicated). They all have this element <CurrencyPair>XXXYYY</CurrencyPair> in a different place.

I only need that value XXXYYY. Is there a way to get it regardless of the structure in Python?

CodePudding user response:

You might use xml.dom.minidom for this task following way

import xml.dom.minidom
def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)
document = '<?xml version="1.0"?><Parent id="top"><CurrencyPair>XXXYYY</CurrencyPair></Parent>'
dom = xml.dom.minidom.parseString(document)
for cur_pair in dom.getElementsByTagName("CurrencyPair"):
    print(getText(cur_pair.childNodes))

gives output

XXXYYY

Note: getText is lifted verbatim from docs, use parse rather than parseString if you wish to process XML file rather than XML stored in str.

  • Related