Python's approach to comparing 2 XML content

Source: Internet
Author: User

This article mainly introduced Python comparison 2 XML content method, involves Python to manipulate the XML file the related skill, needs the friend to be possible to refer to under

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The Xml.etree import ElementTree ok=true main_pid = 10000 loop_depth = 0 def compare_xml (left, right, key_info= '. '): GL Obal loop_depth loop_depth = 1 if loop_depth = = 1:print if Left.tag!= right.tag:print_diff (main_pid, Key_info, ' Diffta G ', Left.tag, Right.tag return if Left.text!= Right.text:print_diff (main_pid, Key_info, ' Difftext ', Left.text, Right.te XT) Return leftitems = Dict (Left.items ()) Rightitems = Dict (Right.items ()) to K,v in Leftitems.items (): If K to right items:s = '%s/%s '% (Key_info, Left.tag) Print_diff (main_pid, S, ' lostattr ', K, "") for K,v in Rightitems.items (): If K n OT in leftitems:s = '%s/%s '% (Key_info, Right.tag) Print_diff (main_pid, S, ' extraattr ', "", k) leftnodes = Left.getchild ren () rightnodes = RigHt.getchildren () Leftlen = Len (leftnodes) Rightlen = Len (rightnodes) if Leftlen!= rightlen:s = '%s/%s '% (Key_info, righ T.tag) Print_diff (main_pid, S, ' Difflen ', Leftlen, rightlen) return L = Leftlen<rightlen and Leftlen or Rightlen d = {} For I in Xrange (l): node=leftnodes[i] If Node.tag not in d:d[node.tag] = 1 Tag = Node.tag Else:tag = node.tag + str (d[n Ode.tag] D[node.tag] + = 1 s = '%s/%s '% (key_info, tag) compare_xml (Leftnodes[i], rightnodes[i], s) def Print_diff (main_p IDs, Key_info, MSG, Base_type, Test_type): global OK info = u ' [%-5s]%s->%-40s [%s!=%s] '% (Msg.upper (), Main_pid, Key_info.strip ('./'), Base_type, Test_type) print info.encode (' GBK ') OK = False

Call:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26 if __name__ = = ' __main__ ': S1 = ' "' <?xml version=" 1.0 "encoding=" UTF-8 "?> <employees> <employee id = ' 1 ' & Gt <name>linux</name> <age>30</age> </employee> <employee id = ' 2 ' > <name> windows</name> <age>20</age> </employee> </employees> ' s2 = ' <?xml version= 1.0 "encoding=" UTF-8 "?> <employees> <employee id = ' 3 ' > <name>windows</name> <age>20 </age> </employee> <employee id = ' 4 ' > <name>linux</name> <age>30</age> < /employee> </employees> ' lroot = elementtree.fromstring (S1) rroot = elementtree.fromstring (s2) compare_xml ( Lroot, Rroot)

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.