让我们假设我有以下XML结构:如何重新排序python 3中的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>
和删除整个<PmtInf></PmtInf>
我已采取<CdtTrfTxInf></CdtTrfTxInf>
从。有点模糊吧?这里有一个例子:
<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行,有人可以告诉我哪种方法是最优雅/最有效的方法吗?一个例子也会受到赞赏。
对于completness的缘故,我会定义将一个字符串返回完整的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
@DeepSpace XML是其他文件处理的结果,我没有为此使用XML模块。这不是一个强制性的要求,但据我研究'lxml'和'xml.etree'我没有成功找到一个干净的方式来做到这一点 –