xml etree not writing changes to file

186 Views Asked by At

I'm working on a program that will open an XML file, alter a few values, and save it as a new file. When I run it, it looks like everything is fine. I use print to look at what the system sees when I run the program, and the print command shows the correct value, but when I look at the actual file it generated, it hasn't written this value.

A lot of the code isn't actually about writing the file, and it's a little bit convoluted, but this is the part that actually does the file alterations.

from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
import xml.etree.ElementTree as etree
import os
HOA = MeasureUtility.convertToInches(HOA)
    HOA = HOA+buryDepth
    oldHOA = MeasureUtility.convertToInches(oldHOA)
    fullOldHOA = int(oldHOA+buryDepth)
    poleChild = pole.find("PPLChildElements")
    insulators = poleChild.findall("Insulator")

    for i in insulators:
        insulatorAttr = i.find("ATTRIBUTES")
        insulatorValues = insulatorAttr.findall("VALUE")

        for iv in insulatorValues:
            if iv.attrib["NAME"] == "CoordinateZ":
                height = int(round(float(iv.text)))
                print("The calculated height: {0} || The researched height: {1}".format(fullOldHOA, height))

                if fullOldHOA == height:
                    iv.text = (str(HOA))
                    print("Insulator Height: {0}".format(etree.tostring(iv)))
                else:
                    continue
        
        

mytree.write(AftersFolder + "\\" + title)

I'm not sure if the xml file is relevant, but if it's deemed important, I'd be happy to post it, it's just long, and ultimately my issue is that where I adjust iv.text, and then write the file, the text in that tag remains exactly the same in the saved file as it was in the original.

Edit: The full code for this aspect of the program is below.

from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
import xml.etree.ElementTree as etree
import os
import MeasureUtility
#MeasureUtility is a module I made to affect normal measurements, such as converting a measurement to inches, to feet, or simply adjusting how a measurement value is perceived.
import numpy

def InstallOrChange(HOA, jobName, scid, oldHOA = 0):
    global PPLXFile
    if PPLXFile == "" or PPLXFile is None:
        return
    global root
    title = "{0}_{1}_AFTER.pplx".format(jobName, scid)
    buryDepth = 0

    mytree = etree.parse(PPLXFile)
    myroot = mytree.getroot()
    root = myroot
    scene = myroot.find("PPLScene")
    rootChild = scene.find("PPLChildElements")
    pole = rootChild.find("WoodPole")
    poleattr = pole.find("ATTRIBUTES")
    poleStats = poleattr.findall("VALUE")
    for x in poleStats:
        if x.attrib["NAME"] == "BuryDepthInInches":
            buryDepth = float(x.text)
    print(scid)
    if oldHOA == 0:
        #We are not looking for an old HOA, so don't worry!
        child = CreateNewElement("PPLChildElements", pole)
        Insulator = CreateNewElement("Insulator", child)
        Attr = CreateNewElement("ATTRIBUTES", Insulator)
        #Create new elements within XML document here
        #This part is working fine, I have no issues with this.
        
    else:
        HOA = MeasureUtility.convertToInches(HOA)
        HOA = HOA+buryDepth
        oldHOA = MeasureUtility.convertToInches(oldHOA)
        fullOldHOA = int(oldHOA+buryDepth)
        poleChild = pole.find("PPLChildElements")
        insulators = poleChild.findall("Insulator")

        for i in insulators:
            insulatorAttr = i.find("ATTRIBUTES")
            insulatorValues = insulatorAttr.findall("VALUE")

            for iv in insulatorValues:
                if iv.attrib["NAME"] == "CoordinateZ":
                    height = int(round(float(iv.text)))
                    print("The calculated height: {0} || The researched height: {1}".format(fullOldHOA, height))

                    if fullOldHOA == height:
                        iv.text = (str(HOA))
                        print("The old height: {0}, Insulator Height: {1}".format(height, etree.tostring(iv)))
                        #When this print command is run, it prints the appropriate response, and shows that the text has been altered.
                    else:
                        continue
            
            

    mytree.write(AftersFolder + "\\" + title)
    
    

And the code that calls that function:

for b in boltHoles:
        boltHoleMin = b-.34
        boltHoleMax = b+.34
        if HOA == b:
            HOA = b
        if HOA > boltHoleMax:
            HOA = HOA
        if HOA < boltHoleMax and HOA > b:
            HOA = b
        if HOA < b and HOA > boltHoleMin:
            HOA = boltHoleMin
        if index == 0:
            PPLXUtility.InstallOrChange(HOA, jobName, scid)
        else:
            PPLXUtility.InstallOrChange(HOA, jobName, scid, b)
0

There are 0 best solutions below