Python Forum
Overwrite values in XML file with values from another XML file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Overwrite values in XML file with values from another XML file
#4
There are two access methods shown below:
  1. process_using_defusedxml this uses an etree, but not xml.etree.ElementTree which is very unsafe, venerable to attacks
    Quote:Note XML is not safe, see: https://docs.python.org/3/library/xml.ht...rabilities use defusedxml instead install with pip: 'pip install defusedxml see GitHub: https://github.com/tiran/defusedxml

  2. process_using_bs4 this is (my) preferred method, and as far as I know safe. It uses BeautifulSoup4 to parse the input.

Using the second method, you can be rearrange into a class with appropriate update methods

from pathlib import Path
import os

def process_using_defusedxml(filename):
    import defusedxml.ElementTree as ET

    def tree_walk(root, level=0):
        indent = " " * (4 * level)
        for child in root:
            print(f"\n{indent}Type(child): {type(child)}")
            print(f"\n{indent}tag: {child.tag}")
            print(f"    {indent}attribute: {child.attrib}")
            print(f"    {indent}text: {child.text}")
            level += 1
            tree_walk(child)

    tree = ET.parse(filename)
    root = tree.getroot()

    tree_walk(root)
    
# alternative method using Beautiful Soup
def process_using_bs4(filename):
    from bs4 import BeautifulSoup

    with filename.open('r') as fp:        
        xmldata = fp.read()
        soup = BeautifulSoup(xmldata, 'lxml')
        module = soup.find('module')
        modulename = module.get('bs')
        print(f"Module Name: {modulename}")

        objects = soup.find_all('object')
        print(f"\nobjects:")
        for n, obj in enumerate(objects):
            print(f"\nobject_id: {obj.get('id')} object name: {obj.get('name')}" \
                f" object number: {obj.get('number')}")
            items = obj.find_all('item')
            if items:
                print()
                for n1, item in enumerate(items):
                    if item:
                        print(f"    item number: {n1} name: {item.get('name')} " \
                            f"value: {item.get('value')}")

os.chdir(os.path.abspath(os.path.dirname(__file__)))
filename = Path('.') / 'Mainfile_1.xml'

# process_using_defusedxml(filename)
process_using_bs4(filename)
BeautifulSoup4 (bs4) method results:
Output:
Module Name: Mainfile_1 objects: object_id: 1000 object name: namex object number: 1 item number: 0 name: item0 value: 100 item number: 1 name: item00 value: 100 object_id: 1001 object name: namey object number: 2 item number: 0 name: item1 value: 100 item number: 1 name: item00 value: 100 object_id: 1234 object name: name1 object number: 3 item number: 0 name: item1 value: FAIL item number: 1 name: item2 value: 233 item number: 2 name: item3 value: 233 item number: 3 name: item4 value: FAIL object_id: 1238 object name: name2 object number: 4 item number: 0 name: item8 value: FAIL item number: 1 name: item9 value: 233 object_id: 2345 object name: name32 object number: 5 item number: 0 name: item1 value: 111 item number: 1 name: item2 value: FAIL object_id: 2347 object name: name4 object number: 6 item number: 0 name: item1 value: FAIL item number: 1 name: item2 value: FAIL item number: 2 name: item3 value: 233 item number: 3 name: item4 value: FAIL
This should give you something to work with.
Reply


Messages In This Thread
RE: Overwrite values in XML file with values from another XML file - by Larz60+ - Apr-01-2022, 08:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 1 2,364 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  How to write variable in a python file then import it in another python file? tatahuft 4 1,070 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Assigning cycle values in a list nmancini 3 1,120 Sep-16-2024, 09:35 PM
Last Post: deanhystad
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,203 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 1,245 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  Append from csv to xlsx with values only Sick_Stigma 2 830 Aug-06-2024, 08:05 PM
Last Post: Sick_Stigma
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 7,129 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  remove duplicates from dicts with list values wardancer84 27 6,471 May-27-2024, 04:54 PM
Last Post: wardancer84
Question Using Lists as Dictionary Values bfallert 8 2,499 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Printing out incidence values for Class Object SquderDragon 3 1,365 Apr-01-2024, 07:52 AM
Last Post: SquderDragon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020