Python method to get arbitrary XML node values

Source: Internet
Author: User
Tags tagname
The example in this article describes how Python obtains arbitrary XML node values. Share to everyone for your reference. The implementation method is as follows:

#-*-coding:utf-8-*-import Xml.dom.minidomELEMENT_NODE = Xml.dom.Node.ELEMENT_NODEclass Simplexmlgetter (object): Def   __init__ (self, data): If type (data) = = Str:self.root = Xml.dom.minidom.parse (data) Else:self.root = data def __getattr__ (self, name): #support.  Operation if name = = ' Data ': Return Self.root.firstChild.data for C in Self.root.childNodes:if C.nodetype = = Element_node and C.tagname = Name:return Simplexmlgetter (c) def __getitem__ (self, Index): #support [] Ope Ration enodes = [E for E in self.root.parentNode.childNodes if e.nodetype = = Element_node and E.tagname = = SE Lf.root.tagName] Return Simplexmlgetter (Enodes[index]) def __call__ (self, *args, **kwargs): #support () openration, fo R query conditions for E in self.root.parentNode.childNodes:if e.nodetype = = Element_node:for key in Kwar Gs.keys (): If E.getattribute (key)! = Kwargs[key]: Break Else:retUrn Simplexmlgetter (e) if __name__ = = "__main__": x = Simplexmlgetter ("Sysd.xml") print X.sysd.sysagent.param[2].data PR int X.sysd.sysagent.param (name= "QueryTimeout", type= "second"). Data

Hopefully this article will help you with Python programming.

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.