]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | # This is an XML API that uses a syntax similar to XPath, but it is written in\r | |
3 | # standard python so that no extra python packages are required to use it.\r | |
4 | #\r | |
1be2ed90 | 5 | # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r |
40d841f6 | 6 | # This program and the accompanying materials\r |
30fdf114 LG |
7 | # are licensed and made available under the terms and conditions of the BSD License\r |
8 | # which accompanies this distribution. The full text of the license may be found at\r | |
9 | # http://opensource.org/licenses/bsd-license.php\r | |
10 | #\r | |
11 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | #\r | |
14 | \r | |
15 | ##\r | |
16 | # Import Modules\r | |
17 | #\r | |
18 | import xml.dom.minidom\r | |
1be2ed90 | 19 | from Common.LongFilePathSupport import OpenLongFilePath as open\r |
30fdf114 LG |
20 | \r |
21 | ## Create a element of XML\r | |
22 | #\r | |
23 | # @param Name\r | |
24 | # @param String\r | |
25 | # @param NodeList\r | |
26 | # @param AttributeList\r | |
27 | #\r | |
28 | # @revel Element\r | |
29 | #\r | |
30 | def CreateXmlElement(Name, String, NodeList, AttributeList):\r | |
31 | Doc = xml.dom.minidom.Document()\r | |
32 | Element = Doc.createElement(Name)\r | |
4231a819 | 33 | if String != '' and String is not None:\r |
30fdf114 LG |
34 | Element.appendChild(Doc.createTextNode(String))\r |
35 | \r | |
36 | for Item in NodeList:\r | |
37 | if type(Item) == type([]):\r | |
38 | Key = Item[0]\r | |
39 | Value = Item[1]\r | |
4231a819 | 40 | if Key != '' and Key is not None and Value != '' and Value is not None:\r |
30fdf114 LG |
41 | Node = Doc.createElement(Key)\r |
42 | Node.appendChild(Doc.createTextNode(Value))\r | |
43 | Element.appendChild(Node)\r | |
44 | else:\r | |
45 | Element.appendChild(Item)\r | |
46 | for Item in AttributeList:\r | |
47 | Key = Item[0]\r | |
48 | Value = Item[1]\r | |
4231a819 | 49 | if Key != '' and Key is not None and Value != '' and Value is not None:\r |
30fdf114 LG |
50 | Element.setAttribute(Key, Value)\r |
51 | \r | |
52 | return Element\r | |
53 | \r | |
54 | ## Get a list of XML nodes using XPath style syntax.\r | |
55 | #\r | |
56 | # Return a list of XML DOM nodes from the root Dom specified by XPath String.\r | |
57 | # If the input Dom or String is not valid, then an empty list is returned.\r | |
58 | #\r | |
59 | # @param Dom The root XML DOM node.\r | |
60 | # @param String A XPath style path.\r | |
61 | #\r | |
62 | # @revel Nodes A list of XML nodes matching XPath style Sting.\r | |
63 | #\r | |
64 | def XmlList(Dom, String):\r | |
4231a819 | 65 | if String is None or String == "" or Dom is None or Dom == "":\r |
30fdf114 LG |
66 | return []\r |
67 | if Dom.nodeType == Dom.DOCUMENT_NODE:\r | |
68 | Dom = Dom.documentElement\r | |
69 | if String[0] == "/":\r | |
70 | String = String[1:]\r | |
71 | TagList = String.split('/')\r | |
72 | Nodes = [Dom]\r | |
73 | Index = 0\r | |
74 | End = len(TagList) - 1\r | |
75 | while Index <= End:\r | |
76 | ChildNodes = []\r | |
77 | for Node in Nodes:\r | |
78 | if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == TagList[Index]:\r | |
79 | if Index < End:\r | |
80 | ChildNodes.extend(Node.childNodes)\r | |
81 | else:\r | |
82 | ChildNodes.append(Node)\r | |
83 | Nodes = ChildNodes\r | |
84 | ChildNodes = []\r | |
85 | Index += 1\r | |
86 | \r | |
87 | return Nodes\r | |
88 | \r | |
89 | \r | |
90 | ## Get a single XML node using XPath style syntax.\r | |
91 | #\r | |
92 | # Return a single XML DOM node from the root Dom specified by XPath String.\r | |
93 | # If the input Dom or String is not valid, then an empty string is returned.\r | |
94 | #\r | |
95 | # @param Dom The root XML DOM node.\r | |
96 | # @param String A XPath style path.\r | |
97 | #\r | |
98 | # @revel Node A single XML node matching XPath style Sting.\r | |
99 | #\r | |
100 | def XmlNode(Dom, String):\r | |
4231a819 | 101 | if String is None or String == "" or Dom is None or Dom == "":\r |
30fdf114 LG |
102 | return ""\r |
103 | if Dom.nodeType == Dom.DOCUMENT_NODE:\r | |
104 | Dom = Dom.documentElement\r | |
105 | if String[0] == "/":\r | |
106 | String = String[1:]\r | |
107 | TagList = String.split('/')\r | |
108 | Index = 0\r | |
109 | End = len(TagList) - 1\r | |
110 | ChildNodes = [Dom]\r | |
111 | while Index <= End:\r | |
112 | for Node in ChildNodes:\r | |
113 | if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == TagList[Index]:\r | |
114 | if Index < End:\r | |
115 | ChildNodes = Node.childNodes\r | |
116 | else:\r | |
117 | return Node\r | |
118 | break\r | |
119 | Index += 1\r | |
120 | return ""\r | |
121 | \r | |
122 | \r | |
123 | ## Get a single XML element using XPath style syntax.\r | |
124 | #\r | |
125 | # Return a single XML element from the root Dom specified by XPath String.\r | |
126 | # If the input Dom or String is not valid, then an empty string is returned.\r | |
127 | #\r | |
128 | # @param Dom The root XML DOM object.\r | |
129 | # @param Strin A XPath style path.\r | |
130 | #\r | |
131 | # @revel Element An XML element matching XPath style Sting.\r | |
132 | #\r | |
133 | def XmlElement(Dom, String):\r | |
134 | try:\r | |
135 | return XmlNode(Dom, String).firstChild.data.strip()\r | |
136 | except:\r | |
137 | return ""\r | |
138 | \r | |
139 | \r | |
140 | ## Get a single XML element of the current node.\r | |
141 | #\r | |
142 | # Return a single XML element specified by the current root Dom.\r | |
143 | # If the input Dom is not valid, then an empty string is returned.\r | |
144 | #\r | |
145 | # @param Dom The root XML DOM object.\r | |
146 | #\r | |
147 | # @revel Element An XML element in current root Dom.\r | |
148 | #\r | |
149 | def XmlElementData(Dom):\r | |
150 | try:\r | |
151 | return Dom.firstChild.data.strip()\r | |
152 | except:\r | |
153 | return ""\r | |
154 | \r | |
155 | \r | |
156 | ## Get a list of XML elements using XPath style syntax.\r | |
157 | #\r | |
158 | # Return a list of XML elements from the root Dom specified by XPath String.\r | |
159 | # If the input Dom or String is not valid, then an empty list is returned.\r | |
160 | #\r | |
161 | # @param Dom The root XML DOM object.\r | |
162 | # @param String A XPath style path.\r | |
163 | #\r | |
164 | # @revel Elements A list of XML elements matching XPath style Sting.\r | |
165 | #\r | |
166 | def XmlElementList(Dom, String):\r | |
167 | return map(XmlElementData, XmlList(Dom, String))\r | |
168 | \r | |
169 | \r | |
170 | ## Get the XML attribute of the current node.\r | |
171 | #\r | |
172 | # Return a single XML attribute named Attribute from the current root Dom.\r | |
173 | # If the input Dom or Attribute is not valid, then an empty string is returned.\r | |
174 | #\r | |
175 | # @param Dom The root XML DOM object.\r | |
176 | # @param Attribute The name of Attribute.\r | |
177 | #\r | |
178 | # @revel Element A single XML element matching XPath style Sting.\r | |
179 | #\r | |
180 | def XmlAttribute(Dom, Attribute):\r | |
181 | try:\r | |
182 | return Dom.getAttribute(Attribute).strip()\r | |
183 | except:\r | |
184 | return ''\r | |
185 | \r | |
186 | \r | |
187 | ## Get the XML node name of the current node.\r | |
188 | #\r | |
189 | # Return a single XML node name from the current root Dom.\r | |
190 | # If the input Dom is not valid, then an empty string is returned.\r | |
191 | #\r | |
192 | # @param Dom The root XML DOM object.\r | |
193 | #\r | |
194 | # @revel Element A single XML element matching XPath style Sting.\r | |
195 | #\r | |
196 | def XmlNodeName(Dom):\r | |
197 | try:\r | |
198 | return Dom.nodeName.strip()\r | |
199 | except:\r | |
200 | return ''\r | |
201 | \r | |
202 | ## Parse an XML file.\r | |
203 | #\r | |
204 | # Parse the input XML file named FileName and return a XML DOM it stands for.\r | |
205 | # If the input File is not a valid XML file, then an empty string is returned.\r | |
206 | #\r | |
207 | # @param FileName The XML file name.\r | |
208 | #\r | |
209 | # @revel Dom The Dom object achieved from the XML file.\r | |
210 | #\r | |
211 | def XmlParseFile(FileName):\r | |
212 | try:\r | |
213 | XmlFile = open(FileName)\r | |
214 | Dom = xml.dom.minidom.parse(XmlFile)\r | |
215 | XmlFile.close()\r | |
216 | return Dom\r | |
217 | except Exception, X:\r | |
218 | print X\r | |
219 | return ""\r | |
220 | \r | |
221 | # This acts like the main() function for the script, unless it is 'import'ed\r | |
222 | # into another script.\r | |
223 | if __name__ == '__main__':\r | |
224 | # Nothing to do here. Could do some unit tests.\r | |
225 | A = CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])\r | |
226 | B = CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])\r | |
227 | C = CreateXmlList('DDD', 'EEE', [A, B], ['FFF', 'GGG'])\r | |
228 | print C.toprettyxml(indent = " ")\r | |
229 | pass\r |