2017-12-05 9 views
1

CSVファイルを読み込み、XMLスニペットを作成する必要があるため、特定の領域(別のノード内の別のスニペット)に配置する必要があります。 。 最初の部分が完成しました。 2番目の部分を行う必要があります。この分野でいくつかの助けが必要です。また結果は、新しいXMLファイルに書き込む必要があります。Pythonの別のxmlにXMLフラグメントを追加する

import csv 
 
import lxml.etree as ET 
 
import xml.etree.cElementTree as ETT 
 
from lxml import etree, html 
 

 
csvFile = open('parameters.csv') 
 
readCsv = csv.reader(csvFile) 
 
data = [] 
 

 
for row in readCsv: 
 
    data.append(row) 
 
csvFile.close() 
 

 
def convert_row(row): 
 
    return """<hashTree><TransactionController guiclass="TransactionControllerGui" testclass="TransactionController" testname="%s" enabled="true"> 
 
      <boolProp name="TransactionController.includeTimers">false</boolProp> 
 
      <boolProp name="TransactionController.parent">false</boolProp> 
 
     </TransactionController> 
 
     <hashTree> 
 
      <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true"> 
 
      <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> 
 
       <collectionProp name="Arguments.arguments" /> 
 
      </elementProp> 
 
      <stringProp name="HTTPSampler.domain">"%s" 
 
      </stringProp> 
 
      <stringProp name="HTTPSampler.port"> 
 
      </stringProp> 
 
      <stringProp name="HTTPSampler.protocol"> 
 
      </stringProp> 
 
      <stringProp name="HTTPSampler.contentEncoding"> 
 
      </stringProp> 
 
      <stringProp name="HTTPSampler.path">"%s" 
 
      </stringProp> 
 
      <stringProp name="HTTPSampler.method">"%s"</stringProp> 
 
      <boolProp name="HTTPSampler.follow_redirects">true</boolProp> 
 
      <boolProp name="HTTPSampler.auto_redirects">false</boolProp> 
 
      <boolProp name="HTTPSampler.use_keepalive">true</boolProp> 
 
      <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp> 
 
      <stringProp name="HTTPSampler.embedded_url_re"> 
 
      </stringProp> 
 
      <stringProp name="HTTPSampler.connect_timeout"> 
 
      </stringProp> 
 
      <stringProp name="HTTPSampler.response_timeout"> 
 
      </stringProp> 
 
      </HTTPSamplerProxy> 
 
     </hashTree> 
 
     </hashTree>"""%(row[0],row[1],row[2],row[3]) 
 

 
fragment = '\n'.join([convert_row(row) for row in data[1:]]) 
 

 
TF = ET.parse('testXml.xml') 
 
content = TF.findall("//jmeterTestPlan/hashTree/hashTree/hashTree/") 
 
content.append(fragment) 
 

 
print(content)

答えて

0

あなたは追加する前に、XMLに文字列を変換する必要があります。

fragment = '\n'.join([convert_row(row) for row in data[1:]]) 
aet=ET.fromstring(fragment) 
TF = ET.ElementTree(file='testXml.xml') 
for child in TF.xpath('//jmeterTestPlan/hashTree/hashTree[1]'): 
child.append(aet) 
TF.write('out.jmx') 
+0

ありがとうVinishそれは私のために働いた。 – Karthik