广告位联系
返回顶部
分享到

python提取xml指定内容的方法

python 来源:互联网 作者:佚名 发布时间:2023-01-04 21:33:56 人浏览
摘要

第一种方法:python操作xml文件 随手找了一个xml文件内容(jenkins相关文件) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

<?xml version="1.0" encoding="UTF-8"?>

<!--

The MIT License

Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts, id:digerata, Yahoo! Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in

all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

THE SOFTWARE.

-->

  

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

         version="3.1"

         metadata-complete="true">

  <display-name>Jenkins v2.336</display-name>

  <description>Build management system</description>

  

  <servlet>

    <servlet-name>Stapler</servlet-name>

    <servlet-class>org.kohsuke.stapler.Stapler</servlet-class>

    <init-param>

      <param-name>default-encodings</param-name>

      <param-value>text/html=UTF-8</param-value>

    </init-param>

    <init-param>

      <param-name>diagnosticThreadName</param-name>

      <param-value>false</param-value>

    </init-param>

    <async-supported>true</async-supported>

  </servlet>

  

  <servlet-mapping>

    <servlet-name>Stapler</servlet-name>

    <url-pattern>/*</url-pattern>

  </servlet-mapping>

  

  <filter>

    <filter-name>suspicious-request-filter</filter-name>

    <filter-class>jenkins.security.SuspiciousRequestFilter</filter-class>

    <async-supported>true</async-supported>

  </filter>

  <filter>

    <filter-name>diagnostic-name-filter</filter-name>

    <filter-class>org.kohsuke.stapler.DiagnosticThreadNameFilter</filter-class>

    <async-supported>true</async-supported>

  </filter>

  <filter>

    <filter-name>encoding-filter</filter-name>

    <filter-class>hudson.util.CharacterEncodingFilter</filter-class>

    <async-supported>true</async-supported>

  </filter>

  <filter>

    <filter-name>compression-filter</filter-name>

    <filter-class>org.kohsuke.stapler.compression.CompressionFilter</filter-class>

    <async-supported>true</async-supported>

  </filter>

  <filter>

    <filter-name>authentication-filter</filter-name>

    <filter-class>hudson.security.HudsonFilter</filter-class>

    <async-supported>true</async-supported>

  </filter>

  <filter>

    <filter-name>csrf-filter</filter-name>

    <filter-class>hudson.security.csrf.CrumbFilter</filter-class>

    <async-supported>true</async-supported>

  </filter>

  <filter>

    <filter-name>plugins-filter</filter-name>

    <filter-class>hudson.util.PluginServletFilter</filter-class>

    <async-supported>true</async-supported>

  </filter>

  

  <!--

    The Headers filter allows us to override headers sent by the container

    that may be in conflict with what we want.  For example, Tomcat will set

    Cache-Control: no-cache for any files behind the security-constraint

    below.  So if Hudson is on a public server, and you want to only allow

    authorized users to access it, you may want to pay attention to this.

     

    See: http://www.nabble.com/No-browser-caching-with-Hudson- -tf4601857.html

   

  <filter>

    <filter-name>change-headers-filter</filter-name>

    <filter-class>hudson.ResponseHeaderFilter</filter-class>

    <!- The value listed here is for 24 hours.  Increase or decrease as you see

    fit.  Value is in seconds. Make sure to keep the public option ->

    <init-param>

      <param-name>Cache-Control</param-name>

      <param-value>max-age=86400, public</param-value>

    </init-param>

    <!- It turns out that Tomcat just doesn't want to let

    go of its cache option.  If you override Cache-Control,

    it starts to send Pragma: no-cache as a backup.

     ->

    <init-param>

      <param-name>Pragma</param-name>

      <param-value>public</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>change-headers-filter</filter-name>

    <url-pattern>*.css</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>change-headers-filter</filter-name>

    <url-pattern>*.gif</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>change-headers-filter</filter-name>

    <url-pattern>*.js</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>change-headers-filter</filter-name>

    <url-pattern>*.png</url-pattern>

  </filter-mapping>

  -->

  

  <filter-mapping>

    <filter-name>suspicious-request-filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>diagnostic-name-filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>encoding-filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>compression-filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>authentication-filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>csrf-filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>plugins-filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  

  <listener>

    <!-- Must be before WebAppMain in order to initialize the context before the first use of this class. -->

    <listener-class>jenkins.util.SystemProperties$Listener</listener-class>

  </listener>

  <listener>

    <listener-class>hudson.WebAppMain</listener-class>

  </listener>

  <listener>

    <listener-class>jenkins.JenkinsHttpSessionListener</listener-class>

  </listener>

  

  <!--

    JENKINS-1235 suggests containers interpret '*' as "all roles defined in web.xml"

    as opposed to "all roles defined in the security realm", so we need to list some

    common names in the hope that users will have at least one of those roles.

  -->

  <security-role>

    <role-name>admin</role-name>

  </security-role>

  <security-role>

    <role-name>user</role-name>

  </security-role>

  <security-role>

    <role-name>hudson</role-name>

  </security-role>

  

  <security-constraint>

    <web-resource-collection>

      <web-resource-name>Hudson</web-resource-name>

      <url-pattern>/loginEntry</url-pattern>

      <!--http-method>GET</http-method-->

    </web-resource-collection>

    <auth-constraint>

      <role-name>**</role-name>

    </auth-constraint>

  </security-constraint>

   

  <!-- Disable TRACE method with security constraint (copied from jetty/webdefaults.xml) -->

  <security-constraint>

    <web-resource-collection>

      <web-resource-name>Disable TRACE</web-resource-name>

      <url-pattern>/*</url-pattern>

      <http-method>TRACE</http-method>

    </web-resource-collection>

    <auth-constraint />

  </security-constraint>

   

  <security-constraint>

    <web-resource-collection>

      <web-resource-name>other</web-resource-name>

      <url-pattern>/*</url-pattern>

    </web-resource-collection>

    <!-- no security constraint -->

  </security-constraint>

  

  <login-config>

    <auth-method>FORM</auth-method>

    <form-login-config>

      <form-login-page>/login</form-login-page>

      <form-error-page>/loginError</form-error-page>

    </form-login-config>

  </login-config>

  

  

  <!-- if specified, this value is used as the Hudson home directory -->

  <env-entry>

    <env-entry-name>HUDSON_HOME</env-entry-name>

    <env-entry-type>java.lang.String</env-entry-type>

    <env-entry-value></env-entry-value>

  </env-entry>

  

  <!-- configure additional extension-content-type mappings -->

  <mime-mapping>

    <extension>xml</extension>

    <mime-type>application/xml</mime-type>

  </mime-mapping>

  <!--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

    <extension>xsl</extension>

    <mime-type>application/xslt+xml</mime-type>

  </mime-mapping-->

  <mime-mapping>

    <extension>log</extension>

    <mime-type>text/plain</mime-type>

  </mime-mapping>

  <mime-mapping>

    <extension>war</extension>

    <mime-type>application/octet-stream</mime-type>

  </mime-mapping>

  <mime-mapping>

    <extension>ear</extension>

    <mime-type>application/octet-stream</mime-type>

  </mime-mapping>

  <mime-mapping>

    <extension>rar</extension>

    <mime-type>application/octet-stream</mime-type>

  </mime-mapping>

  <mime-mapping>

    <extension>webm</extension>

    <mime-type>video/webm</mime-type>

  </mime-mapping>

  

  <error-page>

    <exception-type>java.lang.Throwable</exception-type>

    <location>/oops</location>

  </error-page>

  

  <session-config>

    <cookie-config>

      <!-- See https://www.owasp.org/index.php/HttpOnly for the discussion of this topic in OWASP -->

      <http-only>true</http-only>

    </cookie-config>

    <!-- Tracking mode is managed by WebAppMain.FORCE_SESSION_TRACKING_BY_COOKIE_PROP -->

  </session-config>

</web-app>

提取某个单个字段

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# coding=utf-8

"""

    作者:gaojs

    功能:

    新增功能:

    日期:2022/6/2 17:12

"""

import xml.dom.minidom

  

  

dom = xml.dom.minidom.parse('web.xml')

root = dom.documentElement

bond_list = root.getElementsByTagName('filter-name')

  

print(bond_list[0].firstChild.data)

运行结果:

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

# coding=utf-8

"""

    作者:gaojs

    功能:

    新增功能:

    日期:2022/6/2 17:12

"""

import xml.dom.minidom

  

  

dom = xml.dom.minidom.parse('web.xml')

root = dom.documentElement

filter_list = root.getElementsByTagName('filter-name')

  

# print(filter_list[0].firstChild.data)

  

for bond in filter_list:

    s = bond.firstChild.data

    print(s)

    with open('filter_result.txt', 'a') as fin:

        fin.write(s + '\n')

文件结果:

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

1

2

3

4

5

6

7

with open('web.xml', mode='r') as fin:

    test = fin.read()

    result = re.findall('<filter-name>(.*?)</filter-name>', test)

    for key in result:

        print(key)

        with open('array/filter_result.txt', 'a') as f:

            f.write(key + '\n')

结果:


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/qq_41332844/article/details/126837421
相关文章
  • 简单有效上手Python3异步asyncio问题
    Python3异步asyncio问题 官方文档: https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio.run 看了一大堆相关的资料和教程,针对的Python版本不同,
  • python提取xml指定内容的方法
    第一种方法:python操作xml文件 随手找了一个xml文件内容(jenkins相关文件) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  • 22个Python的万用公式总结分享

    22个Python的万用公式总结分享
    在大家的日常python程序的编写过程中,都会有自己解决某个问题的解决办法,或者是在程序的调试过程中,用来帮助调试的程序公式。 小编
  • Numpy np.array()函数使用方法指南
    1、Numpy ndarray对象 numpy ndarray对象是一个n维数组对象,ndarray只能存储一系列相同元素。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #一维数组 [1,2,3,4] #shape(4,
  • Python实战之天气预报系统的实现

    Python实战之天气预报系统的实现
    鼎鼎大名的南方城市长沙很早就入冬了,街上各种大衣,毛衣,棉衣齐齐出动。 这段时间全国各地大风呜呜地吹,很多地方断崖式降温。
  • vscode配置与python虚拟环境切换的几种方式
    1. 采用工作区设置默认解释器的方式(推荐) 下载完vscode,并安装python支持之后。使用vscode打开一个空文件夹。点击左侧的运行与调试,创
  • 用python获取到照片拍摄时的详细位置(附源码)
    先看获取到的效果 拍摄时间:2021:12:18 16:22:13 照片拍摄地址:(内蒙古自治区包头市昆都仑区, 内蒙古自治区, 包头市, 昆都仑区, 多米幼儿园东
  • 基于Python实现24点游戏的代码
    24数大家之前玩过没有? 规则:一副扑克牌抽走大王,小王,K,Q,J(有的规则里面会抽走10,本文一律不抽走),之后在牌堆里随机抽取四
  • python如何去除字符串中指定字符
    python中的strip()可以去除头尾指定字符 1 2 ss = 我的电话是18827038***,也是微信号,\n 请加入,谢谢\n\n\n print(ss.strip(\n)) 结果: 我的电话是18
  • opencv调用yolov3模型深度学习目标检测实例介绍

    opencv调用yolov3模型深度学习目标检测实例介绍
    opencv调用yolov3模型进行深度学习目标检测,以实例进行代码详解 对于yolo v3已经训练好的模型,opencv提供了加载相关文件,进行图片检测的类
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计