Hướng dẫn python update xml element value - python cập nhật giá trị phần tử xml

Chúng tôi đã thấy cách phân tích tệp XML bằng Python ở đây. Trong bài đăng này, chúng tôi sẽ thảo luận về việc thay đổi giá trị phần tử của tệp XML bằng Python. Chúng tôi sẽ sử dụng mô -đun xml.etree.elementtree để thay đổi tệp XML.xml.etree.ElementTree in order to make changes to the xml file.

Hãy để chúng tôi tạo một tệp XML bằng cách sử dụng nội dung dưới đây và đặt tên cho nó là ví dụ.xml.

  
  
    
    Python Tutorial  
    Beginner  
    2018  
    30.00  
    
    
    SQL Server  
    Advanced  
    2019  
    45.00  
      
  

Mục tiêu của chúng tôi là thay đổi giá của cuốn sách hướng dẫn của Python Python từ 30,00 lên 50,00. Đầu tiên chúng tôi sẽ lặp qua tất cả các cuốn sách và tìm cuốn sách có tiêu đề phù hợp và sau đó áp dụng các thay đổi của chúng tôi.

import xml.etree.ElementTree as ET
filename = "example.xml"
xmlTree = ET.parse(filename)
rootElement = xmlTree.getroot()
for element in rootElement.findall("book"):
    #Find the book that has title as 'Python Tutorial'
    if element.find('title').text == 'Python Tutorial' :
        #Change the price
        element.find('price').text = "50.00"
#Write the modified xml file.        
xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)

Một điều quan trọng cần lưu ý là mã trên sẽ ghi đè lên tệp XML gốc với giá trị được cập nhật.

Tôi đang cố gắng sử dụng thư viện ElementTree của Python 2.7 để phân tích tệp XML, sau đó thay thế các thuộc tính phần tử cụ thể bằng dữ liệu thử nghiệm, sau đó lưu đây là tệp XML duy nhất.

Ý tưởng của tôi về một giải pháp là (1) nguồn dữ liệu mới từ tệp CSV bằng cách đọc tệp thành chuỗi, (2) cắt chuỗi ở một số dấu phân cách nhất định, (3) nối vào danh sách, và sau đó (4) sử dụng ElementTree để cập nhật/xóa/thay thế thuộc tính bằng một giá trị cụ thể từ danh sách.

Tôi đã xem trong tài liệu ElementTree và thấy các chức năng clear()remove(), nhưng tôi không biết về cú pháp để sử dụng chúng một cách đầy đủ.

Một ví dụ về XML để sửa đổi là bên dưới - các thuộc tính có XXXXX sẽ được thay thế/cập nhật:


    
        
    

Kết quả dự định sẽ là, ví dụ:


    
        
    

Làm cách nào để sử dụng các lệnh

import xml.etree.ElementTree as ET
filename = "example.xml"
xmlTree = ET.parse(filename)
rootElement = xmlTree.getroot()
for element in rootElement.findall("book"):
    #Find the book that has title as 'Python Tutorial'
    if element.find('title').text == 'Python Tutorial' :
        #Change the price
        element.find('price').text = "50.00"
