]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/XmlRoutines.py
2 # This is an XML API that uses a syntax similar to XPath, but it is written in
3 # standard python so that no extra python packages are required to use it.
5 # Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 import xml
.dom
.minidom
20 ## Create a element of XML
25 # @param AttributeList
29 def CreateXmlElement(Name
, String
, NodeList
, AttributeList
):
30 Doc
= xml
.dom
.minidom
.Document()
31 Element
= Doc
.createElement(Name
)
32 if String
!= '' and String
!= None:
33 Element
.appendChild(Doc
.createTextNode(String
))
36 if type(Item
) == type([]):
39 if Key
!= '' and Key
!= None and Value
!= '' and Value
!= None:
40 Node
= Doc
.createElement(Key
)
41 Node
.appendChild(Doc
.createTextNode(Value
))
42 Element
.appendChild(Node
)
44 Element
.appendChild(Item
)
45 for Item
in AttributeList
:
48 if Key
!= '' and Key
!= None and Value
!= '' and Value
!= None:
49 Element
.setAttribute(Key
, Value
)
53 ## Get a list of XML nodes using XPath style syntax.
55 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
56 # If the input Dom or String is not valid, then an empty list is returned.
58 # @param Dom The root XML DOM node.
59 # @param String A XPath style path.
61 # @revel Nodes A list of XML nodes matching XPath style Sting.
63 def XmlList(Dom
, String
):
64 if String
== None or String
== "" or Dom
== None or Dom
== "":
66 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
67 Dom
= Dom
.documentElement
70 TagList
= String
.split('/')
73 End
= len(TagList
) - 1
77 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
79 ChildNodes
.extend(Node
.childNodes
)
81 ChildNodes
.append(Node
)
89 ## Get a single XML node using XPath style syntax.
91 # Return a single XML DOM node from the root Dom specified by XPath String.
92 # If the input Dom or String is not valid, then an empty string is returned.
94 # @param Dom The root XML DOM node.
95 # @param String A XPath style path.
97 # @revel Node A single XML node matching XPath style Sting.
99 def XmlNode(Dom
, String
):
100 if String
== None or String
== "" or Dom
== None or Dom
== "":
102 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
103 Dom
= Dom
.documentElement
106 TagList
= String
.split('/')
108 End
= len(TagList
) - 1
111 for Node
in ChildNodes
:
112 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
114 ChildNodes
= Node
.childNodes
122 ## Get a single XML element using XPath style syntax.
124 # Return a single XML element from the root Dom specified by XPath String.
125 # If the input Dom or String is not valid, then an empty string is returned.
127 # @param Dom The root XML DOM object.
128 # @param Strin A XPath style path.
130 # @revel Element An XML element matching XPath style Sting.
132 def XmlElement(Dom
, String
):
134 return XmlNode(Dom
, String
).firstChild
.data
.strip()
139 ## Get a single XML element of the current node.
141 # Return a single XML element specified by the current root Dom.
142 # If the input Dom is not valid, then an empty string is returned.
144 # @param Dom The root XML DOM object.
146 # @revel Element An XML element in current root Dom.
148 def XmlElementData(Dom
):
150 return Dom
.firstChild
.data
.strip()
155 ## Get a list of XML elements using XPath style syntax.
157 # Return a list of XML elements from the root Dom specified by XPath String.
158 # If the input Dom or String is not valid, then an empty list is returned.
160 # @param Dom The root XML DOM object.
161 # @param String A XPath style path.
163 # @revel Elements A list of XML elements matching XPath style Sting.
165 def XmlElementList(Dom
, String
):
166 return map(XmlElementData
, XmlList(Dom
, String
))
169 ## Get the XML attribute of the current node.
171 # Return a single XML attribute named Attribute from the current root Dom.
172 # If the input Dom or Attribute is not valid, then an empty string is returned.
174 # @param Dom The root XML DOM object.
175 # @param Attribute The name of Attribute.
177 # @revel Element A single XML element matching XPath style Sting.
179 def XmlAttribute(Dom
, Attribute
):
181 return Dom
.getAttribute(Attribute
).strip()
186 ## Get the XML node name of the current node.
188 # Return a single XML node name from the current root Dom.
189 # If the input Dom is not valid, then an empty string is returned.
191 # @param Dom The root XML DOM object.
193 # @revel Element A single XML element matching XPath style Sting.
195 def XmlNodeName(Dom
):
197 return Dom
.nodeName
.strip()
201 ## Parse an XML file.
203 # Parse the input XML file named FileName and return a XML DOM it stands for.
204 # If the input File is not a valid XML file, then an empty string is returned.
206 # @param FileName The XML file name.
208 # @revel Dom The Dom object achieved from the XML file.
210 def XmlParseFile(FileName
):
212 XmlFile
= open(FileName
)
213 Dom
= xml
.dom
.minidom
.parse(XmlFile
)
220 # This acts like the main() function for the script, unless it is 'import'ed
221 # into another script.
222 if __name__
== '__main__':
223 # Nothing to do here. Could do some unit tests.
224 A
= CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])
225 B
= CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])
226 C
= CreateXmlList('DDD', 'EEE', [A
, B
], ['FFF', 'GGG'])
227 print C
.toprettyxml(indent
= " ")