Extract specific XML tags Values in python
<p>I have a XML file which contains tags like these. </p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;DataFlows&gt;
    &lt;DataFlow id="ABC"&gt;
            &lt;Flow name="flow4" type="Ingest"&gt;
                &lt;Ingest dataSourceName="type1" tableName="table1"&gt;
                    &lt;DataSet&gt;
                        &lt;DataSetRef&gt;value1-${d1}-${t1}&lt;/DataSetRef&gt;
                        &lt;DataStore&gt;ingest&lt;/DataStore&gt;
                    &lt;/DataSet&gt;
                    &lt;Mode&gt;Overwrite&lt;/Mode&gt;
                &lt;/Ingest&gt;
            &lt;/Flow&gt;    
        &lt;/DataFlow&gt;
        &lt;DataFlow id="MHH" dependsOn="ABC"&gt;
            &lt;Flow name="flow5" type="Reconcile"&gt;
                &lt;Reconciliation&gt;
                    &lt;Source&gt;QW&lt;/Source&gt;
                    &lt;Target&gt;EF&lt;/Target&gt;
                    &lt;ComparisonKey&gt;
                        &lt;Column&gt;dealNumber&lt;/Column&gt;
                    &lt;/ComparisonKey&gt;
    &lt;ReconcileColumns mode="required"&gt;
                        &lt;Column&gt;bookId&lt;/Column&gt;
                    &lt;/ReconcileColumns&gt;
                &lt;/Reconciliation&gt;
            &lt;/Flow&gt;
            &lt;Flow name="output" type="Export" format="Native"&gt;
                &lt;Table publishToSQLServer="true"&gt;
                    &lt;DataSet&gt;
                        &lt;DataSetRef&gt;value4_${cob}_${ts}&lt;/DataSetRef&gt;
                        &lt;DataStore&gt;recon&lt;/DataStore&gt;
                        &lt;Date&gt;${run_date}&lt;/Date&gt;
                    &lt;/DataSet&gt;
                    &lt;Mode&gt;Overwrite&lt;/Mode&gt;
                &lt;/Table&gt;
            &lt;/Flow&gt;
        &lt;/DataFlow&gt;
&lt;/DataFlows&gt;
</code></pre>

<p>I want to process this XML in python using Python Minimal DOM implementation.
 I need to extract information in DataSet Tag only when the Flow type in âReconcile".</p>

<p>For Example:</p>

<p>If my Flow Type is "Reconcile" then i need to go to next Flow tag named "output" and extract values of DataSetRef,DataSource and Date tags.</p>

<p>So far i have tried below mentioned Code but i am getting blank values in all may fields.</p>

<pre><code>#!/usr/bin/python

from xml.dom.minidom import parse

import xml.dom.minidom

# Open XML document using minidom parser

DOMTree = xml.dom.minidom.parse("Store.xml")

collection = DOMTree.documentElement

#if collection.hasAttribute("DataFlows"):

#   print "Root element : %s" % collection.getAttribute("DataFlows")

pretty = DOMTree.toprettyxml()

print "Collectio: %s" % collection

dataflows = DOMTree.getElementsByTagName("DataFlow")

# Print detail of each movie.

for dataflow in dataflows:

   print "*****dataflow*****"

   if dataflow.hasAttribute("dependsOn"):

      print "Depends On is present"

      flows = DOMTree.getElementsByTagName("Flow")

      print "flows"

      for flow in flows:

        print "******flow******"

        if flow.hasAttribute("type") and flow.getAttribute("type") == "Reconcile":

          flowByReconcileType = flow.getAttribute("type")

          TagValue = flow.getElementsByTagName("DataSet")

          print "Tag Value is %s" % TagValue

          print "flow type is: %s" % flowByReconcileType
</code></pre>

<p>From there onwards i need to  pass these 3  values extracted above  to Unix Shell scripts to process some directories.
Any Help would be appreciated.</p>

<p>First of all check if your XML is well formatted. You are missing a root tag and you got wrong double quotes for example here <code>&lt;Flow name=âflow4" type="Ingest"&gt;</code></p>

<p>IN your code you are correctly grabbing the dataflows.</p>

<p>You dont need to query the DOMTree again for the flows, you can check every dataflow's flow by quering like this:</p>

<pre><code>flows = dataflow.getElementsByTagName("Flow")
</code></pre>

<p>Your condition <code>if flow.hasAttribute("type") and flow.getAttribute("type") == "Reconcile":</code> looks ok to me, so in order to get the next flow item you can do something like this always checking your index is inside the array.</p>

<pre><code>for index, flow in enumerate(flows):
    if flow.hasAttribute("type") and flow.getAttribute("type") == "Reconcile":
        if index + 1 &lt; len(flows):
            your_flow = flows[index + 1]
</code></pre>

python xml