]>
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 # SPDX-License-Identifier: BSD-2-Clause-Patent
12 from __future__
import print_function
13 import xml
.dom
.minidom
15 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
17 ## Create a element of XML
22 # @param AttributeList
26 def CreateXmlElement(Name
, String
, NodeList
, AttributeList
):
27 Doc
= xml
.dom
.minidom
.Document()
28 Element
= Doc
.createElement(Name
)
29 if String
!= '' and String
is not None:
30 Element
.appendChild(Doc
.createTextNode(String
))
33 if isinstance(Item
, type([])):
36 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
37 Node
= Doc
.createElement(Key
)
38 Node
.appendChild(Doc
.createTextNode(Value
))
39 Element
.appendChild(Node
)
41 Element
.appendChild(Item
)
42 for Item
in AttributeList
:
45 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
46 Element
.setAttribute(Key
, Value
)
50 ## Get a list of XML nodes using XPath style syntax.
52 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
53 # If the input Dom or String is not valid, then an empty list is returned.
55 # @param Dom The root XML DOM node.
56 # @param String A XPath style path.
58 # @revel Nodes A list of XML nodes matching XPath style Sting.
60 def XmlList(Dom
, String
):
61 if String
is None or String
== "" or Dom
is None or Dom
== "":
63 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
64 Dom
= Dom
.documentElement
67 TagList
= String
.split('/')
70 End
= len(TagList
) - 1
74 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
76 ChildNodes
.extend(Node
.childNodes
)
78 ChildNodes
.append(Node
)
86 ## Get a single XML node using XPath style syntax.
88 # Return a single XML DOM node from the root Dom specified by XPath String.
89 # If the input Dom or String is not valid, then an empty string is returned.
91 # @param Dom The root XML DOM node.
92 # @param String A XPath style path.
94 # @revel Node A single XML node matching XPath style Sting.
96 def XmlNode(Dom
, String
):
97 if String
is None or String
== "" or Dom
is None or Dom
== "":
99 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
100 Dom
= Dom
.documentElement
103 TagList
= String
.split('/')
105 End
= len(TagList
) - 1
108 for Node
in ChildNodes
:
109 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
111 ChildNodes
= Node
.childNodes
119 ## Get a single XML element using XPath style syntax.
121 # Return a single XML element from the root Dom specified by XPath String.
122 # If the input Dom or String is not valid, then an empty string is returned.
124 # @param Dom The root XML DOM object.
125 # @param Strin A XPath style path.
127 # @revel Element An XML element matching XPath style Sting.
129 def XmlElement(Dom
, String
):
131 return XmlNode(Dom
, String
).firstChild
.data
.strip()
136 ## Get a single XML element of the current node.
138 # Return a single XML element specified by the current root Dom.
139 # If the input Dom is not valid, then an empty string is returned.
141 # @param Dom The root XML DOM object.
143 # @revel Element An XML element in current root Dom.
145 def XmlElementData(Dom
):
147 return Dom
.firstChild
.data
.strip()
152 ## Get a list of XML elements using XPath style syntax.
154 # Return a list of XML elements from the root Dom specified by XPath String.
155 # If the input Dom or String is not valid, then an empty list is returned.
157 # @param Dom The root XML DOM object.
158 # @param String A XPath style path.
160 # @revel Elements A list of XML elements matching XPath style Sting.
162 def XmlElementList(Dom
, String
):
163 return map(XmlElementData
, XmlList(Dom
, String
))
166 ## Get the XML attribute of the current node.
168 # Return a single XML attribute named Attribute from the current root Dom.
169 # If the input Dom or Attribute is not valid, then an empty string is returned.
171 # @param Dom The root XML DOM object.
172 # @param Attribute The name of Attribute.
174 # @revel Element A single XML element matching XPath style Sting.
176 def XmlAttribute(Dom
, Attribute
):
178 return Dom
.getAttribute(Attribute
).strip()
183 ## Get the XML node name of the current node.
185 # Return a single XML node name from the current root Dom.
186 # If the input Dom is not valid, then an empty string is returned.
188 # @param Dom The root XML DOM object.
190 # @revel Element A single XML element matching XPath style Sting.
192 def XmlNodeName(Dom
):
194 return Dom
.nodeName
.strip()
198 ## Parse an XML file.
200 # Parse the input XML file named FileName and return a XML DOM it stands for.
201 # If the input File is not a valid XML file, then an empty string is returned.
203 # @param FileName The XML file name.
205 # @revel Dom The Dom object achieved from the XML file.
207 def XmlParseFile(FileName
):
209 XmlFile
= codecs
.open(FileName
,encoding
='utf_8_sig')
210 Dom
= xml
.dom
.minidom
.parse(XmlFile
)
213 except Exception as X
:
217 # This acts like the main() function for the script, unless it is 'import'ed
218 # into another script.
219 if __name__
== '__main__':
220 # Nothing to do here. Could do some unit tests.
221 A
= CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])
222 B
= CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])
223 C
= CreateXmlList('DDD', 'EEE', [A
, B
], ['FFF', 'GGG'])
224 print(C
.toprettyxml(indent
= " "))