]>
Commit | Line | Data |
---|---|---|
77afa5eb | 1 | #!/usr/bin/env python |
2 | ||
3b7a53b6 | 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 | |
8 | # | |
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. | |
11 | ||
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.""" | |
77afa5eb | 14 | |
15 | import xml.dom.minidom | |
16 | ||
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) | |
21 | if String[0] == "/": | |
22 | return XmlList(Dom, String[1:]) | |
23 | if String == "" : | |
24 | return [] | |
25 | TagList = String.split('/') | |
26 | nodes = [] | |
27 | if Dom.nodeType == Dom.ELEMENT_NODE and Dom.tagName.strip() == TagList[0]: | |
28 | if len(TagList) == 1: | |
29 | nodes = [Dom] | |
30 | else: | |
31 | restOfPath = "/".join(TagList[1:]) | |
32 | for child in Dom.childNodes: | |
33 | nodes = nodes + XmlList(child, restOfPath) | |
34 | return nodes | |
35 | ||
36 | def XmlElement (Dom, String): | |
37 | """Return a single element that matches the String which is XPath style syntax.""" | |
38 | try: | |
8d4243f1 | 39 | return XmlList (Dom, String)[0].firstChild.data.strip() |
77afa5eb | 40 | except: |
41 | return '' | |
42 | ||
43 | def XmlElementData (Dom): | |
44 | """Get the text for this element.""" | |
8d4243f1 | 45 | return Dom.firstChild.data.strip() |
77afa5eb | 46 | |
3b7a53b6 | 47 | def XmlAttribute (Dom, AttName): |
48 | """Return a single attribute named AttName.""" | |
77afa5eb | 49 | try: |
3b7a53b6 | 50 | return Dom.getAttribute(AttName) |
77afa5eb | 51 | except: |
52 | return '' | |
53 | ||
e853a9d4 | 54 | def XmlTopTag(Dom): |
55 | """Return the name of the Root or top tag in the XML tree.""" | |
8d4243f1 | 56 | return Dom.firstChild.nodeName |
57 | ||
58 | def XmlParseFile (FileName): | |
59 | """Parse an XML file into a DOM and return the DOM.""" | |
60 | try: | |
61 | f = open(FileName, 'r') | |
62 | Dom = xml.dom.minidom.parse(f) | |
63 | f.close() | |
64 | return Dom | |
65 | except: | |
66 | return xml.dom.minidom.parseString('<empty/>') | |
67 | ||
68 | def XmlParseFileSection (FileName, Tag): | |
69 | """Parse a section of an XML file into a DOM(Document Object Model) and return the DOM.""" | |
70 | try: | |
71 | f = open(FileName, 'r') | |
72 | except: | |
73 | return xml.dom.minidom.parseString('<empty/>') | |
74 | Start = '<' + Tag | |
75 | End = '</' + Tag + '>' | |
76 | File = '' | |
77 | while File.find(Start) < 0 or File.find(End) < 0: | |
78 | Section = f.read(0x1000) | |
79 | if Section == '': | |
80 | break | |
81 | File += Section | |
82 | f.close() | |
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)] | |
86 | try: | |
87 | return xml.dom.minidom.parseString(File) | |
88 | except: | |
89 | return xml.dom.minidom.parseString('<empty/>') | |
90 | ||
91 | def XmlSaveFile (Dom, FileName, Encoding='UTF-8'): | |
92 | """Save a DOM(Document Object Model) into an XML file.""" | |
93 | try: | |
94 | f = open(FileName, 'w') | |
95 | f.write(Dom.toxml(Encoding).replace('"','"').replace('>','>')) | |
96 | f.close() | |
97 | return True | |
98 | except: | |
99 | return False | |
100 | ||
101 | def XmlRemoveElement(Node): | |
102 | """Remove an element node from DOM(Document Object Model) tree.""" | |
103 | ParentNode = Node.parentNode | |
104 | if ParentNode == None: | |
105 | return False | |
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) | |
112 | return True | |
113 | ||
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() | |
117 | if TagName == '': | |
118 | return False | |
119 | Depth = 0 | |
120 | Dom = ParentNode | |
121 | while Dom != None and Dom.nodeType != Dom.DOCUMENT_NODE: | |
122 | Dom = Dom.parentNode | |
123 | Depth += 1 | |
124 | if Dom == None: | |
125 | return False | |
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) | |
133 | return True | |
e853a9d4 | 134 | |
135 | ||
77afa5eb | 136 | # This acts like the main() function for the script, unless it is 'import'ed into another |
137 | # script. | |
138 | if __name__ == '__main__': | |
139 | ||
140 | # Nothing to do here. Could do some unit tests. | |
141 | pass |