使用Python元素树库将文本附加到XML文件

时间:2017-03-09 15:51:48

标签: python xml elementtree

我需要创建一个XML文件,它将N作为用户的输入,看起来像这样(这里,N = 3):

<test name="test1">                                                                
    <parameter name="username" value="bhusriva1" />  
    <classes>
        <class name="bhuv.mllab.tests.DensityTest" />
    </classes>
</test>
<test name="test2">
    <parameter name="username" value="bhusriva2" />
    <classes>
        <class name="bhuv.mllab.tests.DensityTest" />
    </classes>
</test>
<test name="test3">
    <parameter name="username" value="bhusriva3" />
    <classes>
        <class name="bhuv.mllab.tests.DensityTest" />
    </classes>
</test> 

所以,为了做到这一点,我创建了以下代码,如下所示:

from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
import xml.etree.ElementTree as etree

#Number of users to create: Edit this part
N_users = 1

#Counter to terminate the loop and ID's
counter = 0
usr_num = 1

root = Element('test')
root.text = '<parameter name="username" value="bhusriva1" />'
tree = ElementTree(root)
name = Element('classes')
root.append(name)
name.text = '\n<class name="bhuv.mllab.tests.DensityTest" />\n'
root.set('name', 'test1')
print(etree.tostring(root))
tree.write(open('create_users_multi_browser.xml', 'w'))

while (N_users - 1) > counter:
    counter = counter + 1
    usr_num = usr_num + 1
    root = Element('test')
    temp1 = "<parameter name=" + "\"" + "username"+ "\"" + " value=" + "\"" + "bhusriva" + str(usr_num) + "\"" +" />"  
    root.text = temp1
    tree = ElementTree(root)
    name = Element('classes')
    root.append(name)
    name.text = '<class name="bhuv.mllab.tests.DensityTest" />'
    temp2 = "test" + str(usr_num)
    root.set('name', temp2)
    print(etree.tostring(root))
    tree.write(open("create_users_multi_browser.xml", "a"))

此处,对于N_users = 1,代码工作正常,但对于N_users > 1,它在Internet Explorer中提供以下输出:

<parameter name="username" value="bhusriva1" /> <class name="bhuv.mllab.tests.DensityTest" />

那么,代码中需要纠正的是什么?一个全新的代码,有一些不同的库使用对我来说也没关系。

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。首先,如果你使用root.text,那么&lt;和&gt;被字符串转义版本(&amp; gt和&amp; lt)取代。其次,你不必保持这么多的柜台。 Python直接为你做到了!如果我们修复这两个问题(我已添加注释来解释发生了什么)并稍微清理逻辑,那么我们得到以下结果:

 from xml.etree.ElementTree import Element
 from xml.etree.ElementTree import SubElement
 import xml.etree.ElementTree as etree

 # Number of users to create: Edit this part
 users = 3

 op = ""
 for i in xrange(1, users + 1):
     # Create test element
     root = Element('test')
     root.set('name', 'test' + str(i))

     # Create parameter element
     params = SubElement(root, 'parameter')
     params.set('name' , 'username')
     params.set('value', 'bhusriva' + str(i))

     # Create classes element
     classes = SubElement(root, 'classes')

     # Add class subelement 
     class_element = SubElement(classes, 'class')
     class_element.set('name', 'bhuv.mllab.tests.DensityTest')

     # Add output to string
     op = op + etree.tostring(root)

 # Write output to file
 with open('create_users_multi_browser.xml', 'w') as f:
     f.write(op)

运行它会得到输出:

<test name="test1">
  <parameter name="username" value="bhusriva1" />
  <classes>
    <class name="bhuv.mllab.tests.DensityTest" />
  </classes>
</test>
<test name="test2">
  <parameter name="username" value="bhusriva2" />
  <classes>
    <class name="bhuv.mllab.tests.DensityTest" />
  </classes>
</test>
<test name="test3">
  <parameter name="username" value="bhusriva3" />
  <classes>
    <class name="bhuv.mllab.tests.DensityTest" />
  </classes>
</test>

希望这有帮助!