代码之家  ›  专栏  ›  技术社区  ›  megna

python中的RabbitMQ XML配置文件解析

  •  0
  • megna  · 技术社区  · 7 年前

    我需要在python代码中解析XML文件。xml文件如下所示

    <?xml version="1.0" encoding="utf-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:rabbit="http://www.springframework.org/schema/rabbit"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/rabbit
      http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
    
    <rabbit:connection-factory id="connectionFactory" host="localhost" 
              username="guest" password="guest"/>
    
    <rabbit:admin connection-factory="connectionFactory"/>
    <rabbit:queue id ="tpQueue"/>
    
    <rabbit:topic-exchange id="tpExchange" name="tpExchange">
        <rabbit:bindings>
            <rabbit:binding queue="tpQueue" pattern="tp.routingkey.1">
            </rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>
    
    <bean id="asyncListener" class="com.tp.spring_amqp_rabbitmq.SpringAMQPRabbitAyncListener"/>
    <rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory">
        <rabbit:listener ref="asyncListener" queue-names="tpQueue"/>
    </rabbit:listener-container>
    
    </beans>
    

    我需要所有rabbit标记元素及其值,比如,rabbit:queue,rabbit:topic exchange,等等。你能给我建议一种有效的方法吗?谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   G_M    7 年前

    我的第一个猜测是 xml.ElementTree .

    import json
    from xml.etree import ElementTree
    
    text = '''\
    <?xml version="1.0" encoding="utf-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:rabbit="http://www.springframework.org/schema/rabbit"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/rabbit
      http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
    
    <rabbit:connection-factory id="connectionFactory" host="localhost"
              username="guest" password="guest"/>
    
    <rabbit:admin connection-factory="connectionFactory"/>
    <rabbit:queue id ="tpQueue"/>
    
    <rabbit:topic-exchange id="tpExchange" name="tpExchange">
        <rabbit:bindings>
            <rabbit:binding queue="tpQueue" pattern="tp.routingkey.1">
            </rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>
    
    <bean id="asyncListener" class="com.tp.spring_amqp_rabbitmq.SpringAMQPRabbitAyncListener"/>
    <rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory">
        <rabbit:listener ref="asyncListener" queue-names="tpQueue"/>
    </rabbit:listener-container>
    
    </beans>
    '''
    
    result = {}
    root = ElementTree.fromstring(text)
    
    for elem in root.iter():
        if 'rabbit' in elem.tag:
            key = elem.tag[elem.tag.rfind('}') + 1:]
            result[key] = elem.attrib
    
    print(json.dumps(result, indent=4))
    

    输出:

    {
        "connection-factory": {
            "id": "connectionFactory",
            "host": "localhost",
            "username": "guest",
            "password": "guest"
        },
        "admin": {
            "connection-factory": "connectionFactory"
        },
        "queue": {
            "id": "tpQueue"
        },
        "topic-exchange": {
            "id": "tpExchange",
            "name": "tpExchange"
        },
        "bindings": {},
        "binding": {
            "queue": "tpQueue",
            "pattern": "tp.routingkey.1"
        },
        "listener-container": {
            "id": "myListenerContainer",
            "connection-factory": "connectionFactory"
        },
        "listener": {
            "ref": "asyncListener",
            "queue-names": "tpQueue"
        }
    }