Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc stands for Extensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable. That’s why, the design goals of XML emphasize simplicity, generality, and usability across the Internet.
Bàn luận For more information, refer to XML | Basics
XML là viết tắt của ngôn ngữ đánh dấu mở rộng. Nó được thiết kế để lưu trữ và vận chuyển dữ liệu. Nó được thiết kế để có thể đọc được cả con người và máy. Đó là lý do tại sao, các mục tiêu thiết kế của XML nhấn mạnh tính đơn giản, tính tổng quát và khả năng sử dụng trên internet.
Lưu ý: Để biết thêm thông tin, hãy tham khảo XML | Điều cơ bản
Ở đây chúng tôi xem xét rằng tệp XML có trong bộ nhớ. Vui lòng đọc các ý kiến trong mã để hiểu rõ ràng.“test.xml”. Before going further you should know that in XML we do not have predefined tags as we have in HTML. While writing XML the author has to define his/her own tags as well as the document structure. Now we need to parse this file and modify it using Python. We will be using “minidom” library of Python 3 to do the above task. This module does not come built-in with Python. To install this type the below command in the terminal.
pip install minidom
Tệp XML:
Hãy để chúng tôi lưu tệp XML ở trên dưới dạng test test.xml. Trước khi đi xa hơn, bạn nên biết rằng trong XML, chúng tôi không có các thẻ được xác định trước như chúng tôi có trong HTML. Trong khi viết XML, tác giả phải xác định thẻ của riêng mình cũng như cấu trúc tài liệu. Bây giờ chúng ta cần phân tích tệp này và sửa đổi nó bằng Python. Chúng tôi sẽ sử dụng thư viện Python 3 của Minidom Minidom để thực hiện nhiệm vụ trên. Mô-đun này không được tích hợp sẵn với Python. Để cài đặt loại này lệnh dưới đây trong thiết bị đầu cuối.
Example
Đọc XML
Đầu tiên chúng ta sẽ đọc nội dung của tệp XML và sau đó chúng ta sẽ tìm hiểu cách sửa đổi tệp XML.example
import
xml.dom.minidom as md
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
file
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe8
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
file
Jack John Harry3
def
main[]:
file
____10
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe2
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe3
Jack John Harry5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe0
file
Jack John Harry8
Jack John Harry9
print[file.toxml[]]0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
print[file.toxml[]]4
print[file.toxml[]]5
print[file.toxml[]]6
print[file.toxml[]]7
print[file.toxml[]]8
import
0____10 file
Jack John Harry8
import
4 print[file.toxml[]]0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
import
9 print[file.toxml[]]5
xml.dom.minidom as md
1print[file.toxml[]]7
xml.dom.minidom as md
3
xml.dom.minidom as md
5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe0
file
Jack John Harry8
xml.dom.minidom as md
9 print[file.toxml[]]0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
def
4 print[file.toxml[]]5
def
6print[file.toxml[]]7
xml.dom.minidom as md
3
main[]:
0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe0
file
Jack John Harry8
main[]:
4 print[file.toxml[]]0
main[]:
7 main[]:
8main[]:
9
0
file
3
Output::
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe
1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
3
4
5First Name or Last Name we have used firstname[0] / lastname[0]. This is because
there is only 1 “fname” and only 1 “lname” tag. For multiple same tags we can proceed like below.XML:
6
7#document
note
Name: Jack
Surname: Shelby
Favourite Game: Football
Messi
Ronaldo
Mbappe
01010109090991
Đọc XML
Đầu tiên chúng ta sẽ đọc nội dung của tệp XML và sau đó chúng ta sẽ tìm hiểu cách sửa đổi tệp XML.example
import
xml.dom.minidom as md
def
main[]:
file
____10
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe2
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe3
1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe18
main[]:
7 main[]:
8main[]:
9
0
file
3
1#document
note
Name: Jack
Surname: Shelby
Favourite Game: Football
Messi
Ronaldo
Mbappe
5
3
4
5
Jack John Harry
6
7#document
note
Name: Jack
Surname: Shelby
Favourite Game: Football
Messi
Ronaldo
Mbappe
01010109090991
Trong mã python trên trong khi in tên hoặc tên cuối cùng, chúng tôi đã sử dụng FirstName [0] / lastName [0]. Điều này là do chỉ có 1 fname fname và chỉ có 1 thẻ lname. Đối với nhiều thẻ giống nhau, chúng tôi có thể tiến hành như bên dưới.xml:modify an XML file.
XML File:
Pythonadd the following :
file
____10#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe
1#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe
2print[file.toxml[]]
0#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe
05#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe
0file
Jack John Harry
8Jack John Harry
9print[file.toxml[]]
0
main[]:
7
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe13
main[]:
9 #document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe15delete the “hobby” tag. Also let us modify the age to 29.
Python Code:[Modifying XML]
Đọc XML
Đầu tiên chúng ta sẽ đọc nội dung của tệp XML và sau đó chúng ta sẽ tìm hiểu cách sửa đổi tệp XML.example
import
xml.dom.minidom as md
def
main[]:
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe45
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe46
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe47
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe48
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe3
file
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe52
file
____10
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe2
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe3
Jack John Harry5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe0
file
Jack John Harry8
Jack John Harry9
print[file.toxml[]]0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
print[file.toxml[]]4
print[file.toxml[]]5
print[file.toxml[]]6
print[file.toxml[]]7
print[file.toxml[]]8
1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe76
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe77
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe78
import
0____10 file
Jack John Harry8
import
4 print[file.toxml[]]0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
import
9 print[file.toxml[]]5
xml.dom.minidom as md
1print[file.toxml[]]7
xml.dom.minidom as md
3
xml.dom.minidom as md
5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe0
file
Jack John Harry8
xml.dom.minidom as md
9 print[file.toxml[]]0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe6
def
4 print[file.toxml[]]5
def
6print[file.toxml[]]7
xml.dom.minidom as md
3
1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe99
main[]:
0
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe0
file
Jack John Harry8
main[]:
4 print[file.toxml[]]0
main[]:
7 main[]:
8main[]:
9
0
1
Jack John Harry20
file
Jack John Harry22
1
Jack John Harry24
main[]:
7 main[]:
8main[]:
9
0
file
3
Output:
1
#document note Name: Jack Surname: Shelby Favourite Game: Football Messi Ronaldo Mbappe5
3
4
5“file” object into XML using the toxml[] method and writes it to the “test.xml” file. If you do not want to edit the original file and just want to print the modified XML then
replace those 3 lines by:print[file.toxml[]]