XML
Python3

Tomcat で使われているXML形式の設定ファイルをPythonで判りやすくしたい。

More than 1 year has passed since last update.

Tomcat の XML形式の設定ファイルの内容が判りにくいので判りやすくしたい。

xml_tomcat.py
import xml.etree.ElementTree as ET

def xml_dict(parent_name, child_name, child_dict):
    for attr in child_dict:
        print(parent_name, end="/")
        print(child_name, end="\t")
        print(attr, end="\t")
        print(child_dict[attr])

def disassembly(parent):
    for child in list(parent):
        if isinstance(child.attrib, dict):
            xml_dict(parent.tag, child.tag, child.attrib)
        disassembly(child)

filedir = "./"
filename = "server.xml"

file = filedir + filename

tree = ET.parse(file)
root = tree.getroot()

disassembly(root)

結果は、下記のようになりました。

Server/Listener className org.apache.catalina.startup.VersionLoggerListener
Server/Listener className org.apache.catalina.core.AprLifecycleListener
Server/Listener SSLEngine on
Server/Listener className org.apache.catalina.core.JreMemoryLeakPreventionListener
Server/Listener className org.apache.catalina.mbeans.GlobalResourcesLifecycleListener
Server/Listener className org.apache.catalina.core.ThreadLocalLeakPreventionListener
GlobalNamingResources/Resource name UserDatabase
GlobalNamingResources/Resource auth Container
GlobalNamingResources/Resource type org.apache.catalina.UserDatabase
GlobalNamingResources/Resource description User database that can be updated and saved
GlobalNamingResources/Resource factory org.apache.catalina.users.MemoryUserDatabaseFactory
GlobalNamingResources/Resource pathname conf/tomcat-users.xml
Server/Service name Catalina
Service/Connector port 8080
Service/Connector protocol HTTP/1.1
Service/Connector connectionTimeout 20000
Service/Connector redirectPort 8443
Service/Connector port 8009
Service/Connector protocol AJP/1.3
Service/Connector redirectPort 8443
Service/Engine name Catalina
Service/Engine defaultHost localhost
Engine/Realm className org.apache.catalina.realm.LockOutRealm
Realm/Realm className org.apache.catalina.realm.UserDatabaseRealm
Realm/Realm resourceName UserDatabase
Engine/Host name localhost
Engine/Host appBase webapps
Engine/Host unpackWARs true
Engine/Host autoDeploy true
Host/Valve className org.apache.catalina.valves.AccessLogValve
Host/Valve directory logs
Host/Valve prefix localhost_access_log
Host/Valve suffix .txt
Host/Valve pattern %h %l %u %t "%r" %s %b