如何使用 Python 从 XML 文件复制特定的 XML 记录块?
How to copy a particular XML record block from XML file using Python?
我想从 XML 文件中提取特定的 XML 文件块并使用 Python 3.8 将其复制到另一个文件。我尝试了所有类似问题的答案。不幸的是,我做不到。任何帮助将不胜感激。
样本XML文件
<?xml version="1.0" encoding="utf-8"?>
<bookstores>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<miscellaneous id="1000611004" />
</bookstore>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<miscellaneous id="1000611067" />
</bookstore>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<miscellaneous id="3450611067" />
</bookstore>
</bookstores>
示例Python脚本
在这里,我正在检查 orderIds 列表中的项目是否与 miscellaneous[ 的 id 属性匹配=32=]。如果匹配,则需要将整个 XML 块复制到另一个文件。
orderIds = ["1000611004", "1000611067"]
mytree = ET.parse(xmlFile)
myroot = mytree.getroot()
for x in myroot.iter():
if(x.tag == 'miscellaneous'):
attribute = x.attrib
idToCheck = attribute['id']
for id in orderIds:
if(id == idToCheck):
--Confused Part To Be Filled--
预期输出
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<miscellaneous id="1000611004" />
</bookstore>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<miscellaneous id="1000611067" />
</bookstore>
你可以试试:
tree = ET.parse('xmlfile')
root = tree.getroot()
orderIds = ["1000611004", "1000611067"]
bookstore_nodes = root.findall('.//bookstore')
with open('output.xml', 'w') as f:
for bn in bookstore_nodes:
misc_node = bn.findall('.//miscellaneous')
if len(misc_node) > 0 and misc_node[0].attrib['id'] in orderIds:
f.write(ET.tostring(bn).decode('utf-8'))
我想从 XML 文件中提取特定的 XML 文件块并使用 Python 3.8 将其复制到另一个文件。我尝试了所有类似问题的答案。不幸的是,我做不到。任何帮助将不胜感激。
样本XML文件
<?xml version="1.0" encoding="utf-8"?>
<bookstores>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<miscellaneous id="1000611004" />
</bookstore>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<miscellaneous id="1000611067" />
</bookstore>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<miscellaneous id="3450611067" />
</bookstore>
</bookstores>
示例Python脚本
在这里,我正在检查 orderIds 列表中的项目是否与 miscellaneous[ 的 id 属性匹配=32=]。如果匹配,则需要将整个 XML 块复制到另一个文件。
orderIds = ["1000611004", "1000611067"]
mytree = ET.parse(xmlFile)
myroot = mytree.getroot()
for x in myroot.iter():
if(x.tag == 'miscellaneous'):
attribute = x.attrib
idToCheck = attribute['id']
for id in orderIds:
if(id == idToCheck):
--Confused Part To Be Filled--
预期输出
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<miscellaneous id="1000611004" />
</bookstore>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<miscellaneous id="1000611067" />
</bookstore>
你可以试试:
tree = ET.parse('xmlfile')
root = tree.getroot()
orderIds = ["1000611004", "1000611067"]
bookstore_nodes = root.findall('.//bookstore')
with open('output.xml', 'w') as f:
for bn in bookstore_nodes:
misc_node = bn.findall('.//miscellaneous')
if len(misc_node) > 0 and misc_node[0].attrib['id'] in orderIds:
f.write(ET.tostring(bn).decode('utf-8'))