]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/XmlRoutines.py
Added some new field to the far template.
[mirror_edk2.git] / Tools / Python / XmlRoutines.py
1 #!/usr/bin/env python
2
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.
5
6 import xml.dom.minidom
7
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)
12 if String[0] == "/":
13 return XmlList(Dom, String[1:])
14 if String == "" :
15 return []
16 TagList = String.split('/')
17 nodes = []
18 if Dom.nodeType == Dom.ELEMENT_NODE and Dom.tagName.strip() == TagList[0]:
19 if len(TagList) == 1:
20 nodes = [Dom]
21 else:
22 restOfPath = "/".join(TagList[1:])
23 for child in Dom.childNodes:
24 nodes = nodes + XmlList(child, restOfPath)
25 return nodes
26
27 def XmlElement (Dom, String):
28 """Return a single element that matches the String which is XPath style syntax."""
29 try:
30 return XmlList (Dom, String)[0].firstChild.data.strip(' ')
31 except:
32 return ''
33
34 def XmlElementData (Dom):
35 """Get the text for this element."""
36 return Dom.firstChild.data.strip(' ')
37
38 def XmlAttribute (Dom, String):
39 """Return a single attribute that named by String."""
40 try:
41 return Dom.getAttribute(String)
42 except:
43 return ''
44
45 def XmlTopTag(Dom):
46 """Return the name of the Root or top tag in the XML tree."""
47 return Dom.documentElement.nodeName
48
49
50 # This acts like the main() function for the script, unless it is 'import'ed into another
51 # script.
52 if __name__ == '__main__':
53
54 # Nothing to do here. Could do some unit tests.
55 pass