【发布时间】:2016-08-16 12:12:13
【问题描述】:
假设我有以下 XML 结构:
<?xml version="1.0" encoding="utf-8" ?>
<Document>
<CstmrCdtTrfInitn>
<GrpHdr>
<other_tags>a</other_tags> <!--here there might be other nested tags inside <other_tags></other_tags>-->
<other_tags>b</other_tags> <!--here there might be other nested tags inside <other_tags></other_tags>-->
<other_tags>c</other_tags> <!--here there might be other nested tags inside <other_tags></other_tags>-->
</GrpHdr>
<PmtInf>
<things>d</things> <!--here there might be other nested tags inside <things></things>-->
<things>e</things> <!--here there might be other nested tags inside <things></things>-->
<CdtTrfTxInf>
<!-- other nested tags here -->
</CdtTrfTxInf>
</PmtInf>
<PmtInf>
<things>f</things> <!--here there might be other nested tags inside <things></things>-->
<things>g</things> <!--here there might be other nested tags inside <things></things>-->
<CdtTrfTxInf>
<!-- other nested tags here -->
</CdtTrfTxInf>
</PmtInf>
<PmtInf>
<things>f</things> <!--here there might be other nested tags inside <things></things>-->
<things>g</things> <!--here there might be other nested tags inside <things></things>-->
<CdtTrfTxInf>
<!-- other nested tags here -->
</CdtTrfTxInf>
</PmtInf>
</CstmrCdtTrfInitn>
</Document>
现在,鉴于这种结构,我想按如下方式操作这些部分:
如果有两个或多个<PmtInf>标签相同:
<things>d</things> <!--here there might be other nested tags inside <things></things>-->
<things>e</things> <!--here there might be other nested tags inside <things></things>-->
我想将整个 <CdtTrfTxInf></CdtTrfTxInf> 移动到第一个 <PmtInf></PmtInf> 并删除我从 <CdtTrfTxInf></CdtTrfTxInf> 中提取的整个 <PmtInf></PmtInf>。有点模糊,对吧?这是一个例子:
<Document>
<CstmrCdtTrfInitn>
<GrpHdr>
<other_tags>a</other_tags> <!--here there might be other nested tags inside <other_tags></other_tags>-->
<other_tags>b</other_tags> <!--here there might be other nested tags inside <other_tags></other_tags>-->
<other_tags>c</other_tags> <!--here there might be other nested tags inside <other_tags></other_tags>-->
</GrpHdr>
<PmtInf>
<things>d</things> <!--here there might be other nested tags inside <things></things>-->
<things>e</things> <!--here there might be other nested tags inside <things></things>-->
<CdtTrfTxInf>
<!-- other nested tags here -->
</CdtTrfTxInf>
</PmtInf>
<PmtInf>
<things>f</things> <!--here there might be other nested tags inside <things></things>-->
<things>g</things> <!--here there might be other nested tags inside <things></things>-->
<CdtTrfTxInf>
<!-- other nested tags here -->
</CdtTrfTxInf>
<CdtTrfTxInf>
<!-- other nested tags here -->
</CdtTrfTxInf>
</PmtInf>
</CstmrCdtTrfInitn>
</Document>
如您所见,最后两个 <PmtInf></PmtInf> 标记现在变成了一个(因为 <things></matched>)并且复制了 <CdtTrfTxInf></CdtTrfTxInf>。
现在,我想以任何可能的方式执行此操作(lxml、xml.etree、xslt 等)。起初,我想使用一些 RegEx 来执行此操作,但它可能会变得有点难看。然后,我想我也许可以使用一些字符串操作,但我不知道该怎么做。
如果 XML 文件的平均大小约为 2k 行,谁能告诉我哪种方法最优雅/最有效?一个例子也将不胜感激。
为了完整起见,我将定义一个函数,它将以字符串形式返回整个 XML 内容:
def get_xml_from(some_file):
with open(some_file) as xml_file:
content = xml_file.read()
return content
def modify_xml(some_file):
content_of_xml = get_xml_from(some_file)
# here I should be able to process the XML file
return processed_xml
我不是在寻找为我做这件事的人,而是在寻求实现这一目标的最佳方法的想法。
【问题讨论】:
-
不要尝试使用
regex路径,XML不是常规语言。 “没有 XML 模块”的原因是什么? -
@DeepSpace XML 是其他文件处理的结果,我没有为此使用 XML 模块。这不是强制性要求,但据我研究
lxml和xml.etree我没有成功找到一种干净的方法来做到这一点
标签: python xml python-3.x