python lxml将xml文件插入xml字符串

时间:2015-08-24 03:15:57

标签: python lxml

我有一个类似于此的XML文件:

<tes:variable xmlns:tes="http://www.tidalsoftware.com/client/tesservlet" xmlns="http://purl.org/atom/ns#">
  <tes:ownername>OWNER</tes:ownername>
  <tes:productiondate>2015-08-23T00:00:00-0400</tes:productiondate>
  <tes:readonly>N</tes:readonly>
  <tes:publish>N</tes:publish>
  <tes:description>JIRA-88</tes:description>
  <tes:startcalendar>0</tes:startcalendar>
  <tes:ownerid>88</tes:ownerid>
  <tes:type>2</tes:type>
  <tes:innervalue>4</tes:innervalue>
  <tes:calc>N</tes:calc>
  <tes:name>test_number3</tes:name>
  <tes:startdate>1899-12-30T00:00:00-0500</tes:startdate>
  <tes:pub>Y</tes:pub>
  <tes:lastvalue>0</tes:lastvalue>
  <tes:id>2078</tes:id>
  <tes:startdateasstring>18991230000000</tes:startdateasstring>
</tes:variable>

我需要做的是将其嵌入到以下XML中,将<object></object>元素替换为文件中的所有内容。

<?xml version="1.0" encoding="UTF-8" ?>
<entry xmlns="http://purl.org/atom/ns#">
    <tes:Variable.update xmlns:tes="http://www.tidalsoftware.com/client/tesservlet">
        <object></object>
    </tes:Variable.update>
</entry>

我该怎么做?

1 个答案:

答案 0 :(得分:1)

这是使用lxml将元素替换为另一个元素的一种可能方法(请参阅注释以了解其工作原理):

....
....
#assume that 'tree' is variable containing the parsed template XML...
#and 'content_tree' is variable containing the actual content to be embedded, parsed

#get the container element to be replaced
container = tree.xpath('//d:object', namespaces={'d':'http://purl.org/atom/ns#'})[0]

#get parent of the container element
parent = container.getparent()

#replace container element with the actual content element
parent.replace(container, content_tree)

这是一个有效的演示示例:

import lxml.etree as etree

file_content = '''<tes:variable xmlns:tes="http://www.tidalsoftware.com/client/tesservlet" xmlns="http://purl.org/atom/ns#">
  <tes:ownername>OWNER</tes:ownername>
  <tes:productiondate>2015-08-23T00:00:00-0400</tes:productiondate>
  <tes:readonly>N</tes:readonly>
  <tes:publish>N</tes:publish>
  <tes:description>JIRA-88</tes:description>
  <tes:startcalendar>0</tes:startcalendar>
  <tes:ownerid>88</tes:ownerid>
  <tes:type>2</tes:type>
  <tes:innervalue>4</tes:innervalue>
  <tes:calc>N</tes:calc>
  <tes:name>test_number3</tes:name>
  <tes:startdate>1899-12-30T00:00:00-0500</tes:startdate>
  <tes:pub>Y</tes:pub>
  <tes:lastvalue>0</tes:lastvalue>
  <tes:id>2078</tes:id>
  <tes:startdateasstring>18991230000000</tes:startdateasstring>
</tes:variable>'''

template = '''<?xml version="1.0" encoding="UTF-8" ?>
<entry xmlns="http://purl.org/atom/ns#">
    <tes:Variable.update xmlns:tes="http://www.tidalsoftware.com/client/tesservlet">
        <object></object>
    </tes:Variable.update>
</entry>'''

tree = etree.fromstring(template)
container = tree.xpath('//d:object', namespaces={'d':'http://purl.org/atom/ns#'})[0]
parent = container.getparent()

content_tree = etree.fromstring(file_content)

parent.replace(container, content_tree)

print(etree.tostring(tree))