如何从不同的XML文件中获取元素以通过python创建新元素?

时间:2011-06-28 04:22:05

标签: python xml

我有两个xml文件:

1.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

2.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
      </book>

我需要创建一个新的xml文件3.xml,其中包含1.xml和2.xml的内容,如下所示: 3.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
     <root>        
      <bookstore>
      <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
      </book>
      <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>
      <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
      </book>
     </bookstore>
     <book category="WEB">
       <title lang="en">Learning XML</title>
       <author>Erik T. Ray</author>
       <year>2003</year>
       <price>39.95</price>
     </book>
    </root>

我正在使用Python ElementTree模块来解析1.xml和2.xml,然后创建一个新文件。但是它给了我错误: TypeError:无法序列化(键入元素) 我正在使用的代码是:

from xml.etree import ElementTree as ET
#Tree for 1.xml
tree = ET.parse('1.xml')
root = tree.getroot()
Bookstore = root.find('bookstore')
#Tree for 2.xml
tree2 = ET.parse('2.xml')
root2 = tree2.getroot()
#3.xml
root_element = ET.Element("root")
child = ET.SubElement(root_element,Bookstore)
child = ET.SubElement(root_element,root2)
tree = ET.ElementTree(root_element)
tree.write("3.xml")

当我运行这个程序时,它在写入3.xml时给了我“无法序列化”的错误

1 个答案:

答案 0 :(得分:1)

SubElement函数需要标记名称(文本字符串)作为第二个参数,而不是元素。而不是致电SubElement尝试append。脚本的最后一部分应该是:

#3.xml
root_element = ET.Element("root")
root_element.append(Bookstore)
root_element.append(root2)
tree = ET.ElementTree(root_element)
tree.write("3.xml")