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. cây etree. Elementtree to change XML file. xml. cây etree. ElementTree để thay đổi tệp xml
Nội dung chính Hiển thị
- Số lượng phần tử trẻ em đến một công cụ gốc
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. Trước tiên, tôi sẽ xem 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
69 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ông cụ 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] lấy nguồn dữ liệu mới từ tệp CSV bằng cách đọc tệp thành chuỗi, [2] xóa chuỗi ở một số dấu phân cách định trước, [3] kết nối vào danh sách và sau đó
Tôi đã xem trong tài liệu về
69 và thấy các chức năng của
72 và
73, 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ó
74 sẽ được thay thế/cập nhật
Kết quả dự kiến 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 to change XML base to update with a item from list []?Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Ngôn ngữ đánh dấu mở rộng [XML] là ngôn ngữ đánh dấu xác định một bộ quy tắc để mã hóa tài liệu ở định dạng mà con người có thể đọc được và máy có thể đọc được. Mục tiêu thiết kế của XML tập trung vào tính đơn giản, tính tổng quát và khả năng sử dụng trên Internet. Nó 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ữ khác nhau của con người. Mặc dù thiết kế của XML tập trung vào các tài liệu, ngôn ngữ này được sử dụng rộng rãi để biểu diễn các cấu trúc dữ liệu tùy ý, chẳng hạn như các cấu trúc được sử dụng trong các dịch vụ web.
XML vốn dĩ là một định dạng dữ liệu phân cấp và cách tự nhiên nhất để biểu diễn nó là dưới dạng cây. Để thực hiện bất kỳ thao tác 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. cây etree. Yếu TốCây. Nó 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 thao tác. Phần tử đại diện cho một nút duy nhất trong cây này. Đọc và viết từ toàn bộ tài liệu được thực hiện ở cấp độ ElementTree. Các tương tác với một phần tử XML đơn lẻ và các phần tử con của nó được thực hiện ở cấp Phần tử.
Bàn luận
Trăn. Sửa đổi/phân tích cú pháp XML & NBSP; . 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 tập tin XML tập trung vào các tài liệu, ngôn ngữ được sử dụng rộng rãi để có thể thực hiện các cấu trúc dữ liệu tùy chọn 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à theo cách tự nhiên nhất để thể hiện nó là một cây. Để thực hiện bất kỳ hoạt động nào như cú pháp phân tích, tìm kiếm, sửa đổi tệp XML, chúng tôi sử dụng modulexml. cây etree. nguyên tố cây. nó 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 đạ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ử.  Thuộc tính của phần tử. Sự kiện mô tả đặc tínhCó thể được truy cập bằng cách sử dụng tên phần tử. nhãn. NhãnChuỗi xác định loại dữ liệu mà phần tử đại diện. You can access by ElementName. nhãn.
Có thể truy cập theo tên phần tử. thuộc tính. Thuộc tính số lượng được lưu trữ dưới dạng từ điển python. can can access by ElementName. thuộc tính. String textChuỗ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
Chúng ta có thể phân tích cú pháp dữ liệu XML từ một chuỗi hoặc một tài liệu XML. Đang xem xét xml. cây etree. ElementTree dưới dạng ET.
1. ET. phân tích cú pháp ['Tên tệp']. getroot[] -ET. parse['fname']-tạo một cây và sau đó chúng tôi giải nén gốc bằng cách. getroot[].
2. ET. fromstring[stringname] -Để tạo một thư mục gốc từ một chuỗi dữ liệu XML.
Ví dụ 1.
Tài liệu XML.
Số lượng phần tử trẻ em đến một công cụ gốc
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. Xem xét XML. ETREE. ElementTree is et. 1. vân vân. phân tích cú pháp ['tên tệp,]. vân vân. FromString [StringName] -để tạo một gốc từ chuỗi dữ liệu XML. ví dụ 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]
7____18____19XML
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]
1import 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]
2import 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]
3import 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]
4import 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]
5import 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______98
2import 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
0import 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
3import 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
5import 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
7import 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
3import 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
3import 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
0import 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
3import 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
696import 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
8import 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]
8import 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]
9import 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]
47import 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
3import 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_______2693
3import 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
696
6 Python3
0
8
222
697import 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]
4Mã Python.
695
696
699import 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
721____2722____2723
730
731
730
733đầu ra
đầu raví dụ1
724
7261]Phần tử. iter[‘tag’] -Lặp lại trên tất cả các phần tử con[Phần tử cây con]
2]Phần tử. findall[‘tag’] -Chỉ tìm các phần tử có thẻ là phần tử con trực tiếp của phần tử hiện tại.
3]Phần tử. find[‘tag’] -Tìm phần tử con đầu tiên với thẻ cụ thể.
4]Phần tử. get[‘tag’] -Truy cập thuộc tính phần tử.
5]Phần tử. văn bản -Cho văn bản của phần tử.
6]Phần tử. attrib-trả về tất cả các thuộc tính hiện có.
7]Phần tử. tag-trả về tên phần tử.
Ví dụ 2.
Python3
0
8
222
697import 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]
4Mã Python.
695
696
0____2730____101
699import 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
721____2722____2723
724import 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
726
727import 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
729____20
730import 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đầu ra
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; . . văn bản -gives Văn bản của phần tử. . attrib-returns tất cả các thuộc tính có mặt. . tag-returns name section tử. ví dụ 2.