python - 如何在 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;
解决方案
推荐阅读
- python - 使用“缩放到矩形”选项放大 matplotlib 时图形大小发生变化
- c# - 如何通过 .NET Framework 应用程序以编程方式检查我的计算机上安装的 .NET Core 运行时版本?
- asp.net - 在 IE/Net webbrowser 中使用 Angular 项目
- elasticsearch - Elasticsearch 的索引生命周期管理随机删除索引
- python-3.x - 您如何为 Zoom.us 会议发送隐藏字幕?
- python - Python peewee.ImproperlyConfigured:未安装 MySQL 驱动程序
- .net - Bootstrap:在 2500px 宽的浏览器窗口中使用 container-fluid 时,col-lg 确定 col-xl 的宽度
- python - 如何检查目录大小/如何确保重新定位文件完成
- powerbi - 如何在 Power BI 中使用度量值对表进行转置?
- python - 有没有办法在删除后恢复内置函数?