]>
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 Dom
.nodeType
==Dom
.DOCUMENT_NODE
:
20 return XmlList(Dom
.documentElement
, String
)
22 return XmlList(Dom
, String
[1:])
25 TagList
= String
.split('/')
27 if Dom
.nodeType
== Dom
.ELEMENT_NODE
and Dom
.tagName
.strip() == TagList
[0]:
31 restOfPath
= "/".join(TagList
[1:])
32 for child
in Dom
.childNodes
:
33 nodes
= nodes
+ XmlList(child
, restOfPath
)
36 def XmlElement (Dom
, String
):
37 """Return a single element that matches the String which is XPath style syntax."""
39 return XmlList (Dom
, String
)[0].firstChild
.data
.strip()
43 def XmlElementData (Dom
):
44 """Get the text for this element."""
45 return Dom
.firstChild
.data
.strip()
47 def XmlAttribute (Dom
, AttName
):
48 """Return a single attribute named AttName."""
50 return Dom
.getAttribute(AttName
)
55 """Return the name of the Root or top tag in the XML tree."""
56 return Dom
.firstChild
.nodeName
58 def XmlParseFile (FileName
):
59 """Parse an XML file into a DOM and return the DOM."""
61 f
= open(FileName
, 'r')
62 Dom
= xml
.dom
.minidom
.parse(f
)
66 return xml
.dom
.minidom
.parseString('<empty/>')
68 def XmlParseFileSection (FileName
, Tag
):
69 """Parse a section of an XML file into a DOM(Document Object Model) and return the DOM."""
71 f
= open(FileName
, 'r')
73 return xml
.dom
.minidom
.parseString('<empty/>')
75 End
= '</' + Tag
+ '>'
77 while File
.find(Start
) < 0 or File
.find(End
) < 0:
78 Section
= f
.read(0x1000)
83 if File
.find(Start
) < 0 or File
.find(End
) < 0:
84 return xml
.dom
.minidom
.parseString('<empty/>')
85 File
= File
[File
.find(Start
):File
.find(End
)+len(End
)]
87 return xml
.dom
.minidom
.parseString(File
)
89 return xml
.dom
.minidom
.parseString('<empty/>')
91 def XmlSaveFile (Dom
, FileName
, Encoding
='UTF-8'):
92 """Save a DOM(Document Object Model) into an XML file."""
94 f
= open(FileName
, 'w')
95 f
.write(Dom
.toxml(Encoding
).replace('"','"').replace('>','>'))
101 def XmlRemoveElement(Node
):
102 """Remove an element node from DOM(Document Object Model) tree."""
103 ParentNode
= Node
.parentNode
104 if ParentNode
== None:
106 PreviousSibling
= Node
.previousSibling
107 while PreviousSibling
!= None and PreviousSibling
.nodeType
== PreviousSibling
.TEXT_NODE
and PreviousSibling
.data
.strip() == '':
108 Temp
= PreviousSibling
109 PreviousSibling
= PreviousSibling
.previousSibling
110 ParentNode
.removeChild(Temp
)
111 ParentNode
.removeChild(Node
)
114 def XmlAppendChildElement(ParentNode
, TagName
, ElementText
='', AttributeDictionary
= {}):
115 """Add a child element to a DOM(Document Object Model) tree with optional Attributes."""
116 TagName
= TagName
.strip()
121 while Dom
!= None and Dom
.nodeType
!= Dom
.DOCUMENT_NODE
:
126 ParentNode
.appendChild(Dom
.createTextNode('\n%*s' % (Depth
* 2, '')))
127 ElementNode
= Dom
.createElement(TagName
)
128 if ElementText
!= '':
129 ElementNode
.appendChild(Dom
.createTextNode(ElementText
))
130 for Item
in AttributeDictionary
:
131 ElementNode
.setAttribute(Item
, AttributeDictionary
[Item
])
132 ParentNode
.appendChild(ElementNode
)
136 # This acts like the main() function for the script, unless it is 'import'ed into another
138 if __name__
== '__main__':
140 # Nothing to do here. Could do some unit tests.