blob: 8d659c4372bfb9d7ac540af9fa4b18bedac9d0ee (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#!/usr/bin/env python
# Copyright (c) 2007, Intel Corporation
# All rights reserved. This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
"""This is an XML API that uses a syntax similar to XPath, but it is written in
standard python so that no extra python packages are required to use it."""
import xml.dom.minidom
def XmlList(Dom, String):
"""Get a list of XML Elements using XPath style syntax."""
if String == "" or Dom == None or not isinstance(Dom, xml.dom.Node):
return []
if String[0] == "/":
String = String[1:]
if Dom.nodeType==Dom.DOCUMENT_NODE:
Dom = Dom.documentElement
tagList = String.split('/')
nodes = [Dom]
childNodes = []
index = 0
end = len(tagList) - 1
while index <= end:
for node in nodes:
if node.nodeType == node.ELEMENT_NODE and node.tagName == tagList[index]:
if index < end:
childNodes.extend(node.childNodes)
else:
childNodes.append(node)
nodes = childNodes
childNodes = []
index += 1
return nodes
def XmlElement (Dom, String):
"""Return a single element that matches the String which is XPath style syntax."""
if String == "" or Dom == None or not isinstance(Dom, xml.dom.Node):
return ""
if String[0] == "/":
String = String[1:]
if Dom.nodeType==Dom.DOCUMENT_NODE:
Dom = Dom.documentElement
tagList = String.split('/')
childNodes = [Dom]
index = 0
end = len(tagList) - 1
while index <= end:
for node in childNodes:
if node.nodeType == node.ELEMENT_NODE and node.tagName == tagList[index]:
if index < end:
childNodes = node.childNodes
else:
return node
break
index += 1
return ""
def XmlElementData (Dom):
"""Get the text for this element."""
if Dom == None or Dom == '' or Dom.firstChild == None:
return ''
return Dom.firstChild.data.strip(' ')
def XmlAttribute (Dom, String):
"""Return a single attribute that named by String."""
if Dom == None or Dom == '':
return ''
try:
return Dom.getAttribute(String).strip(' ')
except:
return ''
def XmlTopTag(Dom):
"""Return the name of the Root or top tag in the XML tree."""
if Dom == None or Dom == '' or Dom.firstChild == None:
return ''
return Dom.firstChild.nodeName
# This acts like the main() function for the script, unless it is 'import'ed into another
# script.
if __name__ == '__main__':
# Nothing to do here. Could do some unit tests.
pass
|