简体   繁体   English

Python中的RabbitMQ XML配置文件解析

[英]RabbitMQ XML config file parsing in python

I need to parse XML file in my python code. 我需要在python代码中解析XML文件。 The xml file is as below 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>

I need all rabbit tag elements and its values, like , rabbit: queue, rabbit:topic-exchange, etc. Can you please suggest me an effective method to get this? 我需要所有Rabbit标签元素及其值,例如,Rabbit:队列,Rabbit:topic-exchange等。您能否建议我一个有效的方法来实现这一点? Thanks 谢谢

My first guess would be to use xml.ElementTree . 我的第一个猜测是使用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))

Output: 输出:

{
    "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"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM