经过查阅《Python数据处理》这本书觉得分析XML如此的便捷,但是仅仅限制于简单的标签,例如
<?xml version="1.0" encoding="utf-8"?> <catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>测试</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item> </catalog>
<catalog>为简单的标记,对于Tomcat server.xml的标签或许是束手无策了,
在网上也查阅了各路大神分析xml文件,大多数是类似于以上的格式
言归正传,我们先来看一下server.xml的格式
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> -->
.....此处省略
看到第二个标签感觉并不友好
1、首先使用到的是xml标准库
import xml.etree.ElementTree as ET
2、建立一个基础树状对象
tree = ET.ElementTree(file='server.xml')
3、print一下tree 发现tree只是一个对象,对象里面就包含属性
print(tree)
<xml.etree.ElementTree.ElementTree object at 0x0000000858C3F860>
tree.getroot() 返回Server打头的属性值
用root 去接收属性值root = tree.getroot()
4、在xml 文件的特性:标签(tag),attrib(属性)
顺藤摸瓜:
root.tag,root.attrib 执行发现了很惊讶的一幕
('Server', {'port': '8005', 'shutdown': 'SHUTDOWN'}, 'shutdown')
这就是我要的效果,但...只是单行标签,并不是我最终要的结果
经过结果分析得出,或许是迭代器在运行(小括号的结果大致认为是迭代器)
步步为营:
输入root. 按下tab键发现有个属性是getiterator,正是我要的迭代属性
完璧归赵:
dict_i ={}
for i in root.getiterator():dict_i[i.tag]=i.attrib
将结果存入字典内:
{'Server': {'port': '8005', 'shutdown': 'SHUTDOWN'}, 'Listener': {'className': 'org.apache.catalina.core.ThreadLocalLeakPreventionListener'}, 'GlobalNamingResources': {}, 'Resource': {'name': 'UserDatabase', 'auth': 'Container', 'type': 'org.apache.catalina.UserDatabase', 'description': 'User database that can be updated and saved', 'factory': 'org.apache.catalina.users.MemoryUserDatabaseFactory', 'pathname': 'conf/tomcat-users.xml'}, 'Service': {'name': 'Catalina'}, 'Connector': {'port': '8009', 'protocol': 'AJP/1.3', 'redirectPort': '8443'}, 'Engine': {'name': 'Catalina', 'defaultHost': 'localhost'}, 'Realm': {'className': 'org.apache.catalina.realm.UserDatabaseRealm', 'resourceName': 'UserDatabase'}, 'Host': {'name': 'localhost', 'appBase': 'webapps', 'unpackWARs': 'true', 'autoDeploy': 'true'}, 'Valve': {'className': 'org.apache.catalina.valves.AccessLogValve', 'directory': 'logs', 'prefix': 'localhost_access_log', 'suffix': '.txt', 'pattern': '%h %l %u %t "%r" %s %b'}}
个人知识分享,请勿克隆