]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/buildgen/XmlRoutines.py
3 # Copyright (c) 2007, Intel Corporation
4 # All rights reserved. This program and the accompanying materials
5 # are licensed and made available under the terms and conditions of the BSD License
6 # which accompanies this distribution. The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 """This is an XML API that uses a syntax similar to XPath, but it is written in
13 standard python so that no extra python packages are required to use it."""
15 import xml
.dom
.minidom
17 def XmlList(Dom
, String
):
18 """Get a list of XML Elements using XPath style syntax."""
19 if String
== "" or Dom
== None or not isinstance(Dom
, xml
.dom
.Node
):
25 if Dom
.nodeType
==Dom
.DOCUMENT_NODE
:
26 Dom
= Dom
.documentElement
28 tagList
= String
.split('/')
32 end
= len(tagList
) - 1
35 if node
.nodeType
== node
.ELEMENT_NODE
and node
.tagName
== tagList
[index
]:
37 childNodes
.extend(node
.childNodes
)
39 childNodes
.append(node
)
47 def XmlElement (Dom
, String
):
48 """Return a single element that matches the String which is XPath style syntax."""
49 if String
== "" or Dom
== None or not isinstance(Dom
, xml
.dom
.Node
):
55 if Dom
.nodeType
==Dom
.DOCUMENT_NODE
:
56 Dom
= Dom
.documentElement
58 tagList
= String
.split('/')
61 end
= len(tagList
) - 1
63 for node
in childNodes
:
64 if node
.nodeType
== node
.ELEMENT_NODE
and node
.tagName
== tagList
[index
]:
66 childNodes
= node
.childNodes
75 def XmlElementData (Dom
):
76 """Get the text for this element."""
77 if Dom
== None or Dom
== '' or Dom
.firstChild
== None:
80 return Dom
.firstChild
.data
.strip(' ')
82 def XmlAttribute (Dom
, String
):
83 """Return a single attribute that named by String."""
84 if Dom
== None or Dom
== '':
88 return Dom
.getAttribute(String
).strip(' ')
93 """Return the name of the Root or top tag in the XML tree."""
94 if Dom
== None or Dom
== '' or Dom
.firstChild
== None:
96 return Dom
.firstChild
.nodeName
99 # This acts like the main() function for the script, unless it is 'import'ed into another
101 if __name__
== '__main__':
103 # Nothing to do here. Could do some unit tests.