简体   繁体   中英

RabbitMQ XML config file parsing in python

I need to parse XML file in my python code. The xml file is as below

<?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? Thanks

My first guess would be to use 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"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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