Trình phân tích cú pháp công thức excel python
XML là viết tắt của eXtensible Markup Language. Nó được thiết kế để lưu trữ và vận chuyển lượng dữ liệu vừa và nhỏ và được sử dụng rộng rãi để chia sẻ thông tin có cấu trúc Show Python cho phép bạn phân tích cú pháp và sửa đổi các tài liệu XML. Để phân tích cú pháp 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 minidom XML trong Python để tải và phân tích các tệp XML Trong hướng dẫn Tệp XML Python này, bạn sẽ học Cách phân tích cú pháp XML bằng minidomChúng tôi đã tạo một tệp XML mẫu mà chúng tôi sẽ phân tích cú pháp Bước 1) Tạo tệp XML mẫu Bên trong tệp, chúng ta có thể thấy tên, họ, quê quán và lĩnh vực chuyên môn (SQL, Python, Kiểm thử và Kinh doanh) Bước 2) Sử dụng chức năng phân tích cú pháp để tải và phân tích cú pháp 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 thư mục gốc của tài liệu và “tên thẻ con đầu tiên”. Tagname và nodename là các thuộc tính tiêu chuẩn của tệp XML
Ghi chú Tên nút và tên thẻ con là tên hoặc thuộc tính tiêu chuẩn của dom XML Bước 3) Gọi danh sách các thẻ XML từ tài liệu XML và in ra Tiếp theo, chúng ta cũng có thể gọi danh sách các thẻ XML từ tài liệu XML và in ra. Ở đây chúng tôi đã in ra bộ kỹ năng như SQL, Python, Kiểm tra và Kinh doanh
Cách viết nút XMLChúng ta có thể tạo một thuộc tính mới bằng cách sử dụng hàm “createElement” rồi nối thêm 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” vào tệp XML của mình
Ví dụ về trình phân tích cú pháp XMLVí dụ về 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ụ về 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 cú pháp XML bằng ElementTreeElementTree là một 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
Đọc XML bằng ElementTree trước tiên chúng ta phải nhập xml. cây etree. Mô-đun ElementTree import xml.etree.ElementTree as ET Bây giờ hãy tìm nạp 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) đầu ra Expertise Data: SQL Python Tóm lượcPython cho phép bạn phân tích cú pháp toàn bộ tài liệu XML cùng một lúc chứ không chỉ một dòng tại một thời điểm. Để phân tích cú pháp tài liệu XML, bạn cần có toàn bộ tài liệu trong bộ nhớ |