]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/Xml/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 - 2018, 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 from __future__
import print_function
19 import xml
.dom
.minidom
21 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
23 ## Create a element of XML
28 # @param AttributeList
32 def CreateXmlElement(Name
, String
, NodeList
, AttributeList
):
33 Doc
= xml
.dom
.minidom
.Document()
34 Element
= Doc
.createElement(Name
)
35 if String
!= '' and String
is not None:
36 Element
.appendChild(Doc
.createTextNode(String
))
39 if isinstance(Item
, type([])):
42 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
43 Node
= Doc
.createElement(Key
)
44 Node
.appendChild(Doc
.createTextNode(Value
))
45 Element
.appendChild(Node
)
47 Element
.appendChild(Item
)
48 for Item
in AttributeList
:
51 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
52 Element
.setAttribute(Key
, Value
)
56 ## Get a list of XML nodes using XPath style syntax.
58 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
59 # If the input Dom or String is not valid, then an empty list is returned.
61 # @param Dom The root XML DOM node.
62 # @param String A XPath style path.
64 # @revel Nodes A list of XML nodes matching XPath style Sting.
66 def XmlList(Dom
, String
):
67 if String
is None or String
== "" or Dom
is None or Dom
== "":
69 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
70 Dom
= Dom
.documentElement
73 TagList
= String
.split('/')
76 End
= len(TagList
) - 1
80 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
82 ChildNodes
.extend(Node
.childNodes
)
84 ChildNodes
.append(Node
)
92 ## Get a single XML node using XPath style syntax.
94 # Return a single XML DOM node from the root Dom specified by XPath String.
95 # If the input Dom or String is not valid, then an empty string is returned.
97 # @param Dom The root XML DOM node.
98 # @param String A XPath style path.
100 # @revel Node A single XML node matching XPath style Sting.
102 def XmlNode(Dom
, String
):
103 if String
is None or String
== "" or Dom
is None or Dom
== "":
105 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
106 Dom
= Dom
.documentElement
109 TagList
= String
.split('/')
111 End
= len(TagList
) - 1
114 for Node
in ChildNodes
:
115 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
117 ChildNodes
= Node
.childNodes
125 ## Get a single XML element using XPath style syntax.
127 # Return a single XML element from the root Dom specified by XPath String.
128 # If the input Dom or String is not valid, then an empty string is returned.
130 # @param Dom The root XML DOM object.
131 # @param Strin A XPath style path.
133 # @revel Element An XML element matching XPath style Sting.
135 def XmlElement(Dom
, String
):
137 return XmlNode(Dom
, String
).firstChild
.data
.strip()
142 ## Get a single XML element of the current node.
144 # Return a single XML element specified by the current root Dom.
145 # If the input Dom is not valid, then an empty string is returned.
147 # @param Dom The root XML DOM object.
149 # @revel Element An XML element in current root Dom.
151 def XmlElementData(Dom
):
153 return Dom
.firstChild
.data
.strip()
158 ## Get a list of XML elements using XPath style syntax.
160 # Return a list of XML elements from the root Dom specified by XPath String.
161 # If the input Dom or String is not valid, then an empty list is returned.
163 # @param Dom The root XML DOM object.
164 # @param String A XPath style path.
166 # @revel Elements A list of XML elements matching XPath style Sting.
168 def XmlElementList(Dom
, String
):
169 return map(XmlElementData
, XmlList(Dom
, String
))
172 ## Get the XML attribute of the current node.
174 # Return a single XML attribute named Attribute from the current root Dom.
175 # If the input Dom or Attribute is not valid, then an empty string is returned.
177 # @param Dom The root XML DOM object.
178 # @param Attribute The name of Attribute.
180 # @revel Element A single XML element matching XPath style Sting.
182 def XmlAttribute(Dom
, Attribute
):
184 return Dom
.getAttribute(Attribute
).strip()
189 ## Get the XML node name of the current node.
191 # Return a single XML node name from the current root Dom.
192 # If the input Dom is not valid, then an empty string is returned.
194 # @param Dom The root XML DOM object.
196 # @revel Element A single XML element matching XPath style Sting.
198 def XmlNodeName(Dom
):
200 return Dom
.nodeName
.strip()
204 ## Parse an XML file.
206 # Parse the input XML file named FileName and return a XML DOM it stands for.
207 # If the input File is not a valid XML file, then an empty string is returned.
209 # @param FileName The XML file name.
211 # @revel Dom The Dom object achieved from the XML file.
213 def XmlParseFile(FileName
):
215 XmlFile
= codecs
.open(FileName
,encoding
='utf_8_sig')
216 Dom
= xml
.dom
.minidom
.parse(XmlFile
)
219 except Exception as X
:
223 # This acts like the main() function for the script, unless it is 'import'ed
224 # into another script.
225 if __name__
== '__main__':
226 # Nothing to do here. Could do some unit tests.
227 A
= CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])
228 B
= CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])
229 C
= CreateXmlList('DDD', 'EEE', [A
, B
], ['FFF', 'GGG'])
230 print(C
.toprettyxml(indent
= " "))