Skip to content Skip to sidebar Skip to footer

Updating Xml Node Value Using The Condition In Python

In the below XML, I want to parse it and update the value of 'PolicyId' to some random value for example 'POL111112NGJ' and 'TransactionDate' to current date and time ONLY IF IT ME

Solution 1:

Here's a relatively simple solution, but one which uses lxml instead of xml.etree.ElementTree. For the sake of simplicity, I just focus exclusively on changing node values; obviously you'll have to adopt it for your other requirements.

policy = """[your xml above]"""#if parsing from an xml stringfrom lxml import etree

doc = etree.XML(policy.encode()) #if parsing from an xml string
doc = etree.parse(r'path\to\your\file\policy.xml') #if parsing from a file
replacements = ["some random policy number","some random date"]

targets = doc.xpath('//ROW[PolicyId="POL000002NGJ"]')
for target in targets:
    target.xpath('./PolicyId')[0].text = replacements[0]
    target.xpath('.//TransactionDate')[0].text = replacements[1]
print(etree.tostring(doc).decode())
# or to save to a new file:
doc.write('new_policy.xml', pretty_print=True, xml_declaration=True,   encoding="utf-8")

Output:

<TABLE><ROW><PolicyId>some random number</PolicyId><BusinessCoverageCode>COV00002D3X1</BusinessCoverageCode><TransactionDate>some random date</TransactionDate></ROW><ROW><PolicyId>some random number</PolicyId><BusinessCoverageCode>COV00002D3X1</BusinessCoverageCode><TransactionDate>some random date</TransactionDate></ROW><ROW><PolicyId>some random number</PolicyId><BusinessCoverageCode>COV00002D3X4</BusinessCoverageCode><TransactionDate>some random date</TransactionDate></ROW><ROW><PolicyId>POL111111NGJ</PolicyId><BusinessCoverageCode>COV00002D3X4</BusinessCoverageCode><TransactionDate>2020-03-23T10:56:15.00</TransactionDate></ROW></TABLE>

Solution 2:

The code below uses python ElementTree XML lib. (not using an external lib).

It uses xpath and find the ROW having a specific PolicyId value.

It updates the date to the current date.

import xml.etree.ElementTree as ET

import datetime

xml = '''<TABLE><ROW><PolicyId>some random number</PolicyId><BusinessCoverageCode>COV00002D3X1</BusinessCoverageCode><TransactionDate>some random date</TransactionDate></ROW><ROW><PolicyId>some random number</PolicyId><BusinessCoverageCode>COV00002D3X1</BusinessCoverageCode><TransactionDate>some random date</TransactionDate></ROW><ROW><PolicyId>some random number</PolicyId><BusinessCoverageCode>COV00002D3X4</BusinessCoverageCode><TransactionDate>some random date</TransactionDate></ROW><ROW><PolicyId>POL111111NGJ</PolicyId><BusinessCoverageCode>COV00002D3X4</BusinessCoverageCode><TransactionDate>2020-03-23T10:56:15.00</TransactionDate></ROW></TABLE>
'''

root = ET.fromstring(xml)
rows_to_update = root.findall(".//ROW/[PolicyId='POL111111NGJ']")
for row in rows_to_update:
    row.find('TransactionDate').text = str(datetime.datetime.now())
ET.dump(root)

Post a Comment for "Updating Xml Node Value Using The Condition In Python"