]>
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 | |
72443dd2 | 18 | from __future__ import print_function\r |
30fdf114 | 19 | import xml.dom.minidom\r |
1be2ed90 | 20 | from Common.LongFilePathSupport import OpenLongFilePath as open\r |
30fdf114 LG |
21 | \r |
22 | ## Create a element of XML\r | |
23 | #\r | |
24 | # @param Name\r | |
25 | # @param String\r | |
26 | # @param NodeList\r | |
27 | # @param AttributeList\r | |
28 | #\r | |
29 | # @revel Element\r | |
30 | #\r | |
31 | def CreateXmlElement(Name, String, NodeList, AttributeList):\r | |
32 | Doc = xml.dom.minidom.Document()\r | |
33 | Element = Doc.createElement(Name)\r | |
4231a819 | 34 | if String != '' and String is not None:\r |
30fdf114 LG |
35 | Element.appendChild(Doc.createTextNode(String))\r |
36 | \r | |
37 | for Item in NodeList:\r | |
0d1f5b2b | 38 | if isinstance(Item, type([])):\r |
30fdf114 LG |
39 | Key = Item[0]\r |
40 | Value = Item[1]\r | |
4231a819 | 41 | if Key != '' and Key is not None and Value != '' and Value is not None:\r |
30fdf114 LG |
42 | Node = Doc.createElement(Key)\r |
43 | Node.appendChild(Doc.createTextNode(Value))\r | |
44 | Element.appendChild(Node)\r | |
45 | else:\r | |
46 | Element.appendChild(Item)\r | |
47 | for Item in AttributeList:\r | |
48 | Key = Item[0]\r | |
49 | Value = Item[1]\r | |
4231a819 | 50 | if Key != '' and Key is not None and Value != '' and Value is not None:\r |
30fdf114 LG |
51 | Element.setAttribute(Key, Value)\r |
52 | \r | |
53 | return Element\r | |
54 | \r | |
55 | ## Get a list of XML nodes using XPath style syntax.\r | |
56 | #\r | |
57 | # Return a list of XML DOM nodes from the root Dom specified by XPath String.\r | |
58 | # If the input Dom or String is not valid, then an empty list is returned.\r | |
59 | #\r | |
60 | # @param Dom The root XML DOM node.\r | |
61 | # @param String A XPath style path.\r | |
62 | #\r | |
63 | # @revel Nodes A list of XML nodes matching XPath style Sting.\r | |
64 | #\r | |
65 | def XmlList(Dom, String):\r | |
4231a819 | 66 | if String is None or String == "" or Dom is None or Dom == "":\r |
30fdf114 LG |
67 | return []\r |
68 | if Dom.nodeType == Dom.DOCUMENT_NODE:\r | |
69 | Dom = Dom.documentElement\r | |
70 | if String[0] == "/":\r | |
71 | String = String[1:]\r | |
72 | TagList = String.split('/')\r | |
73 | Nodes = [Dom]\r | |
74 | Index = 0\r | |
75 | End = len(TagList) - 1\r | |
76 | while Index <= End:\r | |
77 | ChildNodes = []\r | |
78 | for Node in Nodes:\r | |
79 | if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == TagList[Index]:\r | |
80 | if Index < End:\r | |
81 | ChildNodes.extend(Node.childNodes)\r | |
82 | else:\r | |
83 | ChildNodes.append(Node)\r | |
84 | Nodes = ChildNodes\r | |
85 | ChildNodes = []\r | |
86 | Index += 1\r | |
87 | \r | |
88 | return Nodes\r | |
89 | \r | |
90 | \r | |
91 | ## Get a single XML node using XPath style syntax.\r | |
92 | #\r | |
93 | # Return a single XML DOM node from the root Dom specified by XPath String.\r | |
94 | # If the input Dom or String is not valid, then an empty string is returned.\r | |
95 | #\r | |
96 | # @param Dom The root XML DOM node.\r | |
97 | # @param String A XPath style path.\r | |
98 | #\r | |
99 | # @revel Node A single XML node matching XPath style Sting.\r | |
100 | #\r | |
101 | def XmlNode(Dom, String):\r | |
4231a819 | 102 | if String is None or String == "" or Dom is None or Dom == "":\r |
30fdf114 LG |
103 | return ""\r |
104 | if Dom.nodeType == Dom.DOCUMENT_NODE:\r | |
105 | Dom = Dom.documentElement\r | |
106 | if String[0] == "/":\r | |
107 | String = String[1:]\r | |
108 | TagList = String.split('/')\r | |
109 | Index = 0\r | |
110 | End = len(TagList) - 1\r | |
111 | ChildNodes = [Dom]\r | |
112 | while Index <= End:\r | |
113 | for Node in ChildNodes:\r | |
114 | if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == TagList[Index]:\r | |
115 | if Index < End:\r | |
116 | ChildNodes = Node.childNodes\r | |
117 | else:\r | |
118 | return Node\r | |
119 | break\r | |
120 | Index += 1\r | |
121 | return ""\r | |
122 | \r | |
123 | \r | |
124 | ## Get a single XML element using XPath style syntax.\r | |
125 | #\r | |
126 | # Return a single XML element from the root Dom specified by XPath String.\r | |
127 | # If the input Dom or String is not valid, then an empty string is returned.\r | |
128 | #\r | |
129 | # @param Dom The root XML DOM object.\r | |
130 | # @param Strin A XPath style path.\r | |
131 | #\r | |
132 | # @revel Element An XML element matching XPath style Sting.\r | |
133 | #\r | |
134 | def XmlElement(Dom, String):\r | |
135 | try:\r | |
136 | return XmlNode(Dom, String).firstChild.data.strip()\r | |
137 | except:\r | |
138 | return ""\r | |
139 | \r | |
140 | \r | |
141 | ## Get a single XML element of the current node.\r | |
142 | #\r | |
143 | # Return a single XML element specified by the current root Dom.\r | |
144 | # If the input Dom is not valid, then an empty string is returned.\r | |
145 | #\r | |
146 | # @param Dom The root XML DOM object.\r | |
147 | #\r | |
148 | # @revel Element An XML element in current root Dom.\r | |
149 | #\r | |
150 | def XmlElementData(Dom):\r | |
151 | try:\r | |
152 | return Dom.firstChild.data.strip()\r | |
153 | except:\r | |
154 | return ""\r | |
155 | \r | |
156 | \r | |
157 | ## Get a list of XML elements using XPath style syntax.\r | |
158 | #\r | |
159 | # Return a list of XML elements from the root Dom specified by XPath String.\r | |
160 | # If the input Dom or String is not valid, then an empty list is returned.\r | |
161 | #\r | |
162 | # @param Dom The root XML DOM object.\r | |
163 | # @param String A XPath style path.\r | |
164 | #\r | |
165 | # @revel Elements A list of XML elements matching XPath style Sting.\r | |
166 | #\r | |
167 | def XmlElementList(Dom, String):\r | |
168 | return map(XmlElementData, XmlList(Dom, String))\r | |
169 | \r | |
170 | \r | |
171 | ## Get the XML attribute of the current node.\r | |
172 | #\r | |
173 | # Return a single XML attribute named Attribute from the current root Dom.\r | |
174 | # If the input Dom or Attribute is not valid, then an empty string is returned.\r | |
175 | #\r | |
176 | # @param Dom The root XML DOM object.\r | |
177 | # @param Attribute The name of Attribute.\r | |
178 | #\r | |
179 | # @revel Element A single XML element matching XPath style Sting.\r | |
180 | #\r | |
181 | def XmlAttribute(Dom, Attribute):\r | |
182 | try:\r | |
183 | return Dom.getAttribute(Attribute).strip()\r | |
184 | except:\r | |
185 | return ''\r | |
186 | \r | |
187 | \r | |
188 | ## Get the XML node name of the current node.\r | |
189 | #\r | |
190 | # Return a single XML node name from the current root Dom.\r | |
191 | # If the input Dom is not valid, then an empty string is returned.\r | |
192 | #\r | |
193 | # @param Dom The root XML DOM object.\r | |
194 | #\r | |
195 | # @revel Element A single XML element matching XPath style Sting.\r | |
196 | #\r | |
197 | def XmlNodeName(Dom):\r | |
198 | try:\r | |
199 | return Dom.nodeName.strip()\r | |
200 | except:\r | |
201 | return ''\r | |
202 | \r | |
203 | ## Parse an XML file.\r | |
204 | #\r | |
205 | # Parse the input XML file named FileName and return a XML DOM it stands for.\r | |
206 | # If the input File is not a valid XML file, then an empty string is returned.\r | |
207 | #\r | |
208 | # @param FileName The XML file name.\r | |
209 | #\r | |
210 | # @revel Dom The Dom object achieved from the XML file.\r | |
211 | #\r | |
212 | def XmlParseFile(FileName):\r | |
213 | try:\r | |
214 | XmlFile = open(FileName)\r | |
215 | Dom = xml.dom.minidom.parse(XmlFile)\r | |
216 | XmlFile.close()\r | |
217 | return Dom\r | |
5b0671c1 | 218 | except Exception as X:\r |
72443dd2 | 219 | print(X)\r |
30fdf114 LG |
220 | return ""\r |
221 | \r | |
222 | # This acts like the main() function for the script, unless it is 'import'ed\r | |
223 | # into another script.\r | |
224 | if __name__ == '__main__':\r | |
225 | # Nothing to do here. Could do some unit tests.\r | |
226 | A = CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])\r | |
227 | B = CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])\r | |
228 | C = CreateXmlList('DDD', 'EEE', [A, B], ['FFF', 'GGG'])\r | |
72443dd2 | 229 | print(C.toprettyxml(indent = " "))\r |
30fdf114 | 230 | pass\r |