Phương thức này dùng để tìm kiếm phần tử bên trong file xml và giá trị của nó, tùy thuộc vào loại giá trị mà ta thực hiện tìm kiếm khác nhau. Loại giá trị được tìm kiếm có thể là giá trị thuộc tính phần tử và giá trị nút. Ngoài ra, chúng tôi có thêm một vài tham số trong trường hợp chúng tôi có nhiều phiên bản của cùng một thẻ trong xml
Đây là một ví dụ xml
123654 1100 99 2500 123456 03000 00218 0 2550 50 0 0 0
Đây là mã python để phân tích tệp xml trước đó
# Logger setup logger = logging.getLogger['parseXML'] # Location for log file hdlr = logging.FileHandler['parseXML.log'] formatter = logging.Formatter['%[asctime]s %[levelname]s %[message]s'] hdlr.setFormatter[formatter] logger.addHandler[hdlr]
logger.setLevel[logging.DEBUG] # Method returns true and false depending on the result of seached element and its value, parameters used: # 1] path is the location where the zip files will be located # 2] tag is the tag which will be used to search for the node in xml type document # 3] value is the searched value which needs to be checked # 4] attribute is used to search for the attribute whose value will be used to verify [check] # 5] searchName is used to search for the attribute which will help define the right element with specific tag # 6] searchValue is used to check the value for the searched attribute to verify the correct element is used # Mandatory parameters are file [location and name.extension], tag which will be used to search for element, # value which will be used to check if the value is what we expect for the specified element, # attribute, searchName and searchValue will have default values as empty string in case their values are not sent as parameters def parseXML[path, tag, value, attribute="", searchName="", searchValue=""]: #Parsing the file and searching for the initial element xmldoc = minidom.parse[path] element = xmldoc.getElementsByTagName[tag] # 1] Search for element and check node value if [searchName == "" and searchValue == "" and attribute == ""]: elementValue = element[0].childNodes[0].nodeValue if value == elementValue: logging.info["Successfully found element with tag: " + tag + " which has value: " + elementValue + " in file on location : " + path + "."] return True else: logging.info["A problem has been found, the element value is " + elementValue + " the searched value is " + value + " in file on location: " + path + "."] return False # 2] Search for element and check the attribute value if [searchName == "" and searchValue == "" and attribute != ""]: elementValue = element[0].getAttribute[attribute] if value == elementValue: logging.info["Successfully found element with tag: " + tag + " which has attribute: " + attribute + " with value: " + elementValue + " in file on location: " + path + "."] return True else : logging.info["A problem has been found, the attribute value is " + elementValue + " the searched value is " + value + " for element with tag: " + tag + " in file location: " + path + "."] return False # 3] Search for elements with specific tag # Go through each of them and check the attributes and their values if [searchName != "" and searchValue != "" and attribute != ""]: for e in element: valueCheck = e.attributes[searchName].value if searchValue == valueCheck: attributeValue = e.attributes[attribute].value if attributeValue == value: logging.info["Successfully found element with tag: " + tag + " with attribute: " + attribute + " which has value: " + attributeValue + " in file on location: " + path + "."] return True logging.info["A problem has been found, the searched element with tag: " + tag + " with the searched attribute: " + attribute + " and attribute value: " + value + " could not be found in file on location: " + path + "."] return False
# Example call
path = "D:\\TestXML\\" result = parseXML[path,"SameTag","somevalue1","value","name","somevalue1"]
Chúng tôi có một vài trường hợp mã này dành cho
- Chúng tôi tìm kiếm thẻ và giá trị nút đó
- Chúng tôi tìm kiếm thẻ và một thuộc tính bên trong thẻ và kiểm tra giá trị của thuộc tính
- Chúng tôi tìm kiếm thẻ có nhiều phiên bản có cùng tên, vì vậy chúng tôi sử dụng một thuộc tính và giá trị của nó để xem qua tất cả các phiên bản để tìm phiên bản chúng tôi cần, sau đó chúng tôi tìm kiếm thuộc tính thực tế mà chúng tôi cần và kiểm tra giá trị của nó
Trong ví dụ xml, bạn có thể tìm thấy tất cả các trường hợp được lưu ý. Đối với hai trường hợp đầu tiên, bạn có thể lấy bất kỳ thẻ nào bạn muốn, đối với trường hợp cuối cùng, thẻ có tên "SameTag" hoặc "Function" có thể được sử dụng
Bạn có thể thay đổi mã để bao gồm nhiều trường hợp hơn hoặc một số trường hợp tương tự theo nhu cầu của bạn. Tệp nhật ký có thể được thay đổi hoặc xóa nếu bạn không cần nhật ký. Mã trả về true hoặc false dựa trên việc phần tử có được tìm thấy hay không