0

I can see that the values are updating but when I check that in XMl file it shows original values , How can I update original XML file with edits or create a new XML file with edits. Here is what I am doing

with open("C:/file/abcd.xml","r") as file_in:
    content = file_in.read()
soup = bs(content,"xml")

loc = re.compile(r'[A-Z]+-+[0-9]+/+SM+-+[1-9]$')
for i in soup.find_all('managedOb', dist=loc):
    locat=i.find('p',{'name':'modLoc'})
    locat.string="U.S.A"

here is the example of my XML tags:

Original

<managedOb dist="AKAS-85409/MNOP-85409/SM-1" operation="OPEN">
      <p name="modLoc">India</p>
</managedOb >

Change I am doing

<managedOb dist="AKAS-85409/MNOP-85409/SM-1" operation="OPEN">
          <p name="modLoc">U.S.A</p>
    </managedOb >

But I am not able to save the changes or create a new file with changes.

Akash Rathor
  • 109
  • 8

1 Answers1

0

Another method

from simplified_scrapy import SimplifiedDoc, utils

# html = utils.getFileContent('C:/file/abcd.xml')
html = '''
<managedOb dist="AKAS-85409/MNOP-85409/SM-1" operation="OPEN">
      <p name="modLoc">India</p>
</managedOb>
'''
doc = SimplifiedDoc(html)
managedObs = doc.getElementsByReg(r'dist="[A-Z]+-[0-9]+/+[A-Z]+-[0-9]+/SM+-+[1-9]"',tag='managedOb')
for i in managedObs:
  locat = i.select('p@name=modLoc')
  locat.setContent('U.S.A') # Edit text
utils.saveFile('C:/file/abcd_test.xml',doc.html)

Result:

<managedOb dist="AKAS-85409/MNOP-85409/SM-1" operation="OPEN">
      <p name="modLoc">U.S.A</p>
</managedOb>
dabingsou
  • 2,469
  • 1
  • 5
  • 8