【问题标题】:Using single quotes more than once in xml.etree.ElementTree在 xml.etree.ElementTree 中多次使用单引号
【发布时间】:2021-10-07 19:10:23
【问题描述】:

我需要编辑一个名为activemq.xml 的xml 文件。我将包含 xml 部分、我的 PYTHON 代码 sn-p 以及我需要进行的更改。

XML 文件

<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<!-- START SNIPPET: example -->
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

    <!-- Allows us to use system properties as variables in this configuration file -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:${activemq.conf}/credentials.properties</value>
        </property>
    </bean>

   <!-- Allows accessing the server log -->
    <bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"
          lazy-init="false" scope="singleton"
          init-method="start" destroy-method="stop">
    </bean>

    <!--
        The <broker> element is used to configure the ActiveMQ broker.
    -->
    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">

        <destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" >
                    <!-- The constantPendingMessageLimitStrategy is used to prevent
                         slow topic consumers to block producers and affect other consumers
                         by limiting the number of messages that are retained
                         For more information, see:

                         http://activemq.apache.org/slow-consumer-handling.html

                    -->
                  <pendingMessageLimitStrategy>
                    <constantPendingMessageLimitStrategy limit="1000"/>
                  </pendingMessageLimitStrategy>
                </policyEntry>
              </policyEntries>
            </policyMap>
        </destinationPolicy>


        <!--
            The managementContext is used to configure how ActiveMQ is exposed in
            JMX. By default, ActiveMQ uses the MBean server that is started by
            the JVM. For more information, see:

            http://activemq.apache.org/jmx.html
        -->
        <managementContext>
            <managementContext createConnector="false"/>
        </managementContext>

        <!--
            Configure message persistence for the broker. The default persistence
            mechanism is the KahaDB store (identified by the kahaDB tag).
            For more information, see:

            http://activemq.apache.org/persistence.html
        -->
        <persistenceAdapter>
            <kahaDB directory="${activemq.data}/kahadb"/>
        </persistenceAdapter>


          <!--
            The systemUsage controls the maximum amount of space the broker will
            use before disabling caching and/or slowing down producers. For more information, see:
            http://activemq.apache.org/producer-flow-control.html
          -->
          <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage percentOfJvmHeap="70" />
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="100 gb"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="50 gb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>

        <!--
            The transport connectors expose ActiveMQ over a given protocol to
            clients and other brokers. For more information, see:

            http://activemq.apache.org/configuring-transports.html
        -->
        <transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

        <!-- destroy the spring context on shutdown to stop jetty -->
        <shutdownHooks>
            <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
        </shutdownHooks>

    </broker>

    <!--
        Enable web consoles, REST and Ajax APIs and demos
        The web consoles requires by default login, you can disable this in the jetty.xml file

        Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
    -->
    <import resource="jetty.xml"/>

</beans>

python 代码 sn-p ,它将作为 shell 脚本中的任务(linux)

#!/usr/bin/env python3

import xml.etree.ElementTree as ET

ET.register_namespace('','http://www.springframework.org/schema/beans')
ET.register_namespace('','http://activemq.apache.org/schema/core')

mytree = ET.parse('activemq.xml')
myroot = mytree.getroot()
elemList = list(myroot[2])
up = elemList[2]
up[0].attrib['directory'] = '/nfs'
mytree.write("activemqbak.xml")

我需要做的改变是

<persistenceAdapter>
    <kahaDB directory="${activemq.data}/kahadb"/>
</persistenceAdapter>

$activemqdata/nfs(我的代码已经这样做了。)

然而,我的问题是在修改我的 xml 文件后,我无法按原样保留名称空间。除了修改键值和添加更多代码之外,我无法对 xml 进行任何更改,但不能对原始标签或元素本身进行任何更改。

tree.write 命令的输出示例

<ns0:beans xmlns="http://activemq.apache.org/schema/core" xmlns:ns0="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

    
    <ns0:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <ns0:property name="locations">
            <ns0:value>file:${activemq.conf}/credentials.properties</ns0:value>
        </ns0:property>
    </ns0:bean>

   
    <ns0:bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery" lazy-init="false" scope="singleton" init-method="start" destroy-method="stop">
    </ns0:bean>

    
        <broker brokerName="localhost" dataDirectory="${activemq.data}">

抱歉,帖子太长了,不胜感激!

顺便说一句,使用最新的 Python 版本。不需要向后兼容。

【问题讨论】:

    标签: python python-3.x xml spring


    【解决方案1】:

    只需使用“f 字符串”。将文件加载到字符串,替换需要的内容(见下文)并再次保存。请注意,我将activemq.conf 更改为activemq_conf

    activemq_conf = '/nfs'
    data = f'''<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value>file:${activemq_conf}/credentials.properties</value>
            </property>
        </bean>'''
    

    【讨论】:

    • 嘿 balderman,感谢您尝试回答我的问题,看来这确实是尝试解决我的问题的一种方式,如果我有任何问题,我会向您报告。
    • @M4CHINE 太好了。告诉我。
    • 嘿 balderman,我最终切换到 lxml 来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2012-08-01
    • 2011-03-23
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多