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 một lượng dữ liệu nhỏ đến trung bình và được sử dụng rộng rãi để chia sẻ thông tin có cấu trúc. Python cho phép bạn phân tích và sửa đổi các tài liệu XML. Để phân tích tài liệu XML, bạn cần có toàn bộ tài liệu XML trong bộ nhớ. Trong hướng dẫn này, chúng ta sẽ xem cách chúng ta có thể sử dụng lớp XML Minidom trong Python để tải và phân tích các tệp XML. Chúng tôi đã tạo một tệp XML mẫu mà chúng tôi sẽ phân tích. Bước 1) Tạo tệp XML mẫu Bên trong tập tin, chúng ta có thể thấy tên, họ, nhà và lĩnh vực chuyên môn (SQL, Python, thử nghiệm và kinh doanh) Bước 2) Sử dụng hàm phân tích cú pháp để tải và phân tích tệp XML Khi chúng tôi đã phân tích cú pháp tài liệu, chúng tôi sẽ in ra tên nút của tên gốc của gốc của tài liệu và tên đầu tiên của trò chơi. TagName và Nodename là các thuộc tính tiêu chuẩn của tệp XML.“node name” of the root of the document and the “firstchild tagname”. Tagname and nodename are the standard properties of the XML file.
Ghi chú:: Tên gật đầu và tên con là tên hoặc thuộc tính tiêu chuẩn của XML DOM. Bước 3) Gọi danh sách các thẻ XML từ tài liệu XML và được in ra Tiếp theo, chúng tôi cũng có thể gọi danh sách các thẻ XML từ tài liệu XML và được in ra. Ở đây chúng tôi đã in ra tập hợp các kỹ năng như SQL, Python, thử nghiệm và kinh doanh.
Chúng ta có thể tạo một thuộc tính mới bằng cách sử dụng chức năng created createdEement và sau đó nối phần thuộc tính hoặc thẻ mới này vào các thẻ XML hiện có. Chúng tôi đã thêm một thẻ mới, Bigdata, trong tệp XML của chúng tôi.
Ví dụ trình phân tích cú pháp XMLVí dụ Python 2 import xml.dom.minidom def main(): # use the parse() function to load and parse an XML file doc = xml.dom.minidom.parse("Myxml.xml"); # print out the document node and the name of the first child tag print doc.nodeName print doc.firstChild.tagName # get a list of XML tags from the document and print each one expertise = doc.getElementsByTagName("expertise") print "%d expertise:" % expertise.length for skill in expertise: print skill.getAttribute("name") #Write a new XML tag and add it into the document newexpertise = doc.createElement("expertise") newexpertise.setAttribute("name", "BigData") doc.firstChild.appendChild(newexpertise) print " " expertise = doc.getElementsByTagName("expertise") print "%d expertise:" % expertise.length for skill in expertise: print skill.getAttribute("name") if name == "__main__": main(); Ví dụ Python 3 import xml.dom.minidom def main(): # use the parse() function to load and parse an XML file doc = xml.dom.minidom.parse("Myxml.xml"); # print out the document node and the name of the first child tag print (doc.nodeName) print (doc.firstChild.tagName) # get a list of XML tags from the document and print each one expertise = doc.getElementsByTagName("expertise") print ("%d expertise:" % expertise.length) for skill in expertise: print (skill.getAttribute("name")) # Write a new XML tag and add it into the document newexpertise = doc.createElement("expertise") newexpertise.setAttribute("name", "BigData") doc.firstChild.appendChild(newexpertise) print (" ") expertise = doc.getElementsByTagName("expertise") print ("%d expertise:" % expertise.length) for skill in expertise: print (skill.getAttribute("name")) if __name__ == "__main__": main(); Cách phân tích XML bằng cách sử dụng ElementTreeElementTree là API để thao tác XML. ElementTree là cách dễ dàng để xử lý các tệp XML. Chúng tôi đang sử dụng tài liệu XML sau làm dữ liệu mẫu: <data> <items> <item name="expertise1">SQL</item> <item name="expertise2">Python</item> </items> </data> Đọc XML bằng ElementTree: Trước tiên chúng ta phải nhập mô -đun xml.etree.elementtree. import xml.etree.ElementTree as ET Bây giờ, hãy để lấy phần tử gốc: root = tree.getroot() Sau đây là mã hoàn chỉnh để đọc dữ liệu XML trên import xml.etree.ElementTree as ET tree = ET.parse('items.xml') root = tree.getroot() # all items data print('Expertise Data:') for elem in root: for subelem in elem: print(subelem.text) output: Expertise Data: SQL Python Summary:Python cho phép bạn phân tích toàn bộ tài liệu XML tại một lần và không chỉ một dòng tại một thời điểm. Để phân tích tài liệu XML, bạn cần có toàn bộ tài liệu trong bộ nhớ.
|