我需要在我的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>
我需要所有兔子标签元素及其值,例如,兔子:队列,兔子:主题交换等。你能建议我一个有效的方法来获得这个吗?感谢
答案 0 :(得分:0)
我的第一个猜测是使用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"
}
}