]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/XmlRoutines.py
2707bcbb978c4fc08411c055cedf6a01e27b82cf
3 # This is an XML API that uses a syntax similar to XPath, but it is written in
4 # standard python so that no extra python packages are required to use it.
8 def XmlList(Dom
, String
):
9 """Get a list of XML Elements using XPath style syntax."""
10 if Dom
.nodeType
==Dom
.DOCUMENT_NODE
:
11 return XmlList(Dom
.documentElement
, String
)
13 return XmlList(Dom
, String
[1:])
16 TagList
= String
.split('/')
18 if Dom
.nodeType
== Dom
.ELEMENT_NODE
and Dom
.tagName
.strip() == TagList
[0]:
22 restOfPath
= "/".join(TagList
[1:])
23 for child
in Dom
.childNodes
:
24 nodes
= nodes
+ XmlList(child
, restOfPath
)
27 def XmlElement (Dom
, String
):
28 """Return a single element that matches the String which is XPath style syntax."""
30 return XmlList (Dom
, String
)[0].firstChild
.data
.strip()
34 def XmlElementData (Dom
):
35 """Get the text for this element."""
36 return Dom
.firstChild
.data
.strip()
38 def XmlAttribute (Dom
, String
):
39 """Return a single attribute that named by String."""
41 return Dom
.getAttribute(String
)
46 """Return the name of the Root or top tag in the XML tree."""
47 return Dom
.firstChild
.nodeName
49 def XmlParseFile (FileName
):
50 """Parse an XML file into a DOM and return the DOM."""
52 f
= open(FileName
, 'r')
53 Dom
= xml
.dom
.minidom
.parse(f
)
57 return xml
.dom
.minidom
.parseString('<empty/>')
59 def XmlParseFileSection (FileName
, Tag
):
60 """Parse a section of an XML file into a DOM(Document Object Model) and return the DOM."""
62 f
= open(FileName
, 'r')
64 return xml
.dom
.minidom
.parseString('<empty/>')
66 End
= '</' + Tag
+ '>'
68 while File
.find(Start
) < 0 or File
.find(End
) < 0:
69 Section
= f
.read(0x1000)
74 if File
.find(Start
) < 0 or File
.find(End
) < 0:
75 return xml
.dom
.minidom
.parseString('<empty/>')
76 File
= File
[File
.find(Start
):File
.find(End
)+len(End
)]
78 return xml
.dom
.minidom
.parseString(File
)
80 return xml
.dom
.minidom
.parseString('<empty/>')
82 def XmlSaveFile (Dom
, FileName
, Encoding
='UTF-8'):
83 """Save a DOM(Document Object Model) into an XML file."""
85 f
= open(FileName
, 'w')
86 f
.write(Dom
.toxml(Encoding
).replace('"','"').replace('>','>'))
92 def XmlRemoveElement(Node
):
93 """Remove an element node from DOM(Document Object Model) tree."""
94 ParentNode
= Node
.parentNode
95 if ParentNode
== None:
97 PreviousSibling
= Node
.previousSibling
98 while PreviousSibling
!= None and PreviousSibling
.nodeType
== PreviousSibling
.TEXT_NODE
and PreviousSibling
.data
.strip() == '':
99 Temp
= PreviousSibling
100 PreviousSibling
= PreviousSibling
.previousSibling
101 ParentNode
.removeChild(Temp
)
102 ParentNode
.removeChild(Node
)
105 def XmlAppendChildElement(ParentNode
, TagName
, ElementText
='', AttributeDictionary
= {}):
106 """Add a child element to a DOM(Document Object Model) tree with optional Attributes."""
107 TagName
= TagName
.strip()
112 while Dom
!= None and Dom
.nodeType
!= Dom
.DOCUMENT_NODE
:
117 ParentNode
.appendChild(Dom
.createTextNode('\n%*s' % (Depth
* 2, '')))
118 ElementNode
= Dom
.createElement(TagName
)
119 if ElementText
!= '':
120 ElementNode
.appendChild(Dom
.createTextNode(ElementText
))
121 for Item
in AttributeDictionary
:
122 ElementNode
.setAttribute(Item
, AttributeDictionary
[Item
])
123 ParentNode
.appendChild(ElementNode
)
127 # This acts like the main() function for the script, unless it is 'import'ed into another
129 if __name__
== '__main__':
131 # Nothing to do here. Could do some unit tests.