]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/XmlRoutines.py
3 # Copyright (c) 2007, Intel Corporation
4 # All rights reserved. This program and the accompanying materials
5 # are licensed and made available under the terms and conditions of the BSD License
6 # which accompanies this distribution. The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 """This is an XML API that uses a syntax similar to XPath, but it is written in
13 standard python so that no extra python packages are required to use it."""
15 import xml
.dom
.minidom
17 def XmlList(Dom
, String
):
18 """Get a list of XML Elements using XPath style syntax."""
19 if String
== None or String
== "" or Dom
== None or Dom
== "":
21 if Dom
.nodeType
==Dom
.DOCUMENT_NODE
:
22 Dom
= Dom
.documentElement
25 tagList
= String
.split('/')
28 end
= len(tagList
) - 1
32 if node
.nodeType
== node
.ELEMENT_NODE
and node
.tagName
== tagList
[index
]:
34 childNodes
.extend(node
.childNodes
)
36 childNodes
.append(node
)
43 def XmlNode (Dom
, String
):
44 """Return a single node that matches the String which is XPath style syntax."""
45 if String
== None or String
== "" or Dom
== None or Dom
== "":
47 if Dom
.nodeType
==Dom
.DOCUMENT_NODE
:
48 Dom
= Dom
.documentElement
51 tagList
= String
.split('/')
53 end
= len(tagList
) - 1
56 for node
in childNodes
:
57 if node
.nodeType
== node
.ELEMENT_NODE
and node
.tagName
== tagList
[index
]:
59 childNodes
= node
.childNodes
66 def XmlElement (Dom
, String
):
67 """Return a single element that matches the String which is XPath style syntax."""
69 return XmlNode (Dom
, String
).firstChild
.data
.strip()
73 def XmlElementData (Dom
):
74 """Get the text for this element."""
75 if Dom
== None or Dom
== '' or Dom
.firstChild
== None:
77 return Dom
.firstChild
.data
.strip()
79 def XmlAttribute (Dom
, AttName
):
80 """Return a single attribute named AttName."""
81 if Dom
== None or Dom
== '':
84 return Dom
.getAttribute(AttName
)
89 """Return the name of the Root or top tag in the XML tree."""
90 return Dom
.firstChild
.nodeName
92 def XmlParseFile (FileName
):
93 """Parse an XML file into a DOM and return the DOM."""
95 f
= open(FileName
, 'r')
96 Dom
= xml
.dom
.minidom
.parse(f
)
100 return xml
.dom
.minidom
.parseString('<empty/>')
102 def XmlParseString (Str
):
103 """Parse an XML string into a DOM and return the DOM."""
105 return xml
.dom
.minidom
.parseString(Str
)
107 return xml
.dom
.minidom
.parseString('<empty/>')
109 def XmlParseFileSection (FileName
, Tag
):
110 """Parse a section of an XML file into a DOM(Document Object Model) and return the DOM."""
112 f
= open(FileName
, 'r')
114 return xml
.dom
.minidom
.parseString('<empty/>')
116 End
= '</' + Tag
+ '>'
118 while File
.find(Start
) < 0 or File
.find(End
) < 0:
119 Section
= f
.read(0x1000)
124 if File
.find(Start
) < 0 or File
.find(End
) < 0:
125 return xml
.dom
.minidom
.parseString('<empty/>')
126 File
= File
[File
.find(Start
):File
.find(End
)+len(End
)]
128 return xml
.dom
.minidom
.parseString(File
)
130 return xml
.dom
.minidom
.parseString('<empty/>')
132 def XmlParseStringSection (XmlString
, Tag
):
133 """Parse a section of an XML string into a DOM(Document Object Model) and return the DOM."""
135 End
= '</' + Tag
+ '>'
137 if File
.find(Start
) < 0 or File
.find(End
) < 0:
138 return xml
.dom
.minidom
.parseString('<empty/>')
139 File
= File
[File
.find(Start
):File
.find(End
)+len(End
)]
141 return xml
.dom
.minidom
.parseString(File
)
143 return xml
.dom
.minidom
.parseString('<empty/>')
145 def XmlSaveFile (Dom
, FileName
, Encoding
='UTF-8'):
146 """Save a DOM(Document Object Model) into an XML file."""
148 f
= open(FileName
, 'w')
149 f
.write(Dom
.toxml(Encoding
).replace('"','"').replace('>','>'))
155 def XmlRemoveElement(Node
):
156 """Remove an element node from DOM(Document Object Model) tree."""
157 ParentNode
= Node
.parentNode
158 if ParentNode
== None:
160 PreviousSibling
= Node
.previousSibling
161 while PreviousSibling
!= None and PreviousSibling
.nodeType
== PreviousSibling
.TEXT_NODE
and PreviousSibling
.data
.strip() == '':
162 Temp
= PreviousSibling
163 PreviousSibling
= PreviousSibling
.previousSibling
164 ParentNode
.removeChild(Temp
)
165 ParentNode
.removeChild(Node
)
168 def XmlAppendChildElement(ParentNode
, TagName
, ElementText
='', AttributeDictionary
= {}):
169 """Add a child element to a DOM(Document Object Model) tree with optional Attributes."""
170 TagName
= TagName
.strip()
175 while Dom
!= None and Dom
.nodeType
!= Dom
.DOCUMENT_NODE
:
180 ParentNode
.appendChild(Dom
.createTextNode('\n%*s' % (Depth
* 2, '')))
181 ElementNode
= Dom
.createElement(TagName
)
182 if ElementText
!= '':
183 ElementNode
.appendChild(Dom
.createTextNode(ElementText
))
184 for Item
in AttributeDictionary
:
185 ElementNode
.setAttribute(Item
, AttributeDictionary
[Item
])
186 ParentNode
.appendChild(ElementNode
)
190 # This acts like the main() function for the script, unless it is 'import'ed into another
192 if __name__
== '__main__':
194 # Nothing to do here. Could do some unit tests.