Adding a single occurrence xml tag using lxml

Viewed 43

Based on a couple of other examples I've found here, I've created a script that creates an xml file from a csv input using lxml.etree and lxml.ebuilder. It gives me almost what I need - the one thing I'm struggling with is that I need to also include a single-occurrence tag at the top of the data which will contain a static value.

Here's my sample data:

ACTION|INV_ACCT_CLASS|EXT_INV_ID|WAREHOUSE_ID|NAME|CNTRY_CD|PHONE|ADDR_STR1|ADDR_STR2|CITY|ST|ZIP|ADD_KEY_NUM
add|2|AAA_00005|1001213|Company 1|US|9995555555|1313 Mockingbird Lane||New York|NY|10001|44433322
add|2|BBB_00008|1004312|Company 2|US|43255511110|Some other address||Stamford|CT|44112|11122233

My code so far:

import lxml.etree
 from lxml.builder import E 
 import csv

with open("filename.csv") as csvfile:

                    
  results = E.paiInv(*(
              E.invrec(
               E.action(row['ACTION']),
                E.investor(
                E.inv_account_class(row['INV_ACCOUNT_CLASS']),
                E.ext_inv_id(row['EXT_INV_ID']),
                E.warehouse_id(row['WAREHOUSE_ID']),
                E.name(row['NAME']),
                E.cntry_cd(row['CNTRY_CD']),
                E.phone(row['PHONE']),
                E.addr_str1(row['ADDRESS_STR1']),
                E.addr_str2(row['ADDRESS_STR2']),
                E.city(row['CITY']),
                E.st(row['ST']),
                E.zip(row['ZIP']),
                E.add_key_num(row['ADD_KEY_NUM'])
                      )
                ) for row in csv.DictReader(csvfile, delimiter = '|'))
                          )


 lxml.etree.ElementTree(results).write("OutputFile.xml")

Here's my output so far:

<paiInv>
    <invrec>
        <action>add</action>
        <investor>
            <inv_account_class>2</inv_account_class>
            <ext_inv_id>AAA_00005</ext_inv_id>
            <warehouse_id>1001213</warehouse_id>
            <name>Company 1</name>
            <cntry_cd>US</cntry_cd>
            <phone>9995555555</phone>
            <addr_str1>1313 Mockingbird Lane</addr_str1>
            <addr_str2></addr_str2>
            <city>New York</city>
            <st>NY</st>
            <zip>10001</zip>
            <add_key_num>44433322</add_key_num>
        </investor>
    </invrec>
    <invrec>
        <action>add</action>
        <investor>
            <inv_account_class>2</inv_account_class>
            <ext_inv_id>BBB_00008</ext_inv_id>
            <warehouse_id>1004312</warehouse_id>
            <name>Company 2</name>
            <cntry_cd>US</cntry_cd>
            <phone>43255511110</phone>
            <addr_str1>Some other address</addr_str1>
            <addr_str2></addr_str2>
            <city>Stamford</city>
            <st>NB</st>
            <zip>44112</zip>
            <add_key_num>11122233</add_key_num>
        </investor>
    </invrec>
</paiInv>

And the output I need includes one extra (single occurrence) tag, named request_id, occurring at the top of the data, like this:

<paiInv>
    <request_id>req44</request_id>
    <invrec>
        <action>add</action>
        <investor>
            <inv_account_class>2</inv_account_class>
            <ext_inv_id>AAA_00005</ext_inv_id>
            <warehouse_id>1001213</warehouse_id>
            <name>Company 1</name>
            <cntry_cd>US</cntry_cd>
            <phone>9995555555</phone>
            <addr_str1>1313 Mockingbird Lane</addr_str1>
            <addr_str2></addr_str2>
            <city>New York</city>
            <st>NY</st>
            <zip>10001</zip>
            <add_key_num>44433322</add_key_num>
        </investor>
    </invrec>
    <invrec>
        <action>add</action>
        <investor>
            <inv_account_class>2</inv_account_class>
            <ext_inv_id>BBB_00008</ext_inv_id>
            <warehouse_id>1004312</warehouse_id>
            <name>Company 2</name>
            <cntry_cd>US</cntry_cd>
            <phone>43255511110</phone>
            <addr_str1>Some other address</addr_str1>
            <addr_str2></addr_str2>
            <city>Stamford</city>
            <st>NB</st>
            <zip>44112</zip>
            <add_key_num>11122233</add_key_num>
        </investor>
    </invrec>
</paiInv>

Any suggestions will be appreciated. I haven't been able to get anything other than syntax errors with my attempts to get the extra tag so far.

1 Answers

Before you save the file, try something like:

doc = lxml.etree.ElementTree(results)
ins = lxml.etree.fromstring('<request_id>req44</request_id>')
ins.tail = "\n"
dest = doc.xpath('/paiInv')[0]
dest.insert(0,ins)
print(lxml.etree.tostring(doc).decode())

The output should be what you are looking for.

Related