Let's suppose I have the following XML structure:

<?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>    

Now, given this structure, I want to manipulate the sections as follows:

If there are two or more <PmtInf> tags that have the same:

<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>-->

I would like to move the whole <CdtTrfTxInf></CdtTrfTxInf> to the first <PmtInf></PmtInf> and remove the whole <PmtInf></PmtInf> that I've taken <CdtTrfTxInf></CdtTrfTxInf> from. A bit, fuzzy, right ? Here is an example:

<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>

As you can see, the last two <PmtInf></PmtInf> tags became now a single one (because <things></matched>) and the <CdtTrfTxInf></CdtTrfTxInf> was copied.

Now, I would like to do this in any possible way (lxml, xml.etree, xslt etc). At first, I thought about using some RegEx to do this, but it might become a bit ugly. Then, I thought I might be able to use some string manipulations but I can't figure a way of how would I do this.

Can somebody tell me what method would be the most elegant / efficient one if the average size of an XML file would be about 2k lines ? An example would also be kindly appreciated.

For the sake of completness, I'll define a function which will return the entire XML content in a string:

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

I'm not looking for somebody doing this for me, but asking for ideas on what are the best ways of achieving this.

  • 3
    Don't even try going the regex path, XML is not a regular language. What is the reason behind "without XML module" ? – DeepSpace Aug 16 '16 at 12:18
  • @DeepSpace that XML is the result of other file's processing and I didn't use an XML module for that. It's not a mandatory requirement but as far as I looked into lxml and xml.etree I didn't succeed to find a clean way of doing this – Cajuu' Aug 16 '16 at 12:26
up vote 1 down vote accepted

I'm not going to give you the code you want. Instead I'll say how you can go about doing what you want.

First things first you want to read your xml. So I'll be using xml.etree.ElementTree.

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

After this I'd ignore the parts of the tree that you don't use, and just find CstmrCdtTrfInitn. As you only want to work with PmtInfs you want to findall of them.

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

After this you want to perform your algorithm* to move items on your data. I'll just remove the first child, if the node has one.

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

Now that we have all the nodes, you'll add them to the first pmt_infs.

pmt_infs[0].extend(nodes)

* You'll want to change the third code block to how you want to move your nodes, as you changed your algorithm from v1 to v3 of your question.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.