0

I want to change the publicationDateTime="2023-07-31T07:02:59+00:00" attribute. My xml is

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Research xmlns="http://www.rixml.org/2005/3/RIXML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" createDateTime="2023-07-31T07:02:16+00:00" language="eng" researchID="GPS-4409687-0" xsi:schemaLocation="http://www.rixml.org/2005/3/RIXML http://www.rixml.org/assets/documents/schemas/RIXML-2_4.xsd">  
  <Product productID="12345-0">  
    <Source> 
      <Organization primaryIndicator="Yes" type="SellSideFirm"> 
        <OrganizationID idType="VendorCode">ABP</OrganizationID>  
        <OrganizationName nameType="Display">ABCDF</OrganizationName>  
      </Organization> 
    </Source>  
    <Content> 
      <Title>Novice</Title>  
    </Content>  
    <Context external="Yes">  
      <ProductDetails periodicalIndicator="No" publicationDateTime="2023-07-31T07:02:59+00:00"> 
        <ProductCategory productCategory="Support"/>  
      </ProductDetails>  
    </Context> 
  </Product> 
</Research>

This is my code

import os
import xml.etree.ElementTree as ET
import uuid
import time

ET.register_namespace('', "http://www.rixml.org/2005/3/RIXML")
ET.register_namespace('', "http://www.rixml.org/2005/3/RIXML")
OUTPUT_FOLDER = "OUTPUT/"
input_folder = "INPPUT/"
all_files = os.listdir(input_folder)
json_files = {f: f for f in all_files if f.endswith(".xml")}
json_files_keys = list(json_files.keys())
json_files_keys.sort()

for file_name in json_files_keys:
    print(file_name)
    xmlTree = ET.parse(input_folder+file_name)
    root = xmlTree.getroot()
    print(root)
    print(root.attrib)
    for child in root:
        print(child.attrib)
        pid = '2023-08-04T08:02:59+00:00'
        print(pid)
        child.set('publicationDateTime', pid)
    xmlTree.write(OUTPUT_FOLDER+file_name)
    print("written")

I am not able to update the attribute. It gets added at the root level.

Please suggest how to add at the same location.

I am new to Python apology if its very obvious question.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Sudarshan kumar
  • 1,503
  • 4
  • 36
  • 83

1 Answers1

0

You can search with find(xPath) the attrib and set it to a new time value:

import xml.etree.ElementTree as ET

filename = 'date_sample.xml'
tree = ET.parse(filename)
root = tree.getroot()

# Register the namespace
namespaces = {node[0]: node[1] for event, node in ET.iterparse(filename, events=['start-ns'])}
print("My Namespaces:", namespaces,'\n')
for ns in namespaces:
    ET.register_namespace(ns, namespaces[ns])
    
# New time_value set:    
pid = '2023-08-04T08:02:59+00:00'
time_value = root.find('.//{*}ProductDetails[@publicationDateTime]')
print(time_value.get('publicationDateTime'))
time_value.set('publicationDateTime', pid)
print(time_value.get('publicationDateTime'))

# Write changes to a new xml-file with right xml declaration 
new_filename = "date_sample_new.xml"
xmlDeclaration = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n'

tree1 = ET.ElementTree(root)
ET.indent(tree1, space= '  ')
tree1.write(new_filename)

with open(new_filename, 'r') as xml_tree: data = xml_tree.read()
with open(new_filename, 'w') as xml_plus_dec: xml_plus_dec.write(xmlDeclaration + data)
Hermann12
  • 1,709
  • 2
  • 5
  • 14