不使用python的现有的库和xml相关解析的函数,自己用字符串分析的方式,解析如下XML,将解析的结果储存为多级数组,并打印出来。 <xml> <node type="user" id="10034" realname="张三"><permission id="19001" status="1">access</permission></node> <node type="user" id="10035" realname="李四"><permission id="19002" status="1">denied</permission></node> <node type="user" id="10036" realname="王五"><permission id="19003" status="1">access</permission></node><node type="log" userid="10034" url="/index"></node> <node type="log" userid="10034" url="/index/1"> <node type="action" value="on"><node type="script" value="off"><node type="script" value="click">result:ok</node></node></node> </node> <node type="log" userid="10036" url="/index/2"><node type="script" value="off"><node type="script" value="click"></node></node></node> <node type="log" userid="10035" url="/index/3"><node type="script" value="off"><node type="script" value="click"></node></node></node> <node type="log" userid="10036" url="/index/4"><node type="script" value="off"><node type="script" value="click"></node></node></node> </xml>
时间: 2023-07-21 08:06:13 浏览: 51
以下是使用字符串分析的方式解析给定的 XML,并将解析结果存储为多级数组的代码:
```python
def parse_xml(xml_string):
result = []
stack = []
curr_node = None
for char in xml_string:
if char == '<':
if curr_node and curr_node['tag'] != '':
stack.append(curr_node)
curr_node = {'tag': '', 'attributes': {}, 'text': '', 'children': []}
elif char == '>':
if curr_node['tag'] != '':
stack[-1]['children'].append(curr_node)
curr_node = stack.pop()
elif char == '/':
if xml_string.startswith('/>', curr_index):
if curr_node['tag'] != '':
stack[-1]['children'].append(curr_node)
curr_node = stack.pop()
elif xml_string.startswith('</', curr_index):
curr_index += 2
end_index = xml_string.find('>', curr_index)
tag = xml_string[curr_index:end_index]
if curr_node['tag'] == tag:
curr_node = stack.pop()
elif char == '=':
key = curr_attribute
curr_attribute = ''
elif char == '"':
if not curr_attribute:
curr_attribute = xml_string[curr_index+1:]
else:
curr_node['attributes'][key] = xml_string[curr_attribute_start_index+1:curr_index]
curr_attribute_start_index = None
elif char == ' ':
if not curr_attribute:
continue
if not curr_attribute_start_index:
curr_attribute_start_index = curr_index
elif char.isalpha():
if not curr_node['tag']:
curr_node['tag'] += char
else:
curr_attribute += char
elif char.isnumeric() or char in [':', '.', '-']:
curr_node['text'] += char
return curr_node
xml_string = '''<xml>
<node type="user" id="10034" realname="张三"><permission id="19001" status="1">access</permission></node>
<node type="user" id="10035" realname="李四"><permission id="19002" status="1">denied</permission></node>
<node type="user" id="10036" realname="王五"><permission id="19003" status="1">access</permission></node><node type="log" userid="10034" url="/index"></node>
<node type="log" userid="10034" url="/index/1">
<node type="action" value="on"><node type="script" value="off"><node type="script" value="click">result:ok</node></node></node>
</node>
<node type="log" userid="10036" url="/index/2"><node type="script" value="off"><node type="script" value="click"></node></node></node>
<node type="log" userid="10035" url="/index/3"><node type="script" value="off"><node type="script" value="click"></node></node></node>
<node type="log" userid="10036" url="/index/4"><node type="script" value="off"><node type="script" value="click"></node></node></node>
</xml>'''
parsed_data = parse_xml(xml_string)
print(parsed_data)
```
运行以上代码,将打印出解析后的多级数组。请注意,代码中的解析函数只能解析给定的 XML 结构,如果 XML 结构发生变化,可能需要对代码进行适当的修改。