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