]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/XmlRoutines.py
9d1ff4a0e32cb43f2fc04d3d6b95b1f04cfe6025
3 # This is an XML API that uses a syntax similar to XPath, but it is written in
4 # standard python so that no extra python packages are required to use it.
8 def XmlList(Dom
, String
):
9 """Get a list of XML Elements using XPath style syntax."""
10 if Dom
.nodeType
==Dom
.DOCUMENT_NODE
:
11 return XmlList(Dom
.documentElement
, String
)
13 return XmlList(Dom
, String
[1:])
16 TagList
= String
.split('/')
18 if Dom
.nodeType
== Dom
.ELEMENT_NODE
and Dom
.tagName
.strip() == TagList
[0]:
22 restOfPath
= "/".join(TagList
[1:])
23 for child
in Dom
.childNodes
:
24 nodes
= nodes
+ XmlList(child
, restOfPath
)
27 def XmlElement (Dom
, String
):
28 """Return a single element that matches the String which is XPath style syntax."""
30 return XmlList (Dom
, String
)[0].firstChild
.data
.strip(' ')
34 def XmlElementData (Dom
):
35 """Get the text for this element."""
36 return Dom
.firstChild
.data
.strip(' ')
38 def XmlAttribute (Dom
, String
):
39 """Return a single attribute that named by String."""
41 return Dom
.getAttribute(String
)
46 """Return the name of the Root or top tag in the XML tree."""
47 return Dom
.documentElement
.nodeName
50 # This acts like the main() function for the script, unless it is 'import'ed into another
52 if __name__
== '__main__':
54 # Nothing to do here. Could do some unit tests.