鱼C论坛

 找回密码
 立即注册
查看: 4870|回复: 5

Python 解析多级重复嵌套XML文件

[复制链接]
发表于 2015-7-6 21:36:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. <ScreenInfo xmlns="http://www.hp.com/schemas/imaging/con/ledm/testservice/2011/11/28" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hp.com/schemas/imaging/con/ledm/testservice/2011/11/28 ../schemas/TestService.xsd">
  2. <ScreenId>flow_home::state_home</ScreenId>
  3. <ScreenLabel>Home</ScreenLabel>
  4. <LEDs>...</LEDs>
  5. <Widgets>
  6. <Widget>
  7. <Type>dashboard</Type>
  8. <Id>_DashBoard</Id>
  9. <Geometry>
  10. <X>0</X>
  11. <Y>-206</Y>
  12. <Width>320</Width>
  13. <Height>240</Height>
  14. </Geometry>
  15. <Widgets>
  16. <Widget>
  17. <Type>button</Type>
  18. <Id>_DashboardDragger</Id>
  19. <Geometry>
  20. <X>0</X>
  21. <Y>0</Y>
  22. <Width>320</Width>
  23. <Height>34</Height>
  24. </Geometry>
  25. <Actions>
  26. <Action>
  27. <Type>select</Type>
  28. </Action>
  29. </Actions>
  30. </Widget>
  31. </Widgets>
  32. </Widget>
  33. <Widget>
  34. <Type>icon</Type>
  35. <Id>view</Id>
  36. <Widgets>
  37. <Widget>
  38. <Type>grid</Type>
  39. <Id>home_grid</Id>
  40. <Geometry>
  41. <X>0</X>
  42. <Y>28</Y>
  43. <Width>320</Width>
  44. <Height>212</Height>
  45. </Geometry>
  46. <Actions>
  47. <Action>
  48. <Type>scrollNext</Type>
  49. </Action>
  50. </Actions>
  51. <Widgets>
  52. <Widget>
  53. <Type>grid</Type>
  54. <Id>shomewidget_p</Id>
  55. <Geometry>
  56. <X>0</X>
  57. <Y>192</Y>
  58. <Width>320</Width>
  59. <Height>48</Height>
  60. </Geometry>
  61. <Values>
  62. <Value StringId="0" Property="rowcount">1</Value>
  63. <Value StringId="0" Property="columncount">6</Value>
  64. <Value StringId="0" Property="scrollDirection">Ui::Horizontal</Value>
  65. </Values>
  66. <Actions>
  67. <Action>
  68. <Type>scrollNext</Type>
  69. </Action>
  70. </Actions>
  71. <Widgets>
  72. <Widget>
  73. <Type>button</Type>
  74. <Id>group.home</Id>
  75. <Geometry>
  76. <X>0</X>
  77. <Y>192</Y>
  78. <Width>71</Width>
  79. <Height>48</Height>
  80. </Geometry>
  81. <Values>
  82. <Value StringId="0" Property="index">0</Value>
  83. <Value StringId="0" Property="icon">18098</Value>
  84. <Value StringId="0" Property="subtext">SELECTED</Value>
  85. </Values>
  86. <Actions>
  87. <Action>
  88. <Type>select</Type>
  89. </Action>
  90. </Actions>
  91. <Widgets></Widgets>
  92. </Widget>
  93. <Widget>
  94. <Type>button</Type>
  95. <Id>group.grcopy</Id>
  96. <Geometry>
  97. <X>71</X>
  98. <Y>192</Y>
  99. <Width>71</Width>
  100. <Height>48</Height>
  101. </Geometry>
  102. <Values>
  103. <Value StringId="0" Property="index">1</Value>
  104. <Value StringId="0" Property="icon">18098</Value>
  105. <Value StringId="6624" Property="text">Copy</Value>
  106. <Value StringId="0" Property="subtext">NOT SELECTED</Value>
  107. </Values>
  108. <Actions>
  109. <Action>
  110. <Type>select</Type>
  111. </Action>
  112. </Actions>
  113. <Widgets></Widgets>
  114. </Widget>
  115. <Widget>
  116. <Type>button</Type>
  117. <Id>group.grscan</Id>
  118. <Geometry>
  119. <X>142</X>
  120. <Y>192</Y>
  121. <Width>71</Width>
  122. <Height>48</Height>
  123. </Geometry>
  124. <Values>
  125. <Value StringId="0" Property="index">2</Value>
  126. <Value StringId="0" Property="icon">18098</Value>
  127. <Value StringId="2473" Property="text">Scan</Value>
  128. <Value StringId="0" Property="subtext">NOT SELECTED</Value>
  129. </Values>
  130. <Actions>
  131. <Action>
  132. <Type>select</Type>
  133. </Action>
  134. </Actions>
  135. <Widgets></Widgets>
  136. </Widget>
  137. <Widget>
  138. <Type>button</Type>
  139. <Id>group.grprint</Id>
  140. <Geometry>
  141. <X>213</X>
  142. <Y>192</Y>
  143. <Width>71</Width>
  144. <Height>48</Height>
  145. </Geometry>
  146. <Values>
  147. <Value StringId="0" Property="index">3</Value>
  148. <Value StringId="0" Property="icon">18098</Value>
  149. <Value StringId="2475" Property="text">Photo</Value>
  150. <Value StringId="0" Property="subtext">NOT SELECTED</Value>
  151. </Values>
  152. <Actions>
  153. <Action>
  154. <Type>select</Type>
  155. </Action>
  156. </Actions>
  157. <Widgets></Widgets>
  158. </Widget>
  159. <Widget>
  160. <Type>button</Type>
  161. <Id>group.grfax</Id>
  162. <Geometry>
  163. <X>284</X>
  164. <Y>192</Y>
  165. <Width>71</Width>
  166. <Height>48</Height>
  167. </Geometry>
  168. <Values>
  169. <Value StringId="0" Property="index">4</Value>
  170. <Value StringId="0" Property="icon">18098</Value>
  171. <Value StringId="9312" Property="text">Fax</Value>
  172. <Value StringId="0" Property="subtext">NOT SELECTED</Value>
  173. </Values>
  174. <Actions>
  175. <Action>
  176. <Type>select</Type>
  177. </Action>
  178. </Actions>
  179. <Widgets></Widgets>
  180. </Widget>
  181. </Widgets>
  182. </Widget>
  183. <Widget>
  184. <Type>grid</Type>
  185. <Id>_s</Id>
  186. <Geometry>
  187. <X>0</X>
  188. <Y>28</Y>
  189. <Width>320</Width>
  190. <Height>172</Height>
  191. </Geometry>
  192. <Values>
  193. <Value StringId="0" Property="rowcount">1</Value>
  194. <Value StringId="0" Property="columncount">2</Value>
  195. <Value StringId="0" Property="scrollDirection">Ui::Horizontal</Value>
  196. </Values>
  197. <Actions>
  198. <Action>
  199. <Type>scrollNext</Type>
  200. </Action>
  201. </Actions>
  202. <Widgets>
  203. <Widget>
  204. <Type>button</Type>
  205. <Id>command.copy_doc</Id>
  206. <Geometry>
  207. <X>12</X>
  208. <Y>70</Y>
  209. <Width>124</Width>
  210. <Height>118</Height>
  211. </Geometry>
  212. <Values>
  213. <Value StringId="0" Property="command">copy_doc</Value>
  214. <Value StringId="0" Property="index">0</Value>
  215. <Value StringId="0" Property="icon">17220</Value>
  216. <Value StringId="16709" Property="secondarytext">Copy Document</Value>
  217. </Values>
  218. <Actions>
  219. <Action>
  220. <Type>select</Type>
  221. </Action>
  222. </Actions>
  223. <Widgets></Widgets>
  224. </Widget>
  225. <Widget>
  226. <Type>button</Type>
  227. <Id>command.scan_memory</Id>
  228. <Geometry>
  229. <X>136</X>
  230. <Y>70</Y>
  231. <Width>124</Width>
  232. <Height>118</Height>
  233. </Geometry>
  234. <Values>
  235. <Value StringId="0" Property="command">scan_memory</Value>
  236. <Value StringId="0" Property="index">1</Value>
  237. <Value StringId="0" Property="icon">17220</Value>
  238. <Value StringId="15769" Property="secondarytext">Scan to USB</Value>
  239. </Values>
  240. <Actions>
  241. <Action>
  242. <Type>select</Type>
  243. </Action>
  244. </Actions>
  245. <Widgets></Widgets>
  246. </Widget>
  247. <Widget>
  248. <Type>button</Type>
  249. <Id>command.copy_id</Id>
  250. <Geometry>
  251. <X>260</X>
  252. <Y>70</Y>
  253. <Width>124</Width>
  254. <Height>118</Height>
  255. </Geometry>
  256. <Values>
  257. <Value StringId="0" Property="command">copy_id</Value>
  258. <Value StringId="0" Property="index">2</Value>
  259. <Value StringId="0" Property="icon">17220</Value>
  260. <Value StringId="16710" Property="secondarytext">Copy ID Card</Value>
  261. </Values>
  262. <Actions>
  263. <Action>
  264. <Type>select</Type>
  265. </Action>
  266. </Actions>
  267. <Widgets></Widgets>
  268. </Widget>
  269. </Widgets>
  270. </Widget>
  271. </Widgets>
  272. </Widget>
  273. </Widgets>
  274. </Widget>
  275. </Widgets>
  276. </ScreenInfo>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-7-8 13:18:15 | 显示全部楼层
  1. import xml.etree.ElementTree as ET
  2. fil = open('test.xml') # 把你的文件保存为test.xml
  3. xml = fil.read()
  4. fil.close()

  5. root = ET.fromstring(xml)
  6. width = root[3][0][2][2] # Widgets > Widget > Geometry > Width
  7. print width.text # Width的内容,是320
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-7-8 13:20:26 | 显示全部楼层
http://m.baidu.com/from=844b/bd_page_type=1/ssid=0/uid=0/pu=usm%400%2Csz%401321_1001%2Cta%40utouch_2_4.4_3_537/baiduid=5752D3A30188DE4D76F8E565D6690CFF/w=0_10_xml.etree.ElementTree/t=wap/l=3/tc?ref=www_utouch&lid=9165275184619484301&order=1&vit=osres&tj=www_normal_1_0_10_title&m=8&srd=1&dict=32&title=python开发_xml.etree.ElementTree_XML文件操..._博客园&sec=4781&di=a8839cbc8667adae&bdenc=1&nsrc=IlPT2AEptyoA_yixCFOxXnANedT62v3IEQGG_ytK1DK6mlrte4viZQRAWjz6N7uTHYCb9n00sqdNbybwKzFshdICaPQrfURnliGGwLiqtcDlS1h1sdYq4sLUTSVmyPjzk_
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-7-8 23:29:53 | 显示全部楼层

谢谢,我试试看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-21 09:02:20 | 显示全部楼层
那如果需要循環取出資料,放進資料庫呢?

要怎麼做比較好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-21 10:37:55 | 显示全部楼层
beautifulsoup 支持解析xml
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-16 17:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表