gpt4 book ai didi

python - 如何使用python删除xml中的特定标签

转载 作者:行者123 更新时间:2023-12-01 04:31:29 25 4
gpt4 key购买 nike

我必须删除 apache-tomcat web.xml 文件中的一些特定标记

web.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>



<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<!-- ======================== Introduction ============================== -->
<!-- This document defines default values for *all* web applications -->
<!-- loaded into this instance of Tomcat. As each application is -->
<!-- deployed, this file is processed, followed by the -->
<!-- "/WEB-INF/web.xml" deployment descriptor from your own -->
<!-- applications. -->
<!-- -->
<!-- WARNING: Do not configure application-specific resources here! -->
<!-- They should go in the "/WEB-INF/web.xml" file in your application. -->

<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>

<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
</<web-app>

如果 servlet-name== cgi 我需要删除整个 servlet 标记。 我的代码如下:

    from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse('web.xml')
servlets = tree.findall('servlet')
print "servlets : ",servlets
for servlet in servlets:
servlet_names = foo.findall('servlet-name')
for servlet_name in servlet_names:
if servlet_name == "cgi" :
print "servlet_name :", servlet_name
servlet.remove(servlet-name)

我将 o/p 作为 servlet 获取:[] 而不是所有servlet并且无法进入for循环。 任何人都可以帮助我吗?

我没有遇到任何异常

#!/usr/bin/python
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse('web.xml')
root = tree.getroot()
ns = '{http://java.sun.com/xml/ns/javaee}'
servlets = root.findall(ns + 'servlet')
print "servlets : ",servlets
for servlet in servlets:
servlet_names = servlet.findall(ns + 'servlet-name')
for servlet_name in servlet_names:
if servlet_name.text == "cgi" :
print "servlet_name :", servlet_name.text
print "removed the cgi serverlet", root.remove(servlet)

====输出================servlet :[http://java.sun.com/xml/ns/javaee}servlet 位于 7f84e09b35a8>、http://java.sun.com/xml/ns/javaee}servlet 位于 7f84e09b3878>、http://java .sun.com/xml/ns/javaee}servlet 位于 7f84e09b3bd8>]servlet_name:cgi删除了 cgi serverlet 无

==== 我已经使用 pdb 跟踪器来找出元素(servlet)值,其显示为\n..

> /apps/manu/python/manunamespace.py(10)<module>()
-> servlet_name=servlet.find('{http://java.sun.com/xml/ns/javaee}servlet-name')
(Pdb) servlet_name
<Element {http://java.sun.com/xml/ns/javaee}servlet-name at 882878>
(Pdb) servlet_name.text
'jsp'
(Pdb) n
> /apps/manu/python/manunamespace.py(11)<module>()
-> print "servlet_name:", servlet_name.text
(Pdb) servlet_name.text
'cgi'
(Pdb) servlet.text
'\n '
(Pdb) n
servlet_name: cgi
> /apps/manu/python/manunamespace.py(12)<module>()
-> if servlet_name.text == "cgi":
(Pdb) n
> /apps/manu/python/manunamespace.py(13)<module>()
-> print "remove the element"
(Pdb) n
remove the element
> /apps/manu/python/manunamespace.py(14)<module>()
-> print "remove : ",root.remove(servlet)
(Pdb) servlet
<Element {http://java.sun.com/xml/ns/javaee}servlet at 882d88>
(Pdb) servlet.text
'\n

'

最佳答案

这是失败的:

servlets = tree.findall('servlet')

因为您的文档中没有 servlet 元素。根元素指定:

xmlns="http://java.sun.com/xml/ns/javaee"

这意味着,除非另有指定,否则所有元素都位于此 XML 命名空间中。所以你想要:

>>> tree.findall('{http://java.sun.com/xml/ns/javaee}servlet')
[<Element {http://java.sun.com/xml/ns/javaee}servlet at 0x7f280ec681b8>,
<Element {http://java.sun.com/xml/ns/javaee}servlet at 0x7f280ec68200>,
<Element {http://java.sun.com/xml/ns/javaee}servlet at 0x7f280ec682d8>]
>>>

关于python - 如何使用python删除xml中的特定标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32354285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com