]>
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 - 2014, 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
19 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
21 ## Create a element of XML
26 # @param AttributeList
30 def CreateXmlElement(Name
, String
, NodeList
, AttributeList
):
31 Doc
= xml
.dom
.minidom
.Document()
32 Element
= Doc
.createElement(Name
)
33 if String
!= '' and String
!= None:
34 Element
.appendChild(Doc
.createTextNode(String
))
37 if type(Item
) == type([]):
40 if Key
!= '' and Key
!= None and Value
!= '' and Value
!= None:
41 Node
= Doc
.createElement(Key
)
42 Node
.appendChild(Doc
.createTextNode(Value
))
43 Element
.appendChild(Node
)
45 Element
.appendChild(Item
)
46 for Item
in AttributeList
:
49 if Key
!= '' and Key
!= None and Value
!= '' and Value
!= None:
50 Element
.setAttribute(Key
, Value
)
54 ## Get a list of XML nodes using XPath style syntax.
56 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
57 # If the input Dom or String is not valid, then an empty list is returned.
59 # @param Dom The root XML DOM node.
60 # @param String A XPath style path.
62 # @revel Nodes A list of XML nodes matching XPath style Sting.
64 def XmlList(Dom
, String
):
65 if String
== None or String
== "" or Dom
== None or Dom
== "":
67 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
68 Dom
= Dom
.documentElement
71 TagList
= String
.split('/')
74 End
= len(TagList
) - 1
78 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
80 ChildNodes
.extend(Node
.childNodes
)
82 ChildNodes
.append(Node
)
90 ## Get a single XML node using XPath style syntax.
92 # Return a single XML DOM node from the root Dom specified by XPath String.
93 # If the input Dom or String is not valid, then an empty string is returned.
95 # @param Dom The root XML DOM node.
96 # @param String A XPath style path.
98 # @revel Node A single XML node matching XPath style Sting.
100 def XmlNode(Dom
, String
):
101 if String
== None or String
== "" or Dom
== None or Dom
== "":
103 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
104 Dom
= Dom
.documentElement
107 TagList
= String
.split('/')
109 End
= len(TagList
) - 1
112 for Node
in ChildNodes
:
113 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== TagList
[Index
]:
115 ChildNodes
= Node
.childNodes
123 ## Get a single XML element using XPath style syntax.
125 # Return a single XML element from the root Dom specified by XPath String.
126 # If the input Dom or String is not valid, then an empty string is returned.
128 # @param Dom The root XML DOM object.
129 # @param Strin A XPath style path.
131 # @revel Element An XML element matching XPath style Sting.
133 def XmlElement(Dom
, String
):
135 return XmlNode(Dom
, String
).firstChild
.data
.strip()
140 ## Get a single XML element of the current node.
142 # Return a single XML element specified by the current root Dom.
143 # If the input Dom is not valid, then an empty string is returned.
145 # @param Dom The root XML DOM object.
147 # @revel Element An XML element in current root Dom.
149 def XmlElementData(Dom
):
151 return Dom
.firstChild
.data
.strip()
156 ## Get a list of XML elements using XPath style syntax.
158 # Return a list of XML elements from the root Dom specified by XPath String.
159 # If the input Dom or String is not valid, then an empty list is returned.
161 # @param Dom The root XML DOM object.
162 # @param String A XPath style path.
164 # @revel Elements A list of XML elements matching XPath style Sting.
166 def XmlElementList(Dom
, String
):
167 return map(XmlElementData
, XmlList(Dom
, String
))
170 ## Get the XML attribute of the current node.
172 # Return a single XML attribute named Attribute from the current root Dom.
173 # If the input Dom or Attribute is not valid, then an empty string is returned.
175 # @param Dom The root XML DOM object.
176 # @param Attribute The name of Attribute.
178 # @revel Element A single XML element matching XPath style Sting.
180 def XmlAttribute(Dom
, Attribute
):
182 return Dom
.getAttribute(Attribute
).strip()
187 ## Get the XML node name of the current node.
189 # Return a single XML node name from the current root Dom.
190 # If the input Dom is not valid, then an empty string is returned.
192 # @param Dom The root XML DOM object.
194 # @revel Element A single XML element matching XPath style Sting.
196 def XmlNodeName(Dom
):
198 return Dom
.nodeName
.strip()
202 ## Parse an XML file.
204 # Parse the input XML file named FileName and return a XML DOM it stands for.
205 # If the input File is not a valid XML file, then an empty string is returned.
207 # @param FileName The XML file name.
209 # @revel Dom The Dom object achieved from the XML file.
211 def XmlParseFile(FileName
):
213 XmlFile
= open(FileName
)
214 Dom
= xml
.dom
.minidom
.parse(XmlFile
)
221 # This acts like the main() function for the script, unless it is 'import'ed
222 # into another script.
223 if __name__
== '__main__':
224 # Nothing to do here. Could do some unit tests.
225 A
= CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])
226 B
= CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])
227 C
= CreateXmlList('DDD', 'EEE', [A
, B
], ['FFF', 'GGG'])
228 print C
.toprettyxml(indent
= " ")