Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将父对象追加到xml_Python_Lxml_Elementtree - Fatal编程技术网

Python 将父对象追加到xml

Python 将父对象追加到xml,python,lxml,elementtree,Python,Lxml,Elementtree,我想在xml文件中再添加一个块。基本上,在父级Tss下,我想创建带有属性的子分包条目。下面是我要添加到xml文件中的内容: <Entry> <System string = "rbs005019"/> <Type string = "SECURE"/> <User string = "rbs"/> <Password string

我想在xml文件中再添加一个块。基本上,在父级
Tss
下,我想创建带有属性的子分包
条目
。下面是我要添加到xml文件中的内容:

         <Entry>
            <System string = "rbs005019"/>
            <Type string = "SECURE"/>
            <User string = "rbs"/>
            <Password string = "rbs005019"/>
        </Entry>

这是xml文件

   <ManagedElement sourceType = "CELLO">
        <ManagedElementId string = "rbs005019"/>
        <Tss>
            <Entry>
                <System string = "rbs005019"/>
                <Type string = "NORMAL"/>
                <User string = "rbs"/>
                <Password string = "rbs005019"/>
            </Entry>
        </Tss>
    </ManagedElement>

所以在梳理之后,它应该看起来像:

  <ManagedElement sourceType = "CELLO">
        <ManagedElementId string = "rbs005019"/>
        <Tss>
            <Entry>
                <System string = "rbs005019"/>
                <Type string = "NORMAL"/>
                <User string = "rbs"/>
                <Password string = "rbs005019"/>
            </Entry>
            <Entry>
                <System string = "rbs005019"/>
                <Type string = "SECURE"/>
                <User string = "rbs"/>
                <Password string = "rbs005019"/>
            </Entry>
        </Tss>
        </ManagedElement>


我使用的是Python2.6和
lxml.etree

lxml具有函数
parentElem.insert(position,new_元素)
,允许您在其父元素下插入新的子元素。您可以找到一个示例和(部分元素是列表)

以下是使用insert的示例:

In [31]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:current = """ <ManagedElement sourceType = "CELLO">
:        <ManagedElementId string = "rbs005019"/>
:        <Tss>
:            <Entry>
:                <System string = "rbs005019"/>
:                <Type string = "NORMAL"/>
:                <User string = "rbs"/>
:                <Password string = "rbs005019"/>
:            </Entry>
:        </Tss>
:    </ManagedElement>
:"""
:<EOF>

In [32]: current = etree.fromstring(current)

In [33]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:want = """
: <Entry>
:            <System string = "rbs005019"/>
:            <Type string = "SECURE"/>
:            <User string = "rbs"/>
:            <Password string = "rbs005019"/>
:        </Entry>
:"""
:<EOF>

In [34]: want = etree.fromstring(want)

In [35]: current.find('./Tss').insert(0,want)

In [36]: print etree.tostring(current, pretty_print=True)
<ManagedElement sourceType="CELLO">
        <ManagedElementId string="rbs005019"/>
        <Tss>
            <Entry>
            <System string="rbs005019"/>
            <Type string="SECURE"/>
            <User string="rbs"/>
            <Password string="rbs005019"/>
        </Entry>
        <Entry>
            <System string="rbs005019"/>
            <Type string="NORMAL"/>
            <User string="rbs"/>
            <Password string="rbs005019"/>
        </Entry>
       </Tss>
    </ManagedElement>
[31]中的
:%cpaste
粘贴代码;在行中单独输入“---”以停止或使用Ctrl-D。
:current=“”
:        
:        
:            
:                
:                
:                
:                
:            
:        
:    
:"""
:
在[32]中:current=etree.fromstring(当前)
在[33]:%cpaste中
粘贴代码;在行中单独输入“---”以停止或使用Ctrl-D。
:want=“”
: 
:            
:            
:            
:            
:        
:"""
:
在[34]中:want=etree.fromstring(want)
[35]中:current.find('./Tss').insert(0,want)
在[36]中:打印etree.tostring(当前,pretty\u print=True)
插入发生在这一行:
current.find('./Tss').insert(0,want)