欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
python使用ElementTree解析xml
    公司使用TestLink作為T(mén)est case管理工具,有時(shí)需要將Test link中的數據作為數據源進(jìn)行一些分析。手工?No!
思路:
Test case in Testlink ----export-----> XML file -----parse----> data structure ----analyze----> Result

著(zhù)重說(shuō)怎么解析,其他都很好辦:)

Python中解析XML的工具很多,SAX,DOM,各種,個(gè)人偏愛(ài)使用直觀(guān)的樹(shù)形結構來(lái)解析xml,在python中的實(shí)現就是ElementTree.

首先導入lib。
ElementTree在 Python 標準庫中有兩種實(shí)現。一種是純 Python 實(shí)現例如 xml.etree.ElementTree ,另外一種是速度快一點(diǎn)的 xml.etree.cElementTree 。盡量使用 C 語(yǔ)言實(shí)現的那種,因為它速度更快,而且消耗的內存更少。以下代碼會(huì )在有C實(shí)現的etree系統上優(yōu)先使用C的實(shí)現,如果沒(méi)有,使用Python的實(shí)現,避免trigger exception。

  1. try:
  2.     import xml.etree.cElementTree as ET
  3. except ImportError:
  4.     import xml.etree.ElementTree as ET

好了,接下來(lái)我們來(lái)解析testlink導出的xml文件。下面一段是導出的xml文件,現在我需要的是解析出externalid, name,summary,steps,expected result,

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <testsuite name="New DH Group Support" >
  3. <node_order><![CDATA[1]]></node_order>
  4. <details><![CDATA[]]>
  5. </details><testsuite name="DH Group 14" >
  6. <node_order><![CDATA[1]]></node_order>
  7. <details><![CDATA[]]>
  8. </details>
  9. <testcase internalid="270258" name="BOVPN-Interop-Encryptions-Route-Main-P1-SHA1-DES-DHG14">
  10.     <node_order><![CDATA[0]]></node_order>
  11.     <externalid><![CDATA[108852]]></externalid>
  12.     <summary><![CDATA[<p>pc1--fb1(external)---(external)fb2--pc2</p>]]></summary>
  13.     <steps><![CDATA[<p>1.Config bovpn gateway:set fb1 &amp; fb2 to main mode,</p>
  14. <p>P1:SHA1-DES-DHG14,others are default settings.</p>
  15. <p>2.Check the traffic can pass through the tunnel. </p>]]></steps>
  16.     <expectedresults><![CDATA[<p>Step2:the traffic can pass through the tunnel. </p>]]></expectedresults>
  17. </testcase>

  18. <testcase internalid="270260" name="BOVPN-Interop-Encryptions-Route-Main-P1-SHA1-3DES-DHG14">
  19.     <node_order><![CDATA[0]]></node_order>
  20.     <externalid><![CDATA[108853]]></externalid>
  21.     <summary><![CDATA[<p>pc1--fb1(external)---(external)fb2--pc2</p>]]></summary>
  22.     <steps><![CDATA[<p>1.Config bovpn gateway:set fb1 &amp; fb2 to main mode,</p>
  23. <p>P1:SHA1-3DES-DHG14,others are default settings.</p>
  24. <p>2.Check the traffic can pass through the tunnel. </p>]]></steps>
  25.     <expectedresults><![CDATA[<p>Step2:the traffic can pass through the tunnel. </p>]]></expectedresults>
  26. </testcase>

  27. <testcase internalid="270262" name="BOVPN-Interop-Encryptions-Route-Main-P1-SHA1-AES-128bit-DHG14">
  28.     <node_order><![CDATA[0]]></node_order>
  29.     <externalid><![CDATA[108854]]></externalid>
  30.     <summary><![CDATA[<p>pc1--fb1(external)---(external)fb2--pc2</p>]]></summary>
  31.     <steps><![CDATA[<p>1.Config bovpn gateway:set fb1 &amp; fb2 to main mode,</p>
  32. <p>P1:SHA1-AES-128bit-DHG14,others are default settings.</p>
  33. <p>2.Check the traffic can pass through the tunnel. </p>]]></steps>
  34.     <expectedresults><![CDATA[<p>Step2:the traffic can pass through the tunnel. </p>]]></expectedresults>
  35. </testcase>
  36. </testsuite></testsuite>
下面一個(gè)類(lèi)我們來(lái)解析每一個(gè)我需要的數據

  1. class TestCaseXml:

  2.     def __init__(self, xmlFile):
  3.         self.tree = ET.parse(xmlFile)
  4.         self.root = self.tree.getroot()
  5.         # return self.root

  6.     def getTCName(self, tc):
  7.         # get name attribute
  8.         return tc.get('name')
  9.     
  10.     def getTCId(self, tc):
  11.         # get externalid child data
  12.         return tc.find('externalid').text
  13.     
  14.     def getTCStep(self, tc):
  15.         return tc.find('steps').text
  16.     
  17.     def getTCExpect(self, tc):
  18.         return tc.find('expectedresults').text
  19.     
  20.     def getTCSummary(self, tc):
  21.         return tc.find('summary').text

  22.     def rootObj(self):
  23.         return self.root
  24.     
  25.     def test(self):
  26.         for tc in self.root.findall('.//testcase'):
  27.             print self.getTCName(tc).lower()+'-'+self.getTCId(tc)

代碼不做過(guò)多解釋?zhuān)芎?jiǎn)單,self.root.findall()找到所有的testcase對象,然后遍歷每個(gè)對象,獲得每個(gè)對象中包含對應字段的值,注意這里使用tc.find().text直接可以返回CDATA的內容,非常方便。

這里只是一個(gè)例子,實(shí)際工作中可以將其保存在一個(gè)元素為dict的list中,進(jìn)行各種分析之類(lèi)的,就是很簡(jiǎn)單的事情了

這里只使用了基本的方法,更多使用幫助見(jiàn)官方文檔:http://docs.python.org/2/library/xml.etree.elementtree.html
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Python3 XML解析處理-ElementTree
669fb864tc76c16d35844 (300×300)
用Python解析XML的幾種常見(jiàn)方法的介紹_python_腳本之家
Python 進(jìn)階(八):XML 基本操作
第104天: Python 解析 XML
如何在XML中使用nbsp
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久