首页 > 解决方案 > 如何在 python 中使用 cElementTree 为 XML 中的特定节点插入子节点

问题描述

以下是将 BNF 形式的语法转换为 XML 的要求。

输入:

define program 
    [repeat statement] 
end define 

define statement 
    [includeStatement] 
    |   [keysStatement] 
    |   [compoundsStatement] 
    |   [commentsStatement] 
    |   [tokensStatement] 
    |   [defineStatement] 
    |   [redefineStatement] 
    |   [ruleStatement] 
    |   [functionStatement] 
    |   [externalStatement] 
    |   [comment] [NL]
end define 

预期输出:

<Feature>
   <program>
    <statement>
       <includeStatement />
       <keysStatement />
       <compoundsStatement />
       <commentsStatement />
       <tokensStatement />
       <defineStatement />
       <redefineStatement />
       <ruleStatement />
       <functionStatement />
       <externalStatement />
       <comment />
       <NL />
    </statement>
   </program>
</Feature>

实际输出:

<Feature>
   <program>
       <statement />
   </program>
</Feature>

下面是我的代码中的一个函数, ET.SubElement(parent, ) 在一个部分工作,但在另一部分工作不工作,原因可能是 ET.Element(nonTmnl) 返回一个值而不是返回一个引用。我已经评论了我的发现的代码。感谢有关如何访问 XML 中的节点的任何建议,以便我可以向其中插入子节点。

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;

标签: pythonxmlelementtreecelementtree

解决方案


推荐阅读