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

python lxml insert xml file into xml string

我有一个与此类似的 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>

我该怎么做?

这是使用 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))