2016-08-16 102 views
-1

让我们假设我有以下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>被复制。

现在,我想以任何可能的方式做到这一点(lxmlxml.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 

我不找人做这对我来说,却要求想法是什么是实现这一目标的最佳途径。

+3

甚至不尝试使用'regex'路径,'XML'不是常规语言。 “没有XML模块”背后的原因是什么? – DeepSpace

+0

@DeepSpace XML是其他文件处理的结果,我没有为此使用XML模块。这不是一个强制性的要求,但据我研究'lxml'和'xml.etree'我没有成功找到一个干净的方式来做到这一点 –

回答

1

我不会给你你想要的代码。相反,我会说你如何去做你想做的事。

首先你要先读你的xml。所以我会用xml.etree.ElementTree

import xml.etree.ElementTree as ET 
root = ET.fromstring(country_data_as_string) 

这之后我会忽略你不使用树的部分,只是findCstmrCdtTrfInitn。 由于您只想与PmtInf一起工作,您想要findall

pmt_infs = root.find('.//CstmrCdtTrfInitn').findall('PmtInf') 

在这之后要执行你的算法*对你的数据移动项目。 我只是删除第一个孩子,如果节点有一个。

nodes = [] 
for node in pmt_infs: 
    children = list(node) 
    if children: 
     node.remove(children[0]) 
     nodes.append(children[0]) 

现在,我们拥有所有的节点,你将它们添加到第一pmt_infs

pmt_infs[0].extend(nodes) 

*你要改变第三代码块要如何将您的节点,当你从V1改变了你的算法,你的问题的V3。

相关问题