#Write the modified xml file.        
xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
0 để thay đổi XML cơ sở để cập nhật với một mục từ danh sách []?

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc
    Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.The design goals of XML focus on simplicity, generality, and usability across the Internet.It is a textual data format with strong support via Unicode for different human languages. Although the design of XML focuses on documents, the language is widely used for the representation of arbitrary data structures such as those used in web services.
    XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree.To perform any operations like parsing, searching, modifying an XML file we use a modulexml.etree.ElementTree.It has two classes.ElementTree represents the whole XML document as a tree which helps while performing the operations. Element represents a single node in this tree.Reading and writing from the whole document are done on the ElementTree level.Interactions with a single XML element and its sub-elements are done on the Element level.
     

    Bàn luận 

    Python | Sửa đổi/phân tích cú pháp XML & NBSP; Ngôn ngữ đánh dấu có thể mở rộng (XML) là một ngôn ngữ đánh dấu xác định một tập hợp các quy tắc để mã hóa các tài liệu theo định dạng vừa có thể đọc được và có thể đọc được. và khả năng sử dụng trên Internet. Đó là một định dạng dữ liệu văn bản với sự hỗ trợ mạnh mẽ thông qua Unicode cho các ngôn ngữ con người khác nhau. Mặc dù thiết kế của XML tập trung vào các tài liệu, ngôn ngữ được sử dụng rộng rãi để thể hiện các cấu trúc dữ liệu tùy ý như các cấu trúc được sử dụng trong dịch vụ web.xml là một định dạng dữ liệu phân cấp vốn có và cách tự nhiên nhất để thể hiện nó là với một cây. Để thực hiện bất kỳ hoạt động nào như phân tích cú pháp, tìm kiếm, sửa đổi tệp XML, chúng tôi sử dụng modulexml.etree.elementtree.it có hai lớp.elementtree đại diện cho toàn bộ tài liệu XML dưới dạng cây giúp thực hiện các hoạt động. Phần tử đại diện cho một nút duy nhất trong cây này. Đọc và ghi từ toàn bộ tài liệu được thực hiện ở cấp phần tử. Xuất hiện với một phần tử XML duy nhất và các phần tử phụ của nó được thực hiện ở cấp phần tử. & NBSP;Thuộc tính của phần tử: & nbsp;
    Đặc tínhSự mô tả
    Can be accessed using elementname.tag.
    NhãnChuỗi xác định loại dữ liệu mà phần tử đại diện. Có thể truy cập bằng ElementName.tag.
    Can be accesses by elementname.attrib.
    Số lượng thuộc tínhĐược lưu trữ dưới dạng từ điển python.can có thể truy cập bởi ElementName.Attrib.
    Chuỗi văn bảnChuỗi thông tin liên quan đến phần tử.
    Chuỗi trẻ emThông tin chuỗi yếu tố con tùy chọn.

    Yếu tố trẻ em
    We can parse XML data from a string or an XML document.Considering xml.etree.ElementTree as ET.
    1. ET.parse(‘Filename’).getroot() -ET.parse(‘fname’)-creates a tree and then we extract the root by .getroot().
    2. ET.fromstring(stringname) -To create a root from an XML data string.
    Example 1: 
    XML document:
     

    Số lượng phần tử trẻ em đến một gốc cụ thể.

    Phân tích cú pháp: Chúng ta có thể phân tích dữ liệu XML từ một chuỗi hoặc tài liệu XML.Considing XML.ETREE.ElementTree là et.1. Et.parse (‘fileName,). Et.FromString (StringName) -để tạo một gốc từ chuỗi dữ liệu XML.example 1: & nbsp; tài liệu XML: & nbsp;

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    XML

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    1
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    2
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    5
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    6

    
        
            
        
    
    
    0
    
        
            
        
    
    
    8
    
        
            
        
    
    
    2
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    2
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    5
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    9
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    2
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    5
    
        
            
        
    
    
    6

    
        
            
        
    
    
    0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    2
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4ElementTree6
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    9
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4ElementTree3
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4ElementTree6
    
        
            
        
    
    
    6
     
     

    Python3

    
        
            
        
    
    
    0
    
        
            
        
    
    
    8
    
        
            
        
    
    
    222

    ElementTree7

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4

    Mã Python: & nbsp; & nbsp;

    ElementTree5 ElementTree6

    ElementTree9

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4 clear()1clear()2clear()3

    remove()0remove()1

    remove()0remove()3

    Output:

    Hướng dẫn python update xml element value - python cập nhật giá trị phần tử xml

    outputexample1

    clear()4

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4 clear()6
    1)Element.iter(‘tag’) -Iterates over all the child elements(Sub-tree elements) 
    2)Element.findall(‘tag’) -Finds only elements with a tag which are direct children of the current element. 
    3)Element.find(‘tag’) -Finds the first Child with the particular tag. 
    4)Element.get(‘tag’) -Accesses the elements attributes. 
    5)Element.text -Gives the text of the element. 
    6)Element.attrib-returns all the attributes present. 
    7)Element.tag-returns the element name.
    Example 2: 
     

    Python3

    
        
            
        
    
    
    0
    
        
            
        
    
    
    8
    
        
            
        
    
    
    222

    ElementTree7

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4

    Mã Python: & nbsp; & nbsp;

    ElementTree5 ElementTree6

    
        
            
        
    
    
    0remove()0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    01

    ElementTree9

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4 clear()1clear()2clear()3

    clear()4

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4 clear()6

    clear()7

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4 clear()9

    
        
            
        
    
    
    0remove()0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    22

    Output:

    Hướng dẫn python update xml element value - python cập nhật giá trị phần tử xml

    Phương pháp phần tử: 1) phần tử.Iter ('tag') -iterates trên tất cả các phần tử con (các phần tử phụ) & nbsp; 2) phần tử phần tử hiện tại. & nbsp; 3) phần tử. văn bản -gives Văn bản của phần tử. & nbsp; 6) phần tử.attrib-returns tất cả các thuộc tính có mặt. & nbsp; 7) phần tử.tag-returns tên phần tử.example 2: & nbsp; & nbsp;

    clear()4

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4 clear()9
    Modifying the XML document can also be done through Element methods. 
    Methods: 
    1)Element.set(‘attrname’, ‘value’) – Modifying element attributes. 
    2)Element.SubElement(parent, new_childtag) -creates a new child tag under the parent. 
    3)Element.write(‘filename.xml’)-creates the tree of xml into another file. 
    4)Element.pop() -delete a particular attribute. 
    5)Element.remove() -to delete a complete tag.
    Example 3: 
    XML Document: 
     

    XML

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    1
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    2
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    5
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    6

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    30
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    39
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    41
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    42
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    48
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    54

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    55
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    61
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    
        
            
        
    
    
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    39
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    777__178
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    84
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    90

    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    91
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    97
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    
        
            
        
    
    
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    39
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    13
    
        
            
        
    
    
    14
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    
        
            
        
    
    
    20
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    
        
            
        
    
    
    26

    
        
            
        
    
    
    27
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    97
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    
        
            
        
    
    
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    39
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    49
    
        
            
        
    
    
    50
    
        
            
        
    
    
    3
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    
        
            
        
    
    
    56
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    47
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    
        
            
        
    
    
    62

    
        
            
        
    
    
    63
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    53
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    7
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    
        
            
        
    
    
    69
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    60
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    0
    
        
            
        
    
    
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    34
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    
        
            
        
    
    
    8
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    30
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    9

    Mã Python: & nbsp; & nbsp; 
     

    Python3

    ElementTree5 ElementTree6

    
        
            
        
    
    
    81
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4 clear()1
    
        
            
        
    
    
    84clear()3

    
        
            
        
    
    
    86
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    88

    XXXXX1

    
        
            
        
    
    
    90XXXXX3
    
        
            
        
    
    
    92XXXXX5__96 ____295XXXXX8

    
        
            
        
    
    
    0
    
        
            
        
    
    
    98
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    00XXXXX6
    
        
            
        
    
    
    02
    
        
            
        
    
    
    03
    
        
            
        
    
    
    04____
    
        
            
        
    
    
    05clear()3

    
        
            
        
    
    
    0
    
        
            
        
    
    
    08
    
        
            
        
    
    
    09XXXXX6
    
        
            
        
    
    
    11
    
        
            
        
    
    
    12
    
        
            
        
    
    
    13clear()3

    
        
            
        
    
    
    15
    
        
            
        
    
    
    16
    
        
            
        
    
    
    17
    
        
            
        
    
    
    18clear()3

    XXXXX1

    
        
            
        
    
    
    21XXXXX3
    
        
            
        
    
    
    92XXXXX5____96
    
        
            
        
    
    
    18XXXXX8

    
        
            
        
    
    
    0
    
        
            
        
    
    
    29
    import xml.etree.ElementTree as ET
    filename = "example.xml"
    xmlTree = ET.parse(filename)
    rootElement = xmlTree.getroot()
    for element in rootElement.findall("book"):
        #Find the book that has title as 'Python Tutorial'
        if element.find('title').text == 'Python Tutorial' :
            #Change the price
            element.find('price').text = "50.00"
    #Write the modified xml file.        
    xmlTree.write(filename,encoding='UTF-8',xml_declaration=True)
    4
    
        
            
        
    
    
    00XXXXX6__33333333

    
        
            
        
    
    
    35
    
        
            
        
    
    
    36
    
        
            
        
    
    
    37
    
        
            
        
    
    
    16
    
        
            
        
    
    
    39
    
        
            
        
    
    
    40clear()3

    
        
            
        
    
    
    42
    
        
            
        
    
    
    43
    
        
            
        
    
    
    44

    
        
            
        
    
    
    45
    
        
            
        
    
    
    46clear()3

    Đầu ra: & nbsp; & nbsp; 
     

    Hướng dẫn python update xml element value - python cập nhật giá trị phần tử xml