XML Parser

来源:互联网 发布:国家电网题库软件免费 编辑:程序博客网 时间:2024/05/21 17:38
class TreeNode(object):
def __init__(self, content):
self.content = content
self.children = []


def add_child(self, node):
self.children.append(node)


def XMLElementParser(XMLString, begin):
begin_pos = 0
for i in range(begin, len(XMLString)):
if XMLString[i] == '<':
begin_pos = i
break
end_pos = begin_pos
for i in range(begin_pos + 1, len(XMLString)):
if XMLString[i] == '>':
end_pos = i
break
if XMLString[begin_pos+1] == '?':
if XMLString[end_pos - 1] != '?':
raise Exception('invalid comment')
else:
return end_pos, '', 0
elif XMLString[end_pos - 1] == '/':
content = XMLString[begin_pos + 1: end_pos - 1]
content = content.strip()
element = content.split(' ')[0]
return end_pos, element, 1
elif XMLString[begin_pos + 1] == '/':
content = XMLString[begin_pos + 2: end_pos]
content = content.strip()
element = content.split(' ')[0]
return end_pos, element, 2
else:
content = XMLString[begin_pos + 1: end_pos]
content = content.strip()
element = content.split(' ')[0]
return end_pos, element, 3


def XMLParser(XMLString):
status = 0
end_pos = 0
element = ''
while status == 0:
end_pos, element, status = XMLElementParser(XMLString, end_pos)
end_pos = end_pos + 1
parse_stack = []
root = TreeNode(element)
parse_stack.append(root)
while len(parse_stack) > 0:
end_pos, element, status = XMLElementParser(XMLString, end_pos)
end_pos = end_pos + 1
if status == 0:
continue
elif status == 1:
node = TreeNode(element)
parse_stack[-1].add_child(node)
elif status == 2:
if element != parse_stack[-1].content:
raise Exception('tag mismatch')
else:
parse_stack = parse_stack[:-1]
elif status == 3:
node = TreeNode(element)
parse_stack[-1].add_child(node)
parse_stack.append(node)
return root
1 0
原创粉丝点击