]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/Xml/XmlRoutines.py
4294016ae3213422d614dbc448372621cf1b4804
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
20 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
22 ## Create a element of XML
27 # @param AttributeList
31 def CreateXmlElement(Name
, String
, NodeList
, AttributeList
):
32 Doc
= xml
.dom
.minidom
.Document()
33 Element
= Doc
.createElement(Name
)
34 if String
!= '' and String
is not None:
35 Element
.appendChild(Doc
.createTextNode(String
))
38 if isinstance(Item
, type([])):
41 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
42 Node
= Doc
.createElement(Key
)
43 Node
.appendChild(Doc
.createTextNode(Value
))
44 Element
.appendChild(Node
)
46 Element
.appendChild(Item
)
47 for Item
in AttributeList
:
50 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
51 Element
.setAttribute(Key
, Value
)
55 ## Get a list of XML nodes using XPath style syntax.
57 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
58 # If the input Dom or String is not valid, then an empty list is returned.
60 # @param Dom The root XML DOM node.
61 # @param String A XPath style path.
63 # @revel Nodes A list of XML nodes matching XPath style Sting.
65 def XmlList(Dom
, String
):
66 if String
is None or String
== "" or Dom
is None or Dom
== "":
68 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
69 Dom
= Dom
.documentElement
72 TagList
= String
.split('/')
75 End
= len(TagList
) - 1
79 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
81 ChildNodes
.extend(Node
.childNodes
)
83 ChildNodes
.append(Node
)
91 ## Get a single XML node using XPath style syntax.
93 # Return a single XML DOM node from the root Dom specified by XPath String.
94 # If the input Dom or String is not valid, then an empty string is returned.
96 # @param Dom The root XML DOM node.
97 # @param String A XPath style path.
99 # @revel Node A single XML node matching XPath style Sting.
101 def XmlNode(Dom
, String
):
102 if String
is None or String
== "" or Dom
is None or Dom
== "":
104 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
105 Dom
= Dom
.documentElement
108 TagList
= String
.split('/')
110 End
= len(TagList
) - 1
113 for Node
in ChildNodes
:
114 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
116 ChildNodes
= Node
.childNodes
124 ## Get a single XML element using XPath style syntax.
126 # Return a single XML element from the root Dom specified by XPath String.
127 # If the input Dom or String is not valid, then an empty string is returned.
129 # @param Dom The root XML DOM object.
130 # @param Strin A XPath style path.
132 # @revel Element An XML element matching XPath style Sting.
134 def XmlElement(Dom
, String
):
136 return XmlNode(Dom
, String
).firstChild
.data
.strip()
141 ## Get a single XML element of the current node.
143 # Return a single XML element specified by the current root Dom.
144 # If the input Dom is not valid, then an empty string is returned.
146 # @param Dom The root XML DOM object.
148 # @revel Element An XML element in current root Dom.
150 def XmlElementData(Dom
):
152 return Dom
.firstChild
.data
.strip()
157 ## Get a list of XML elements using XPath style syntax.
159 # Return a list of XML elements from the root Dom specified by XPath String.
160 # If the input Dom or String is not valid, then an empty list is returned.
162 # @param Dom The root XML DOM object.
163 # @param String A XPath style path.
165 # @revel Elements A list of XML elements matching XPath style Sting.
167 def XmlElementList(Dom
, String
):
168 return map(XmlElementData
, XmlList(Dom
, String
))
171 ## Get the XML attribute of the current node.
173 # Return a single XML attribute named Attribute from the current root Dom.
174 # If the input Dom or Attribute is not valid, then an empty string is returned.
176 # @param Dom The root XML DOM object.
177 # @param Attribute The name of Attribute.
179 # @revel Element A single XML element matching XPath style Sting.
181 def XmlAttribute(Dom
, Attribute
):
183 return Dom
.getAttribute(Attribute
).strip()
188 ## Get the XML node name of the current node.
190 # Return a single XML node name from the current root Dom.
191 # If the input Dom is not valid, then an empty string is returned.
193 # @param Dom The root XML DOM object.
195 # @revel Element A single XML element matching XPath style Sting.
197 def XmlNodeName(Dom
):
199 return Dom
.nodeName
.strip()
203 ## Parse an XML file.
205 # Parse the input XML file named FileName and return a XML DOM it stands for.
206 # If the input File is not a valid XML file, then an empty string is returned.
208 # @param FileName The XML file name.
210 # @revel Dom The Dom object achieved from the XML file.
212 def XmlParseFile(FileName
):
214 XmlFile
= open(FileName
)
215 Dom
= xml
.dom
.minidom
.parse(XmlFile
)
218 except Exception as X
:
222 # This acts like the main() function for the script, unless it is 'import'ed
223 # into another script.
224 if __name__
== '__main__':
225 # Nothing to do here. Could do some unit tests.
226 A
= CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])
227 B
= CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])
228 C
= CreateXmlList('DDD', 'EEE', [A
, B
], ['FFF', 'GGG'])
229 print(C
.toprettyxml(indent
= " "))