title: How to insert a sub-node for a particular node in XML using cElementTree in python
body: <p>Below is the requirement to convert a BNF-form grammar into XML.</p>
  
  <p>input:</p>
  
  <pre><code>define program 
      [repeat statement] 
  end define 
  
  define statement 
      [includeStatement] 
      |   [keysStatement] 
      |   [compoundsStatement] 
      |   [commentsStatement] 
      |   [tokensStatement] 
      |   [defineStatement] 
      |   [redefineStatement] 
      |   [ruleStatement] 
      |   [functionStatement] 
      |   [externalStatement] 
      |   [comment] [NL]
  end define 
  </code></pre>
  
  <p>expected output:</p>
  
  <pre><code>&lt;Feature&gt;
     &lt;program&gt;
      &lt;statement&gt;
         &lt;includeStatement /&gt;
         &lt;keysStatement /&gt;
         &lt;compoundsStatement /&gt;
         &lt;commentsStatement /&gt;
         &lt;tokensStatement /&gt;
         &lt;defineStatement /&gt;
         &lt;redefineStatement /&gt;
         &lt;ruleStatement /&gt;
         &lt;functionStatement /&gt;
         &lt;externalStatement /&gt;
         &lt;comment /&gt;
         &lt;NL /&gt;
      &lt;/statement&gt;
     &lt;/program&gt;
  &lt;/Feature&gt;
  </code></pre>
  
  <p>actual output:</p>
  
  <pre><code>&lt;Feature&gt;
     &lt;program&gt;
         &lt;statement /&gt;
     &lt;/program&gt;
  &lt;/Feature&gt;
  </code></pre>
  
  <p>Below is a function in my code, ET.SubElement(parent, ) is working for one section but not working in another part the reason could because of ET.Element(nonTmnl) returns a value rather than returning a reference. I have commented the code on my finding. Appreciate any suggestion on how I can get access to a node in XML so I can insert a child node to it.</p>
  
  <pre><code>import xml.etree.cElementTree as ET
  def getNonTerminal (strline):
       wordList=''
       global parent
       if re.match('define \w',strline):
           nonTmnl = strline.replace('define ','')
           nonTmnl = nonTmnl.replace('\n','')
           nonTmnl = nonTmnl.replace(' ','')
           if nonTmnl not in nonterminals:
              child = ET.SubElement(parent, nonTmnl) #This line is working Problem line 2 not working and has a dependency on problem line 1            parent = child
              nonterminals.append(nonTmnl)
           else:
               parent = ET.Element(nonTmnl) #Problem line1: Here I am searching for a node under which I want to insert a new sub-node           
           return;
       if re.match('.*\[.*\].*',strline):
           strline = strline.replace('\'[','')
           while (re.match('.*\[.*\].*',strline)):
               wordList = extractWords(strline)
               strList = wordList.split(' ')
               for item in strList:
                   if item not in TXLtoken and item not in TXLunparseElement and item not in TXLmodifier and item not in TXLother and item not in nonterminals :
                       if not item.startswith('\''):
                           item = item.replace(' ','')
                           while(item[-1] in  TXLmodifier):
                               item = item[:-1]
  
                           nonterminals.append(item)
                           child = ET.SubElement(parent, item) #Problem line2: Here I am adding the subnode. While debugging I see it adds to the parent node(variable), but it never reflects in final XML.
               strline = strline.replace('['+wordList+']','',1)
           return;
  </code></pre>