python提取xml指定内容

举报
建帅小伙儿 发表于 2022/09/25 02:58:30 2022/09/25
【摘要】 1.第一种方法:python操作xml文件 随手找了一个xml文件内容(jenkins相关文件) <?xml version="1.0" encoding="UTF-8"?><!--The MIT LicenseCopyright (c) 2004-2009, Sun Microsystems, Inc....

1.第一种方法:python操作xml文件

随手找了一个xml文件内容(jenkins相关文件)


   
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. The MIT License
  4. Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts, id:digerata, Yahoo! Inc.
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -->
  21. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  22. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  24. version="3.1"
  25. metadata-complete="true">
  26. <display-name>Jenkins v2.336</display-name>
  27. <description>Build management system</description>
  28. <servlet>
  29. <servlet-name>Stapler</servlet-name>
  30. <servlet-class>org.kohsuke.stapler.Stapler</servlet-class>
  31. <init-param>
  32. <param-name>default-encodings</param-name>
  33. <param-value>text/html=UTF-8</param-value>
  34. </init-param>
  35. <init-param>
  36. <param-name>diagnosticThreadName</param-name>
  37. <param-value>false</param-value>
  38. </init-param>
  39. <async-supported>true</async-supported>
  40. </servlet>
  41. <servlet-mapping>
  42. <servlet-name>Stapler</servlet-name>
  43. <url-pattern>/*</url-pattern>
  44. </servlet-mapping>
  45. <filter>
  46. <filter-name>suspicious-request-filter</filter-name>
  47. <filter-class>jenkins.security.SuspiciousRequestFilter</filter-class>
  48. <async-supported>true</async-supported>
  49. </filter>
  50. <filter>
  51. <filter-name>diagnostic-name-filter</filter-name>
  52. <filter-class>org.kohsuke.stapler.DiagnosticThreadNameFilter</filter-class>
  53. <async-supported>true</async-supported>
  54. </filter>
  55. <filter>
  56. <filter-name>encoding-filter</filter-name>
  57. <filter-class>hudson.util.CharacterEncodingFilter</filter-class>
  58. <async-supported>true</async-supported>
  59. </filter>
  60. <filter>
  61. <filter-name>compression-filter</filter-name>
  62. <filter-class>org.kohsuke.stapler.compression.CompressionFilter</filter-class>
  63. <async-supported>true</async-supported>
  64. </filter>
  65. <filter>
  66. <filter-name>authentication-filter</filter-name>
  67. <filter-class>hudson.security.HudsonFilter</filter-class>
  68. <async-supported>true</async-supported>
  69. </filter>
  70. <filter>
  71. <filter-name>csrf-filter</filter-name>
  72. <filter-class>hudson.security.csrf.CrumbFilter</filter-class>
  73. <async-supported>true</async-supported>
  74. </filter>
  75. <filter>
  76. <filter-name>plugins-filter</filter-name>
  77. <filter-class>hudson.util.PluginServletFilter</filter-class>
  78. <async-supported>true</async-supported>
  79. </filter>
  80. <!--
  81. The Headers filter allows us to override headers sent by the container
  82. that may be in conflict with what we want. For example, Tomcat will set
  83. Cache-Control: no-cache for any files behind the security-constraint
  84. below. So if Hudson is on a public server, and you want to only allow
  85. authorized users to access it, you may want to pay attention to this.
  86. See: http://www.nabble.com/No-browser-caching-with-Hudson- -tf4601857.html
  87. <filter>
  88. <filter-name>change-headers-filter</filter-name>
  89. <filter-class>hudson.ResponseHeaderFilter</filter-class>
  90. <!- The value listed here is for 24 hours. Increase or decrease as you see
  91. fit. Value is in seconds. Make sure to keep the public option ->
  92. <init-param>
  93. <param-name>Cache-Control</param-name>
  94. <param-value>max-age=86400, public</param-value>
  95. </init-param>
  96. <!- It turns out that Tomcat just doesn't want to let
  97. go of its cache option. If you override Cache-Control,
  98. it starts to send Pragma: no-cache as a backup.
  99. ->
  100. <init-param>
  101. <param-name>Pragma</param-name>
  102. <param-value>public</param-value>
  103. </init-param>
  104. </filter>
  105. <filter-mapping>
  106. <filter-name>change-headers-filter</filter-name>
  107. <url-pattern>*.css</url-pattern>
  108. </filter-mapping>
  109. <filter-mapping>
  110. <filter-name>change-headers-filter</filter-name>
  111. <url-pattern>*.gif</url-pattern>
  112. </filter-mapping>
  113. <filter-mapping>
  114. <filter-name>change-headers-filter</filter-name>
  115. <url-pattern>*.js</url-pattern>
  116. </filter-mapping>
  117. <filter-mapping>
  118. <filter-name>change-headers-filter</filter-name>
  119. <url-pattern>*.png</url-pattern>
  120. </filter-mapping>
  121. -->
  122. <filter-mapping>
  123. <filter-name>suspicious-request-filter</filter-name>
  124. <url-pattern>/*</url-pattern>
  125. </filter-mapping>
  126. <filter-mapping>
  127. <filter-name>diagnostic-name-filter</filter-name>
  128. <url-pattern>/*</url-pattern>
  129. </filter-mapping>
  130. <filter-mapping>
  131. <filter-name>encoding-filter</filter-name>
  132. <url-pattern>/*</url-pattern>
  133. </filter-mapping>
  134. <filter-mapping>
  135. <filter-name>compression-filter</filter-name>
  136. <url-pattern>/*</url-pattern>
  137. </filter-mapping>
  138. <filter-mapping>
  139. <filter-name>authentication-filter</filter-name>
  140. <url-pattern>/*</url-pattern>
  141. </filter-mapping>
  142. <filter-mapping>
  143. <filter-name>csrf-filter</filter-name>
  144. <url-pattern>/*</url-pattern>
  145. </filter-mapping>
  146. <filter-mapping>
  147. <filter-name>plugins-filter</filter-name>
  148. <url-pattern>/*</url-pattern>
  149. </filter-mapping>
  150. <listener>
  151. <!-- Must be before WebAppMain in order to initialize the context before the first use of this class. -->
  152. <listener-class>jenkins.util.SystemProperties$Listener</listener-class>
  153. </listener>
  154. <listener>
  155. <listener-class>hudson.WebAppMain</listener-class>
  156. </listener>
  157. <listener>
  158. <listener-class>jenkins.JenkinsHttpSessionListener</listener-class>
  159. </listener>
  160. <!--
  161. JENKINS-1235 suggests containers interpret '*' as "all roles defined in web.xml"
  162. as opposed to "all roles defined in the security realm", so we need to list some
  163. common names in the hope that users will have at least one of those roles.
  164. -->
  165. <security-role>
  166. <role-name>admin</role-name>
  167. </security-role>
  168. <security-role>
  169. <role-name>user</role-name>
  170. </security-role>
  171. <security-role>
  172. <role-name>hudson</role-name>
  173. </security-role>
  174. <security-constraint>
  175. <web-resource-collection>
  176. <web-resource-name>Hudson</web-resource-name>
  177. <url-pattern>/loginEntry</url-pattern>
  178. <!--http-method>GET</http-method-->
  179. </web-resource-collection>
  180. <auth-constraint>
  181. <role-name>**</role-name>
  182. </auth-constraint>
  183. </security-constraint>
  184. <!-- Disable TRACE method with security constraint (copied from jetty/webdefaults.xml) -->
  185. <security-constraint>
  186. <web-resource-collection>
  187. <web-resource-name>Disable TRACE</web-resource-name>
  188. <url-pattern>/*</url-pattern>
  189. <http-method>TRACE</http-method>
  190. </web-resource-collection>
  191. <auth-constraint />
  192. </security-constraint>
  193. <security-constraint>
  194. <web-resource-collection>
  195. <web-resource-name>other</web-resource-name>
  196. <url-pattern>/*</url-pattern>
  197. </web-resource-collection>
  198. <!-- no security constraint -->
  199. </security-constraint>
  200. <login-config>
  201. <auth-method>FORM</auth-method>
  202. <form-login-config>
  203. <form-login-page>/login</form-login-page>
  204. <form-error-page>/loginError</form-error-page>
  205. </form-login-config>
  206. </login-config>
  207. <!-- if specified, this value is used as the Hudson home directory -->
  208. <env-entry>
  209. <env-entry-name>HUDSON_HOME</env-entry-name>
  210. <env-entry-type>java.lang.String</env-entry-type>
  211. <env-entry-value></env-entry-value>
  212. </env-entry>
  213. <!-- configure additional extension-content-type mappings -->
  214. <mime-mapping>
  215. <extension>xml</extension>
  216. <mime-type>application/xml</mime-type>
  217. </mime-mapping>
  218. <!--mime-mapping> commenting out until this works out of the box with JOnAS. See http://www.nabble.com/Error-with-mime-type%2D-%27application-xslt%2Bxml%27-when-deploying-hudson-1.316-in-jonas-td24740489.html
  219. <extension>xsl</extension>
  220. <mime-type>application/xslt+xml</mime-type>
  221. </mime-mapping-->
  222. <mime-mapping>
  223. <extension>log</extension>
  224. <mime-type>text/plain</mime-type>
  225. </mime-mapping>
  226. <mime-mapping>
  227. <extension>war</extension>
  228. <mime-type>application/octet-stream</mime-type>
  229. </mime-mapping>
  230. <mime-mapping>
  231. <extension>ear</extension>
  232. <mime-type>application/octet-stream</mime-type>
  233. </mime-mapping>
  234. <mime-mapping>
  235. <extension>rar</extension>
  236. <mime-type>application/octet-stream</mime-type>
  237. </mime-mapping>
  238. <mime-mapping>
  239. <extension>webm</extension>
  240. <mime-type>video/webm</mime-type>
  241. </mime-mapping>
  242. <error-page>
  243. <exception-type>java.lang.Throwable</exception-type>
  244. <location>/oops</location>
  245. </error-page>
  246. <session-config>
  247. <cookie-config>
  248. <!-- See https://www.owasp.org/index.php/HttpOnly for the discussion of this topic in OWASP -->
  249. <http-only>true</http-only>
  250. </cookie-config>
  251. <!-- Tracking mode is managed by WebAppMain.FORCE_SESSION_TRACKING_BY_COOKIE_PROP -->
  252. </session-config>
  253. </web-app>

提取某个单个字段:


   
  1. # coding=utf-8
  2. """
  3. 作者:gaojs
  4. 功能:
  5. 新增功能:
  6. 日期:2022/6/2 17:12
  7. """
  8. import xml.dom.minidom
  9. dom = xml.dom.minidom.parse('web.xml')
  10. root = dom.documentElement
  11. bond_list = root.getElementsByTagName('filter-name')
  12. print(bond_list[0].firstChild.data)

运行结果:

image-1654161545688

批量提取某个标签值,并将其写入文本:


   
  1. # coding=utf-8
  2. """
  3. 作者:gaojs
  4. 功能:
  5. 新增功能:
  6. 日期:2022/6/2 17:12
  7. """
  8. import xml.dom.minidom
  9. dom = xml.dom.minidom.parse('web.xml')
  10. root = dom.documentElement
  11. filter_list = root.getElementsByTagName('filter-name')
  12. # print(filter_list[0].firstChild.data)
  13. for bond in filter_list:
  14. s = bond.firstChild.data
  15. print(s)
  16. with open('filter_result.txt', 'a') as fin:
  17. fin.write(s + '\n')

文件结果:

image-1654161710019

2.第二种:正则提取xml指定内容方法


   
  1. with open('web.xml', mode='r') as fin:
  2. test = fin.read()
  3. result = re.findall('<filter-name>(.*?)</filter-name>', test)
  4. for key in result:
  5. print(key)
  6. with open('array/filter_result.txt', 'a') as f:
  7. f.write(key + '\n')

结果:

image-1655457019912

文章来源: blog.csdn.net,作者:懿曲折扇情,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_41332844/article/details/126837421

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。